fixed support for Twig loaders when they do not extend Twig_ExistsLoaderInterface

This commit is contained in:
Fabien Potencier 2012-12-13 17:11:23 +01:00
parent f005649315
commit fc444f1a55
3 changed files with 58 additions and 4 deletions

View File

@ -101,14 +101,14 @@ class ExceptionController
// when not in debug, try to find a template for the specific HTTP status code and format
if (!$debug) {
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig');
if ($this->twig->getLoader()->exists($template)) {
if ($this->templateExists($template)) {
return $template;
}
}
// try to find a template for the given format
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
if ($this->twig->getLoader()->exists($template)) {
if ($this->templateExists($template)) {
return $template;
}
@ -117,4 +117,22 @@ class ExceptionController
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig');
}
// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface && $loader->exists($template)) {
return true;
}
try {
$loader->getSource($template);
return true;
} catch (Twig_Error_Loader $e) {
}
return false;
}
}

View File

@ -82,7 +82,7 @@ class ExceptionController
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
$template = $this->getTemplate();
if (!$this->twig->getLoader()->exists($template)) {
if (!$this->templateExists($template)) {
$handler = new ExceptionHandler();
return new Response($handler->getStylesheet($exception));
@ -95,4 +95,22 @@ class ExceptionController
{
return '@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig';
}
// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface && $loader->exists($template)) {
return true;
}
try {
$loader->getSource($template);
return true;
} catch (Twig_Error_Loader $e) {
}
return false;
}
}

View File

@ -107,7 +107,7 @@ class TemplateManager
$template = substr($template, 0, -10);
}
if (!$this->twig->getLoader()->exists($template.'.html.twig')) {
if (!$this->templateExists($template.'.html.twig')) {
throw new \UnexpectedValueException(sprintf('The profiler template "%s.html.twig" for data collector "%s" does not exist.', $template, $name));
}
@ -116,4 +116,22 @@ class TemplateManager
return $templates;
}
// to be removed when the minimum required version of Twig is >= 2.0
protected function templateExists($template)
{
$loader = $this->twig->getLoader();
if ($loader instanceof \Twig_ExistsLoaderInterface && $loader->exists($template)) {
return true;
}
try {
$loader->getSource($template);
return true;
} catch (Twig_Error_Loader $e) {
}
return false;
}
}