Skip to content

Commit

Permalink
bug #20326 [VarDumper] Fix dumping Twig source in stack traces (nicol…
Browse files Browse the repository at this point in the history
…as-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
  • Loading branch information
fabpot committed Oct 27, 2016
2 parents 5abd889 + f16d44d commit 74ed9e1
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 17 deletions.
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
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
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
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
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) {
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'));
}
}

0 comments on commit 74ed9e1

Please sign in to comment.