Skip to content

Commit 4744c3d

Browse files
bug #20175 [VarDumper] Fix source links with latests Twig versions (nicolas-grekas)
This PR was merged into the 2.8 branch. Discussion ---------- [VarDumper] Fix source links with latests Twig versions | Q | A | ------------- | --- | Branch? | 2.8 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Sibling to #20173 Commits ------- f3b09d9 [VarDumper] Fix source links with latests Twig versions
2 parents ee8203a + f3b09d9 commit 4744c3d

File tree

4 files changed

+113
-32
lines changed

4 files changed

+113
-32
lines changed

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

+11-7
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,19 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
150150

151151
if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
152152
$template = isset($f['object']) ? $f['object'] : new $f['class'](new \Twig_Environment(new \Twig_Loader_Filesystem()));
153-
154-
try {
155-
$templateName = $template->getTemplateName();
156-
$templateSrc = explode("\n", method_exists($template, 'getSource') ? $template->getSource() : $template->getEnvironment()->getLoader()->getSource($templateName));
157-
$templateInfo = $template->getDebugInfo();
158-
if (isset($templateInfo[$f['line']])) {
153+
$templateName = $template->getTemplateName();
154+
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
155+
$templateInfo = $template->getDebugInfo();
156+
if (isset($templateInfo[$f['line']])) {
157+
if (method_exists($template, 'getSourceContext')) {
158+
$templateName = $template->getSourceContext()->getPath() ?: $templateName;
159+
}
160+
if ($templateSrc) {
161+
$templateSrc = explode("\n", $templateSrc);
159162
$src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
163+
} else {
164+
$src[$templateName] = $templateInfo[$f['line']];
160165
}
161-
} catch (\Twig_Error_Loader $e) {
162166
}
163167
}
164168
} else {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\VarDumper\Tests\Caster;
13+
14+
use Symfony\Component\VarDumper\Caster\FrameStub;
15+
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
16+
17+
class ExceptionCasterTest extends VarDumperTestCase
18+
{
19+
/**
20+
* @requires function Twig_Template::getSourceContext
21+
*/
22+
public function testFrameWithTwig()
23+
{
24+
require_once dirname(__DIR__).'/Fixtures/Twig.php';
25+
26+
$f = array(
27+
new FrameStub(array(
28+
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
29+
'line' => 19,
30+
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
31+
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem())),
32+
)),
33+
new FrameStub(array(
34+
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
35+
'line' => 19,
36+
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
37+
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()), null),
38+
)),
39+
);
40+
41+
$expectedDump = <<<'EODUMP'
42+
array:2 [
43+
0 => {
44+
class: "__TwigTemplate_VarDumperFixture_u75a09"
45+
object: __TwigTemplate_VarDumperFixture_u75a09 {
46+
%A
47+
}
48+
src: {
49+
%sTwig.php:19: """
50+
// line 2\n
51+
throw new \Exception('Foobar');\n
52+
}\n
53+
"""
54+
bar.twig:2: """
55+
foo bar\n
56+
twig source\n
57+
\n
58+
"""
59+
}
60+
}
61+
1 => {
62+
class: "__TwigTemplate_VarDumperFixture_u75a09"
63+
object: __TwigTemplate_VarDumperFixture_u75a09 {
64+
%A
65+
}
66+
src: {
67+
%sTwig.php:19: """
68+
// line 2\n
69+
throw new \Exception('Foobar');\n
70+
}\n
71+
"""
72+
foo.twig:2: """
73+
foo bar\n
74+
twig source\n
75+
\n
76+
"""
77+
}
78+
}
79+
]
80+
81+
EODUMP;
82+
83+
$this->assertDumpMatchesFormat($expectedDump, $f);
84+
}
85+
}

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

+9-14
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,9 @@ public function testClosedResource()
201201
);
202202
}
203203

204+
/**
205+
* @requires function Twig_Template::getSourceContext
206+
*/
204207
public function testThrowingCaster()
205208
{
206209
$out = fopen('php://memory', 'r+b');
@@ -235,19 +238,6 @@ public function testThrowingCaster()
235238
rewind($out);
236239
$out = stream_get_contents($out);
237240

238-
if (method_exists($twig, 'getSource')) {
239-
$twig = <<<EOTXT
240-
foo.twig:2: """
241-
foo bar\\n
242-
twig source\\n
243-
\\n
244-
"""
245-
246-
EOTXT;
247-
} else {
248-
$twig = '';
249-
}
250-
251241
$r = defined('HHVM_VERSION') ? '' : '#%d';
252242
$this->assertStringMatchesFormat(
253243
<<<EOTXT
@@ -269,7 +259,12 @@ public function testThrowingCaster()
269259
throw new \Exception('Foobar');\\n
270260
}\\n
271261
"""
272-
{$twig} }
262+
bar.twig:2: """
263+
foo bar\\n
264+
twig source\\n
265+
\\n
266+
"""
267+
}
273268
}
274269
%d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
275270
src: {

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

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
/* foo.twig */
44
class __TwigTemplate_VarDumperFixture_u75a09 extends Twig_Template
55
{
6-
public function __construct(Twig_Environment $env)
6+
private $filename;
7+
8+
public function __construct(Twig_Environment $env, $filename = 'bar.twig')
79
{
810
parent::__construct($env);
9-
1011
$this->parent = false;
11-
12-
$this->blocks = array(
13-
);
12+
$this->blocks = array();
13+
$this->filename = $filename;
1414
}
1515

1616
protected function doDisplay(array $context, array $blocks = array())
@@ -26,14 +26,11 @@ public function getTemplateName()
2626

2727
public function getDebugInfo()
2828
{
29-
return array (19 => 2);
29+
return array(19 => 2);
3030
}
3131

32-
public function getSource()
32+
public function getSourceContext()
3333
{
34-
return " foo bar
35-
twig source
36-
37-
";
34+
return new Twig_Source(" foo bar\n twig source\n\n", 'foo.twig', $this->filename);
3835
}
3936
}

0 commit comments

Comments
 (0)