prefer getSourceContext() over getSource()

This commit is contained in:
Christian Flothmann 2016-11-07 19:42:43 +01:00
parent 589d019f2e
commit adbc529b7b
4 changed files with 11 additions and 7 deletions

View File

@ -36,7 +36,7 @@ class TranslationExtensionTest extends \PHPUnit_Framework_TestCase
$twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false)); $twig = new \Twig_Environment($loader, array('debug' => true, 'cache' => false));
$twig->addExtension(new TranslationExtension(new Translator('en', new MessageSelector()))); $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)); $this->assertEquals($expected, $this->getTemplate($template)->render($variables));
} }

View File

@ -75,14 +75,14 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface
$loader = $this->environment->getLoader(); $loader = $this->environment->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) { if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
return $loader->exists((string) $name); return $loader->exists((string) $name);
} }
try { try {
// cast possible TemplateReferenceInterface to string because the // cast possible TemplateReferenceInterface to string because the
// EngineInterface supports them but Twig_LoaderInterface does not // EngineInterface supports them but Twig_LoaderInterface does not
$loader->getSource((string) $name); $loader->getSourceContext((string) $name)->getCode();
} catch (\Twig_Error_Loader $e) { } catch (\Twig_Error_Loader $e) {
return false; return false;
} }

View File

@ -131,12 +131,12 @@ class ExceptionController
$template = (string) $template; $template = (string) $template;
$loader = $this->twig->getLoader(); $loader = $this->twig->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) { if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
return $loader->exists($template); return $loader->exists($template);
} }
try { try {
$loader->getSource($template); $loader->getSourceContext($template)->getCode();
return true; return true;
} catch (\Twig_Error_Loader $e) { } catch (\Twig_Error_Loader $e) {

View File

@ -140,12 +140,16 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
} }
$loader = $this->templating->getLoader(); $loader = $this->templating->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) { if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
return $loader->exists($template); return $loader->exists($template);
} }
try { try {
$loader->getSource($template); if (method_exists($loader, 'getSourceContext')) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}
return true; return true;
} catch (\Twig_Error_Loader $e) { } catch (\Twig_Error_Loader $e) {