8000 [TwigBridge][TwigBundle][HttpKernel] prefer getSourceContext() over getSource() by xabbuh · Pull Request #20440 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBridge][TwigBundle][HttpKernel] prefer getSourceContext() over getSource() #20440

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 7, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
prefer getSourceContext() over getSource()
  • Loading branch information
xabbuh committed Nov 7, 2016
commit adbc529b7b355e93b00c2f8db0a1c843c5c646c5
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function testTrans($template, $expected, array $variables = array())
$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));
}

Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Bridge/Twig/TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,14 +75,14 @@ public function exists($name)

$loader = $this->environment->getLoader();

if ($loader instanceof \Twig_ExistsLoaderInterface) {
if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In Twig 2.0, the exists() method will be present in the Twig_LoaderInterface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not needed as Twig_ExistsLoaderInterface still exist in 2.0. It will be removed in 3.0 only.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The call to exists() will never be made though if the loader does not implement Twig_ExistsLoaderInterface, but is just an instance of Twig_LoaderInterface.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

indeed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this seems to happen. Otherwise, I don't see how #20435 would ever reach the getSource() call.

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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,12 +131,12 @@ protected function templateExists($template)
$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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,16 @@ private function templateExists($template)
}

$loader = $this->templating->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface) {
if ($loader instanceof \Twig_ExistsLoaderInterface || method_exists($loader, 'exists')) {
return $loader->exists($template);
}

try {
$loader->getSource($template);
if (method_exists($loader, 'getSourceContext')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 as HttpKernel does not constrain Twig version.

$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}
Copy link
Contributor
@FrancescoBorzi FrancescoBorzi Nov 7, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest: ... } else if (method_exists($loader, 'getSource')) { ... instead of line 150


return true;
} catch (\Twig_Error_Loader $e) {
Expand Down
0