Sync Twig templateExists behaviors

This commit is contained in:
Thomas Calvet 2019-10-01 15:11:47 +02:00
parent d2b66ff592
commit d7682fee6c
6 changed files with 83 additions and 55 deletions

View File

@ -19,6 +19,7 @@ use Twig\Environment;
use Twig\Error\Error;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
use Twig\Template;
/**
@ -74,19 +75,24 @@ class TwigEngine implements EngineInterface, StreamingEngineInterface
$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);
}
/**

View File

@ -18,6 +18,7 @@ use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
/**
* ExceptionController renders error or exception pages for a given
@ -120,23 +121,28 @@ class ExceptionController
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);
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 false;
}
try {
$loader->getSourceContext($template)->getCode();
return true;
} catch (LoaderError $e) {
}
return false;
return $loader->exists($template);
}
}

View File

@ -19,6 +19,7 @@ use Symfony\Component\HttpKernel\Profiler\Profiler;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
/**
* ExceptionController.
@ -118,17 +119,22 @@ class ExceptionController
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof ExistsLoaderInterface) {
return $loader->exists($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 false;
}
try {
$loader->getSource($template);
return true;
} catch (LoaderError $e) {
}
return false;
return $loader->exists($template);
}
}

View File

@ -117,21 +117,22 @@ class TemplateManager
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);
}
}

View File

@ -15,6 +15,8 @@ use Symfony\Bundle\WebProfilerBundle\Profiler\TemplateManager;
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.
@ -124,11 +126,16 @@ class TemplateManagerTest extends TestCase
->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;

View File

@ -19,6 +19,7 @@ use Symfony\Component\Templating\EngineInterface;
use Twig\Environment;
use Twig\Error\LoaderError;
use Twig\Loader\ExistsLoaderInterface;
use Twig\Loader\SourceContextLoaderInterface;
/**
* Implements the Hinclude rendering strategy.
@ -137,22 +138,23 @@ class HIncludeFragmentRenderer extends RoutableFragmentRenderer
}
$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);
}
/**