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
This commit is contained in:
commit
4744c3de64
@ -150,15 +150,19 @@ class ExceptionCaster
|
|||||||
|
|
||||||
if (!empty($f['class']) && is_subclass_of($f['class'], 'Twig_Template') && method_exists($f['class'], 'getDebugInfo')) {
|
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'] : new $f['class'](new \Twig_Environment(new \Twig_Loader_Filesystem()));
|
||||||
|
|
||||||
try {
|
|
||||||
$templateName = $template->getTemplateName();
|
$templateName = $template->getTemplateName();
|
||||||
$templateSrc = explode("\n", method_exists($template, 'getSource') ? $template->getSource() : $template->getEnvironment()->getLoader()->getSource($templateName));
|
$templateSrc = method_exists($template, 'getSourceContext') ? $template->getSourceContext()->getCode() : (method_exists($template, 'getSource') ? $template->getSource() : '');
|
||||||
$templateInfo = $template->getDebugInfo();
|
$templateInfo = $template->getDebugInfo();
|
||||||
if (isset($templateInfo[$f['line']])) {
|
if (isset($templateInfo[$f['line']])) {
|
||||||
$src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
|
if (method_exists($template, 'getSourceContext')) {
|
||||||
|
$templateName = $template->getSourceContext()->getPath() ?: $templateName;
|
||||||
|
}
|
||||||
|
if ($templateSrc) {
|
||||||
|
$templateSrc = explode("\n", $templateSrc);
|
||||||
|
$src[$templateName.':'.$templateInfo[$f['line']]] = self::extractSource($templateSrc, $templateInfo[$f['line']], self::$srcContext);
|
||||||
|
} else {
|
||||||
|
$src[$templateName] = $templateInfo[$f['line']];
|
||||||
}
|
}
|
||||||
} catch (\Twig_Error_Loader $e) {
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -0,0 +1,85 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This file is part of the Symfony package.
|
||||||
|
*
|
||||||
|
* (c) Fabien Potencier <fabien@symfony.com>
|
||||||
|
*
|
||||||
|
* For the full copyright and license information, please view the LICENSE
|
||||||
|
* file that was distributed with this source code.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Symfony\Component\VarDumper\Tests\Caster;
|
||||||
|
|
||||||
|
use Symfony\Component\VarDumper\Caster\FrameStub;
|
||||||
|
use Symfony\Component\VarDumper\Test\VarDumperTestCase;
|
||||||
|
|
||||||
|
class ExceptionCasterTest extends VarDumperTestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* @requires function Twig_Template::getSourceContext
|
||||||
|
*/
|
||||||
|
public function testFrameWithTwig()
|
||||||
|
{
|
||||||
|
require_once dirname(__DIR__).'/Fixtures/Twig.php';
|
||||||
|
|
||||||
|
$f = array(
|
||||||
|
new FrameStub(array(
|
||||||
|
'file' => dirname(__DIR__).'/Fixtures/Twig.php',
|
||||||
|
'line' => 19,
|
||||||
|
'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,
|
||||||
|
'class' => '__TwigTemplate_VarDumperFixture_u75a09',
|
||||||
|
'object' => new \__TwigTemplate_VarDumperFixture_u75a09(new \Twig_Environment(new \Twig_Loader_Filesystem()), null),
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
|
||||||
|
$expectedDump = <<<'EODUMP'
|
||||||
|
array:2 [
|
||||||
|
0 => {
|
||||||
|
class: "__TwigTemplate_VarDumperFixture_u75a09"
|
||||||
|
object: __TwigTemplate_VarDumperFixture_u75a09 {
|
||||||
|
%A
|
||||||
|
}
|
||||||
|
src: {
|
||||||
|
%sTwig.php:19: """
|
||||||
|
// line 2\n
|
||||||
|
throw new \Exception('Foobar');\n
|
||||||
|
}\n
|
||||||
|
"""
|
||||||
|
bar.twig:2: """
|
||||||
|
foo bar\n
|
||||||
|
twig source\n
|
||||||
|
\n
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
1 => {
|
||||||
|
class: "__TwigTemplate_VarDumperFixture_u75a09"
|
||||||
|
object: __TwigTemplate_VarDumperFixture_u75a09 {
|
||||||
|
%A
|
||||||
|
}
|
||||||
|
src: {
|
||||||
|
%sTwig.php:19: """
|
||||||
|
// line 2\n
|
||||||
|
throw new \Exception('Foobar');\n
|
||||||
|
}\n
|
||||||
|
"""
|
||||||
|
foo.twig:2: """
|
||||||
|
foo bar\n
|
||||||
|
twig source\n
|
||||||
|
\n
|
||||||
|
"""
|
||||||
|
}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
EODUMP;
|
||||||
|
|
||||||
|
$this->assertDumpMatchesFormat($expectedDump, $f);
|
||||||
|
}
|
||||||
|
}
|
@ -201,6 +201,9 @@ EOTXT
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @requires function Twig_Template::getSourceContext
|
||||||
|
*/
|
||||||
public function testThrowingCaster()
|
public function testThrowingCaster()
|
||||||
{
|
{
|
||||||
$out = fopen('php://memory', 'r+b');
|
$out = fopen('php://memory', 'r+b');
|
||||||
@ -235,19 +238,6 @@ EOTXT
|
|||||||
rewind($out);
|
rewind($out);
|
||||||
$out = stream_get_contents($out);
|
$out = stream_get_contents($out);
|
||||||
|
|
||||||
if (method_exists($twig, 'getSource')) {
|
|
||||||
$twig = <<<EOTXT
|
|
||||||
foo.twig:2: """
|
|
||||||
foo bar\\n
|
|
||||||
twig source\\n
|
|
||||||
\\n
|
|
||||||
"""
|
|
||||||
|
|
||||||
EOTXT;
|
|
||||||
} else {
|
|
||||||
$twig = '';
|
|
||||||
}
|
|
||||||
|
|
||||||
$r = defined('HHVM_VERSION') ? '' : '#%d';
|
$r = defined('HHVM_VERSION') ? '' : '#%d';
|
||||||
$this->assertStringMatchesFormat(
|
$this->assertStringMatchesFormat(
|
||||||
<<<EOTXT
|
<<<EOTXT
|
||||||
@ -269,7 +259,12 @@ stream resource {@{$ref}
|
|||||||
throw new \Exception('Foobar');\\n
|
throw new \Exception('Foobar');\\n
|
||||||
}\\n
|
}\\n
|
||||||
"""
|
"""
|
||||||
{$twig} }
|
bar.twig:2: """
|
||||||
|
foo bar\\n
|
||||||
|
twig source\\n
|
||||||
|
\\n
|
||||||
|
"""
|
||||||
|
}
|
||||||
}
|
}
|
||||||
%d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
|
%d. Twig_Template->displayWithErrorHandling() ==> __TwigTemplate_VarDumperFixture_u75a09->doDisplay(): {
|
||||||
src: {
|
src: {
|
||||||
|
@ -3,14 +3,14 @@
|
|||||||
/* foo.twig */
|
/* foo.twig */
|
||||||
class __TwigTemplate_VarDumperFixture_u75a09 extends Twig_Template
|
class __TwigTemplate_VarDumperFixture_u75a09 extends Twig_Template
|
||||||
{
|
{
|
||||||
public function __construct(Twig_Environment $env)
|
private $filename;
|
||||||
|
|
||||||
|
public function __construct(Twig_Environment $env, $filename = 'bar.twig')
|
||||||
{
|
{
|
||||||
parent::__construct($env);
|
parent::__construct($env);
|
||||||
|
|
||||||
$this->parent = false;
|
$this->parent = false;
|
||||||
|
$this->blocks = array();
|
||||||
$this->blocks = array(
|
$this->filename = $filename;
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function doDisplay(array $context, array $blocks = array())
|
protected function doDisplay(array $context, array $blocks = array())
|
||||||
@ -29,11 +29,8 @@ class __TwigTemplate_VarDumperFixture_u75a09 extends Twig_Template
|
|||||||
return array(19 => 2);
|
return array(19 => 2);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getSource()
|
public function getSourceContext()
|
||||||
{
|
{
|
||||||
return " foo bar
|
return new Twig_Source(" foo bar\n twig source\n\n", 'foo.twig', $this->filename);
|
||||||
twig source
|
|
||||||
|
|
||||||
";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user