From fc444f1a55c449ac672be9420eff3c1ca131be6d Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 13 Dec 2012 17:11:23 +0100 Subject: [PATCH] fixed support for Twig loaders when they do not extend Twig_ExistsLoaderInterface --- .../Controller/ExceptionController.php | 22 +++++++++++++++++-- .../Controller/ExceptionController.php | 20 ++++++++++++++++- .../Profiler/TemplateManager.php | 20 ++++++++++++++++- 3 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php index 6039fb883c..8c07b90b91 100644 --- a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php @@ -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; + } } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php index 170d61a251..9a760c9f32 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php @@ -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; + } } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php index da59efb57b..87842dbdbc 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Profiler/TemplateManager.php @@ -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; + } }