bug #20440 [TwigBridge][TwigBundle][HttpKernel] prefer getSourceContext() over getSource() (xabbuh)
This PR was merged into the 2.7 branch.
Discussion
----------
[TwigBridge][TwigBundle][HttpKernel] prefer getSourceContext() over getSource()
| Q | A
| ------------- | ---
| Branch? | 2.7
| Bug fix? | yes
| New feature? | no
| BC breaks? | no
| Deprecations? | no
| Tests pass? | yes
| Fixed tickets | #20435
| License | MIT
| Doc PR | n/a
Commits
-------
adbc529
prefer getSourceContext() over getSource()
This commit is contained in:
commit
40dc73aa28
@ -36,7 +36,7 @@ class TranslationExtensionTest extends \PHPUnit_Framework_TestCase
|
||||
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
|
||||
$twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector())));
|
||||
|
||||
echo $twig->compile($twig->parse($twig->tokenize(new \Twig_Source($twig->getLoader()->getSource('index'), 'index'))))."\n\n";
|
||||
echo $twig->compile($twig->parse($twig->tokenize($twig->getLoader()->getSourceContext('index'))))."\n\n";
|
||||
$this->assertEquals($expected, $this->getTemplate($template)->render($variables));
|
||||
}
|
||||
|
||||
|
@ -75,14 +75,14 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface
|
||||
|
||||
$loader = $this->environment->getLoader();
|
||||
|
||||
if ($loader instanceof \Twig_ExistsLoaderInterface) {
|
||||
if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
|
||||
return $loader->exists((string) $name);
|
||||
}
|
||||
|
||||
try {
|
||||
// cast possible TemplateReferenceInterface to string because the
|
||||
// EngineInterface supports them but Twig_LoaderInterface does not
|
||||
$loader->getSource((string) $name);
|
||||
$loader->getSourceContext((string) $name)->getCode();
|
||||
} catch (\Twig_Error_Loader $e) {
|
||||
return false;
|
||||
}
|
||||
|
@ -131,12 +131,12 @@ class ExceptionController
|
||||
$template = (string) $template;
|
||||
|
||||
$loader = $this->twig->getLoader();
|
||||
if ($loader instanceof \Twig_ExistsLoaderInterface) {
|
||||
if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
|
||||
return $loader->exists($template);
|
||||
}
|
||||
|
||||
try {
|
||||
$loader->getSource($template);
|
||||
$loader->getSourceContext($template)->getCode();
|
||||
|
||||
return true;
|
||||
} catch (\Twig_Error_Loader $e) {
|
||||
|
@ -140,12 +140,16 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
|
||||
}
|
||||
|
||||
$loader = $this->templating->getLoader();
|
||||
if ($loader instanceof \Twig_ExistsLoaderInterface) {
|
||||
if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
|
||||
return $loader->exists($template);
|
||||
}
|
||||
|
||||
try {
|
||||
if (method_exists($loader, 'getSourceContext')) {
|
||||
$loader->getSourceContext($template);
|
||||
} else {
|
||||
$loader->getSource($template);
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (\Twig_Error_Loader $e) {
|
||||
|
Reference in New Issue
Block a user