Skip to content

[VarDumper] Fix dumping Twig source in stack traces #20326

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function dump(Data $data)
$info = $template->getDebugInfo();
if (isset($info[$trace[$i - 1]['line']])) {
$line = $info[$trace[$i - 1]['line']];
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : false;
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;

if ($src) {
$src = explode("\n", $src);
Expand Down Expand Up @@ -266,7 +266,7 @@ private function doDump($data, $name, $file, $line)
if (PHP_VERSION_ID >= 50400 && $this->dumper instanceof CliDumper) {
$contextDumper = function ($name, $file, $line, $fileLinkFormat) {
if ($this instanceof HtmlDumper) {
if ('' !== $file) {
if ($file) {
$s = $this->style('meta', '%s');
$name = strip_tags($this->style('', $name));
$file = strip_tags($this->style('', $file));
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
$src[$f['file'].':'.$f['line']] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);

if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
$template = isset($f['object']) ? $f['object'] : new $f['class'](new \Twig_Environment(new \Twig_Loader_Filesystem()));
$template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));

$templateName = $template->getTemplateName();
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
$templateInfo = $template->getDebugInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,27 +26,23 @@ public function testFrameWithTwig()
$f = array(
new FrameStub(array(
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
'line' => 19,
'line' => 21,
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem())),
)),
new FrameStub(array(
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
'line' => 19,
'line' => 21,
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()), null),
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, false),
)),
);

$expectedDump = <<<'EODUMP'
array:2 [
0 => {
class: "__TwigTemplate_VarDumperFixture_u75a09"
object: __TwigTemplate_VarDumperFixture_u75a09 {
%A
}
src: {
%sTwig.php:19: """
%sTwig.php:21: """
// line 2\n
throw new \Exception('Foobar');\n
}\n
Expand All @@ -64,7 +60,7 @@ class: "__TwigTemplate_VarDumperFixture_u75a09"
%A
}
src: {
%sTwig.php:19: """
%sTwig.php:21: """
// line 2\n
throw new \Exception('Foobar');\n
}\n
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/VarDumper/Tests/CliDumperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public function testThrowingCaster()
-trace: {
%d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
src: {
%sTwig.php:19: """
%sTwig.php:21: """
// line 2\\n
throw new \Exception('Foobar');\\n
}\\n
Expand Down
10 changes: 6 additions & 4 deletions src/Symfony/Component/VarDumper/Tests/Fixtures/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ class __TwigTemplate_VarDumperFixture_u75a09 extends Twig_Template
{
private $filename;

public function __construct(Twig_Environment $env, $filename = 'bar.twig')
public function __construct(Twig_Environment $env = null, $filename = null)
{
parent::__construct($env);
if (null !== $env) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if ($env)

parent::__construct($env);
}
$this->parent = false;
$this->blocks = array();
$this->filename = $filename;
Expand All @@ -26,11 +28,11 @@ public function getTemplateName()

public function getDebugInfo()
{
return array(19 => 2);
return array(21 => 2);
}

public function getSourceContext()
{
return new Twig_Source(" foo bar\n twig source\n\n", 'foo.twig', $this->filename);
return new Twig_Source(" foo bar\n twig source\n\n", 'foo.twig', false === $this->filename ? null : ($this->filename ?: 'bar.twig'));
}
}