From f16d44dbc6766c0c8bbdd6c4182cb70e7167a963 Mon Sep 17 00:00:00 2001 From: Nicolas Grekas Date: Thu, 27 Oct 2016 13:18:01 +0200 Subject: [PATCH] [VarDumper] Fix dumping Twig source in stack traces --- .../HttpKernel/DataCollector/DumpDataCollector.php | 4 ++-- .../Component/VarDumper/Caster/ExceptionCaster.php | 3 ++- .../VarDumper/Tests/Caster/ExceptionCasterTest.php | 14 +++++--------- .../Component/VarDumper/Tests/CliDumperTest.php | 2 +- .../Component/VarDumper/Tests/Fixtures/Twig.php | 10 ++++++---- 5 files changed, 16 insertions(+), 17 deletions(-) diff --git a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php index 538d73c783ba..3f5e6acf6344 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php @@ -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); @@ -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)); diff --git a/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php b/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php index 173d79dd41d6..25635399f7bd 100644 --- a/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php +++ b/src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php @@ -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(); diff --git a/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php b/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php index 4c72eb1f7529..1614f58a9ae9 100644 --- a/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php +++ b/src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php @@ -26,15 +26,14 @@ 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), )), ); @@ -42,11 +41,8 @@ public function testFrameWithTwig() 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 @@ -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 diff --git a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php index 36d45b6b5bb1..7a5ee0c5c3c0 100644 --- a/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php +++ b/src/Symfony/Component/VarDumper/Tests/CliDumperTest.php @@ -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 diff --git a/src/Symfony/Component/VarDumper/Tests/Fixtures/Twig.php b/src/Symfony/Component/VarDumper/Tests/Fixtures/Twig.php index 7ffdd2bd54a6..9eaa39c88909 100644 --- a/src/Symfony/Component/VarDumper/Tests/Fixtures/Twig.php +++ b/src/Symfony/Component/VarDumper/Tests/Fixtures/Twig.php @@ -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) { + parent::__construct($env); + } $this->parent = false; $this->blocks = array(); $this->filename = $filename; @@ -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')); } }