minor #33792 Sync Twig templateExists behaviors (fancyweb)
This PR was merged into the 3.4 branch.
Discussion
----------
Sync Twig templateExists behaviors
| Q | A
| ------------- | ---
| Branch? | 4.3
| Bug fix? | no
| New feature? | no
| Deprecations? | no
| Tickets | -
| License | MIT
| Doc PR | -
There are 5 places in the code that does the same check, but they are not consistent. The difficulty here is to support Twig 1, 2 & 3. I think relying on `Environment::MAJOR_VERSION` is OK. `method_exists` are useless IMO. The proof is that they are currenty missing in some of them.
Basically if Twig >= 2 -> the method `exists` exists on the loader so just call it.
For Twig 1, check `ExistsLoaderInterface`, then check for `SourceContextLoaderInterface`, then call `getSource()`.
Commits
-------
d7682fee6c
Sync Twig templateExists behaviors
This commit is contained in:
commit
ff194d9844
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user