8000 [VarDumper] Fix dumping twig templates found in exceptions · colinodell/symfony@0229e5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 0229e5c

Browse files
event15nicolas-grekas
authored andcommitted
[VarDumper] Fix dumping twig templates found in exceptions
1 parent 186e83a commit 0229e5c

File tree

3 files changed

+77
-32
lines changed

3 files changed

+77
-32
lines changed

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

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -214,18 +214,24 @@ public static function castFrameStub(FrameStub $frame, array $a, Stub $stub, $is
214214

215215
if (file_exists($f['file']) && 0 <= self::$srcContext) {
216216
if (!empty($f['class']) && (is_subclass_of($f['class'], 'Twig\Template') || is_subclass_of($f['class'], 'Twig_Template')) && method_exists($f['class'], 'getDebugInfo')) {
217-
$template = $f['object'] ?? unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
218-
219-
$ellipsis = 0;
220-
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
221-
$templateInfo = $template->getDebugInfo();
222-
if (isset($templateInfo[$f['line']])) {
223-
if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
224-
$templatePath = null;
225-
}
226-
if ($templateSrc) {
227-
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
228-
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
217+
$template = null;
218+
if (isset($f['object'])) {
219+
$template = $f['object'];
220+
} elseif ((new \ReflectionClass($f['class']))->isInstantiable()) {
221+
$template = unserialize(sprintf('O:%d:"%s":0:{}', \strlen($f['class']), $f['class']));
222+
}
223+
if (null !== $template) {
224+
$ellipsis = 0;
225+
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
226+
$templateInfo = $template->getDebugInfo();
227+
if (isset($templateInfo[$f['line']])) {
228+
if (!method_exists($template, 'getSourceContext') || !file_exists($templatePath = $template->getSourceContext()->getPath())) {
229+
$templatePath = null;
230+
}
231+
if ($templateSrc) {
232+
$src = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext, 'twig', $templatePath, $f);
233+
$srcKey = ($templatePath ?: $template->getTemplateName()).':'.$templateInfo[$f['line']];
234+
}
229235
}
230236
}
231237
}

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

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,12 @@
1515
use Symfony\Component\VarDumper\Caster\Caster;
1616
use Symfony\Component\VarDumper\Caster\ExceptionCaster;
1717
use Symfony\Component\VarDumper\Caster\FrameStub;
18+
use Symfony\Component\VarDumper\Caster\TraceStub;
1819
use Symfony\Component\VarDumper\Cloner\VarCloner;
20+
use Symfony\Component\VarDumper\Dumper\CliDumper;
1921
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
2022
use Symfony\Component\VarDumper\Test\VarDumperTestTrait;
23+
use Symfony\Component\VarDumper\Tests\Fixtures\DumpClassWithErrors;
2124

2225
class ExceptionCasterTest extends TestCase
2326
{
@@ -44,15 +47,15 @@ public function testDefaultSettings()
4447
#message: "foo"
4548
#code: 0
4649
#file: "%sExceptionCasterTest.php"
47-
#line: 28
50+
#line: %d
4851
trace: {
49-
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
52+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
5053
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
5154
› {
5255
› return new \Exception(''.$msg);
5356
› }
5457
}
55-
%s%eTests%eCaster%eExceptionCasterTest.php:40 { …}
58+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
5659
%A
5760
EODUMP;
5861

@@ -66,13 +69,13 @@ public function testSeek()
6669

6770
$expectedDump = <<<'EODUMP'
6871
{
69-
%s%eTests%eCaster%eExceptionCasterTest.php:28 {
72+
%s%eTests%eCaster%eExceptionCasterTest.php:%d {
7073
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
7174
› {
7275
› return new \Exception(''.$msg);
7376
› }
7477
}
75-
%s%eTests%eCaster%eExceptionCasterTest.php:65 { …}
78+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
7679
%A
7780
EODUMP;
7881

@@ -89,15 +92,15 @@ public function testNoArgs()
8992
#message: "1"
9093
#code: 0
9194
#file: "%sExceptionCasterTest.php"
92-
#line: 28
95+
#line: %d
9396
trace: {
94-
%sExceptionCasterTest.php:28 {
97+
%sExceptionCasterTest.php:%d {
9598
Symfony\Component\VarDumper\Tests\Caster\ExceptionCasterTest->getTestException($msg, &$ref = null)
9699
› {
97100
› return new \Exception(''.$msg);
98101
› }
99102
}
100-
%s%eTests%eCaster%eExceptionCasterTest.php:84 { …}
103+
%s%eTests%eCaster%eExceptionCasterTest.php:%d { …}
101104
%A
102105
EODUMP;
103106

@@ -114,16 +117,40 @@ public function testNoSrcContext()
114117
#message: "1"
115118
#code: 0
116119
#file: "%sExceptionCasterTest.php"
117-
#line: 28
120+
#line: %d
118121
trace: {
119-
%s%eTests%eCaster%eExceptionCasterTest.php:28
122+
%s%eTests%eCaster%eExceptionCasterTest.php:%d
120123
%s%eTests%eCaster%eExceptionCasterTest.php:%d
121124
%A
122125
EODUMP;
123126

124127
$this->assertDumpMatchesFormat($expectedDump, $e);
125128
}
126129

130+
public function testShouldReturnTraceForConcreteTwigWithError()
131+
{
132+
require_once \dirname(__DIR__).'/Fixtures/Twig.php';
133+
134+
$innerExc = (new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__))->provideError();
135+
$nestingWrapper = new \stdClass();
136+
$nestingWrapper->trace = new TraceStub($innerExc->getTrace());
137+
138+
$expectedDump = <<<'EODUMP'
139+
{
140+
+"trace": {
141+
%sTwig.php:%d {
142+
AbstractTwigTemplate->provideError()
143+
› {
144+
› return $this->createError();
145+
› }
146+
}
147+
%sExceptionCasterTest.php:%d { …}
148+
%A
149+
EODUMP;
150+
151+
$this->assertDumpMatchesFormat($expectedDump, $nestingWrapper);
152+
}
153+
127154
public function testHtmlDump()
128155
{
129156
if (ini_get('xdebug.file_link_format') || get_cfg_var('xdebug.file_link_format')) {
@@ -146,10 +173,10 @@ public function testHtmlDump()
146173
#<span class=sf-dump-protected title="Protected property">code</span>: <span class=sf-dump-num>0</span>
147174
#<span class=sf-dump-protected title="Protected property">file</span>: "<span class=sf-dump-str title="%sExceptionCasterTest.php
148175
%d characters"><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%e</span>Tests%eCaster%eExceptionCasterTest.php</span>"
149-
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>28</span>
176+
#<span class=sf-dump-protected title="Protected property">line</span>: <span class=sf-dump-num>%d</span>
150177
<span class=sf-dump-meta>trace</span>: {<samp>
151178
<span class=sf-dump-meta title="%sExceptionCasterTest.php
152-
Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%e</span>Tests%eCaster%eExceptionCasterTest.php</span>:<span class=sf-dump-num>28</span>
179+
Stack level %d."><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%s%eVarDumper</span><span class="sf-dump-ellipsis sf-dump-ellipsis-path">%e</span>Tests%eCaster%eExceptionCasterTest.php</span>:<span class=sf-dump-num>%d</span>
153180
&hellip;%d
154181
</samp>}
155182
</samp>}
@@ -169,12 +196,12 @@ public function testFrameWithTwig()
169196
$f = [
170197
new FrameStub([
171198
'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
172-
'line' => 20,
199+
'line' => 33,
173200
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
174201
]),
175202
new FrameStub([
176203
'file' => \dirname(__DIR__).'/Fixtures/Twig.php',
177-
'line' => 21,
204+
'line' => 34,
178205
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
179206
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(null, __FILE__),
180207
]),
@@ -186,7 +213,7 @@ public function testFrameWithTwig()
186213
class: "__TwigTemplate_VarDumperFixture_u75a09"
187214
src: {
188215
%sTwig.php:1 {
189-
216+
%s
190217
› foo bar
191218
› twig source
192219
}
@@ -201,12 +228,11 @@ class: "__TwigTemplate_VarDumperFixture_u75a09"
201228
%sExceptionCasterTest.php:2 {
202229
› foo bar
203230
› twig source
204-
231+
%s
205232
}
206233
}
207234
}
208235
]
209-
210236
EODUMP;
211237

212238
$this->assertDumpMatchesFormat($expectedDump, $f);
@@ -221,7 +247,7 @@ public function testExcludeVerbosity()
221247
#message: "foo"
222248
#code: 0
223249
#file: "%sExceptionCasterTest.php"
224-
#line: 28
250+
#line: %d
225251
}
226252
EODUMP;
227253

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,20 @@
11
<?php
22

3+
abstract class AbstractTwigTemplate extends Twig\Template
4+
{
5+
private function createError()
6+
{
7+
return new \RuntimeException('Manually triggered error.');
8+
}
9+
10+
public function provideError()
11+
{
12+
return $this->createError();
13+
}
14+
}
15+
316
/* foo.twig */
4-
class __TwigTemplate_VarDumperFixture_u75a09 extends Twig\Template
17+
class __TwigTemplate_VarDumperFixture_u75a09 extends AbstractTwigTemplate
518
{
619
private $path;
720

@@ -28,7 +41,7 @@ public function getTemplateName()
2841

2942
public function getDebugInfo()
3043
{
31-
return [20 => 1, 21 => 2];
44+
return [33 => 1, 34 => 2];
3245
}
3346

3447
public function getSourceContext()

0 commit comments

Comments
 (0)
0