Skip to content

Sync Twig templateExists behaviors #33792

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
Oct 2, 2019
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
24 changes: 15 additions & 9 deletions src/Symfony/Bridge/Twig/TwigEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Twig\Error\Error;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
use Twig\Template;

/**
Expand Down Expand Up @@ -74,19 +75,24 @@ public function exists($name)

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

if ($loader instanceof ExistsLoaderInterface || method_exists($loader, 'exists')) {
return $loader->exists((string) $name);
}
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
try {
// cast possible TemplateReferenceInterface to string because the
// EngineInterface supports them but LoaderInterface does not
if ($loader instanceof SourceContextLoaderInterface) {
$loader->getSourceContext((string) $name);
} else {
$loader->getSource((string) $name);
}

return true;
} catch (LoaderError $e) {
}

try {
// cast possible TemplateReferenceInterface to string because the
// EngineInterface supports them but LoaderInterface does not
$loader->getSourceContext((string) $name)->getCode();
} catch (LoaderError $e) {
return false;
}

return true;
return $loader->exists((string) $name);
}

/**
Expand Down
24 changes: 15 additions & 9 deletions src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;

/**
* ExceptionController renders error or exception pages for a given
Expand Down Expand Up @@ -120,23 +121,28 @@ protected function findTemplate(Request $request, $format, $code, $showException
return sprintf('@Twig/Exception/%s.html.twig', $showException ? 'exception_full' : $name);
}

// to be removed when the minimum required version of Twig is >= 3.0
// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists($template)
{
$template = (string) $template;

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

try {
$loader->getSourceContext($template)->getCode();
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
try {
if ($loader instanceof SourceContextLoaderInterface) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}

return true;
} catch (LoaderError $e) {
}

return true;
} catch (LoaderError $e) {
return false;
}

return false;
return $loader->exists($template);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;

/**
* ExceptionController.
Expand Down Expand Up @@ -118,17 +119,22 @@ protected function getTemplate()
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof ExistsLoaderInterface) {
return $loader->exists($template);
}

try {
$loader->getSource($template);
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
try {
if ($loader instanceof SourceContextLoaderInterface) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}

return true;
} catch (LoaderError $e) {
}

return true;
} catch (LoaderError $e) {
return false;
}

return false;
return $loader->exists($template);
}
}
23 changes: 12 additions & 11 deletions src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,22 @@ public function getNames(Profile $profile)
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof ExistsLoaderInterface) {
return $loader->exists($template);
}

try {
if ($loader instanceof SourceContextLoaderInterface || method_exists($loader, 'getSourceContext')) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
try {
if ($loader instanceof SourceContextLoaderInterface) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}

return true;
} catch (LoaderError $e) {
}

return true;
} catch (LoaderError $e) {
return false;
}

return false;
return $loader->exists($template);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
use Symfony\Bundle\WebProfilerBundle\Tests\TestCase;
use Symfony\Component\HttpKernel\Profiler\Profile;
use Twig\Environment;
use Twig\Loader\LoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;

/**
* Test for TemplateManager class.
Expand Down Expand Up @@ -124,11 +126,16 @@ protected function mockTwigEnvironment()
->method('loadTemplate')
->willReturn('loadedTemplate');

if (interface_exists('Twig\Loader\SourceContextLoaderInterface')) {
$loader = $this->getMockBuilder('Twig\Loader\SourceContextLoaderInterface')->getMock();
if (Environment::MAJOR_VERSION > 1) {
$loader = $this->createMock(LoaderInterface::class);
$loader
->expects($this->any())
->method('exists')
->willReturn(true);
} else {
$loader = $this->getMockBuilder('Twig\Loader\LoaderInterface')->getMock();
$loader = $this->createMock(SourceContextLoaderInterface::class);
}

$this->twigEnvironment->expects($this->any())->method('getLoader')->willReturn($loader);

return $this->twigEnvironment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;

/**
* Implements the Hinclude rendering strategy.
Expand Down Expand Up @@ -137,22 +138,23 @@ private function templateExists($template)
}

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

try {
if (method_exists($loader, 'getSourceContext')) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
if (1 === Environment::MAJOR_VERSION && !$loader instanceof ExistsLoaderInterface) {
try {
if ($loader instanceof SourceContextLoaderInterface) {
$loader->getSourceContext($template);
} else {
$loader->getSource($template);
}

return true;
} catch (LoaderError $e) {
}

return true;
} catch (LoaderError $e) {
return false;
}

return false;
return $loader->exists($template);
}

/**
Expand Down