8000 bug #20326 [VarDumper] Fix dumping Twig source in stack traces (nicol… · symfony/symfony@74ed9e1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 74ed9e1

Browse files
committed
bug #20326 [VarDumper] Fix dumping Twig source in stack traces (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [VarDumper] Fix dumping Twig source in stack traces | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #20320 | License | MIT | Doc PR | - Commits ------- f16d44d [VarDumper] Fix dumping Twig source in stack traces
2 parents 5abd889 + f16d44d commit 74ed9e1

File tree

5 files changed

+16
-17
lines changed

5 files changed

+16
-17
lines changed

src/Symfony/Component/HttpKernel/DataCollector/DumpDataCollector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function dump(Data $data)
103103
$info = $template->getDebugInfo();
104104
if (isset($info[$trace[$i - 1]['line']])) {
105105
$line = $info[$trace[$i - 1]['line']];
106-
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : false;
106+
$file = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getPath() : null;
107107

108108
if ($src) {
109109
$src = explode("\n", $src);
@@ -266,7 +266,7 @@ private function doDump($data, $name, $file, $line)
266266
if (PHP_VERSION_ID >= 50400 && $this->dumper instanceof CliDumper) {
267267
$contextDumper = function ($name, $file, $line, $fileLinkFormat) {
268268
if ($this instanceof HtmlDumper) {
269-
if ('' !== $file) {
269+
if ($file) {
270270
$s = $this->style('meta', '%s');
271271
$name = strip_tags($this->style('', $name));
272272
$file = strip_tags($this->style('', $file));

src/Symfony/Component/VarDumper/Caster/ExceptionCaster.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
149149
$src[$f['file'].':'.$f['line']] = self::extractSource(explode("\n", file_get_contents($f['file'])), $f['line'], self::$srcContext);
150150

151151
if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
152-
$template = isset($f['object']) ? $f['object'] : new $f['class'](new \Twig_Environment(new \Twig_Loader_Filesystem()));
152+
$template = isset($f['object']) ? $f['object'] : unserialize(sprintf('O:%d:"%s":0:{}', strlen($f['class']), $f['class']));
153+
153154
$templateName = $template->getTemplateName();
154155
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
155156
$templateInfo = $template->getDebugInfo();

src/Symfony/Component/VarDumper/Tests/Caster/ExceptionCasterTest.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,23 @@ public function testFrameWithTwig()
2626
$f = array(
2727
new FrameStub(array(
2828
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
29-
'line' => 19,
29+
'line' => 21,
3030
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
31-
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem())),
3231
)),
3332
new FrameStub(array(
3433
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
35-
'line' => 19,
34+
'line' => 21,
3635
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
37-
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()), null),
36+
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, false),
3837
)),
3938
);
4039

4140
$expectedDump = <<<'EODUMP'
4241
array:2 [
4342
0 => {
4443
class: "__TwigTemplate_VarDumperFixture_u75a09"
45-
object: __TwigTemplate_VarDumperFixture_u75a09 {
46-
%A
47-
}
4844
src: {
49-
%sTwig.php:19: """
45+
%sTwig.php:21: """
5046
// line 2\n
5147
throw new \Exception('Foobar');\n
5248
}\n
@@ -64,7 +60,7 @@ class: "__TwigTemplate_VarDumperFixture_u75a09"
6460
%A
6561
}
6662
src: {
67-
%sTwig.php:19: """
63+
%sTwig.php:21: """
6864
// line 2\n
6965
throw new \Exception('Foobar');\n
7066
}\n

src/Symfony/Component/VarDumper/Tests/CliDumperTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ public function testThrowingCaster()
254254
-trace: {
255255
%d. __TwigTemplate_VarDumperFixture_u75a09->doDisplay() ==> new Exception(): {
256256
src: {
257-
%sTwig.php:19: """
257+
%sTwig.php:21: """
258258
// line 2\\n
259259
throw new \Exception('Foobar');\\n
260260
}\\n

src/Symfony/Component/VarDumper/Tests/Fixtures/Twig.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ class __TwigTemplate_VarDumperFixture_u75a09 extends Twig_Template
55
{
66
private $filename;
77

8-
public function __construct(Twig_Environment $env, $filename = 'bar.twig')
8+
public function __construct(Twig_Environment $env = null, $filename = null)
99
{
10-
parent::__construct($env);
10+
if (null !== $env) {
11+
parent::__construct($env);
12+
}
1113
$this->parent = false;
1214
$this->blocks = array();
1315
$this->filename = $filename;
@@ -26,11 +28,11 @@ public function getTemplateName()
2628

2729
public function getDebugInfo()
2830
{
29-
return array(19 => 2);
31+
return array(21 => 2);
3032
}
3133

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

0 commit comments

Comments
 (0)
0