From 35d63df044cba20cdf441963ca85a7f4d51200cc Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 13 Dec 2012 15:44:37 +0100 Subject: [PATCH] removed the dependency on the container for exception handling --- src/Symfony/Bundle/TwigBundle/CHANGELOG.md | 1 + .../Controller/ExceptionController.php | 46 +++++++++++-------- .../DependencyInjection/Configuration.php | 2 +- .../TwigBundle/Resources/config/twig.xml | 6 +++ .../Controller/ExceptionController.php | 32 ++++++++----- .../Resources/config/profiler.xml | 6 +++ .../views/Collector/exception.html.twig | 2 +- 7 files changed, 62 insertions(+), 33 deletions(-) diff --git a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md index 01834ca16c..8c03bdbbf1 100644 --- a/src/Symfony/Bundle/TwigBundle/CHANGELOG.md +++ b/src/Symfony/Bundle/TwigBundle/CHANGELOG.md @@ -4,6 +4,7 @@ CHANGELOG 2.2.0 ----- + * moved the exception controller to be a service (`twig.controller.exception:showAction` vs `Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction`) * added support for multiple loaders via the "twig.loader" tag. * added automatic registration of namespaced paths for registered bundles * added support for namespaced paths diff --git a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php index 54be66a8fc..6039fb883c 100644 --- a/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php @@ -13,9 +13,9 @@ namespace Symfony\Bundle\TwigBundle\Controller; use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface; use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference; -use Symfony\Component\DependencyInjection\ContainerAware; use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; +use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Response; /** @@ -23,8 +23,17 @@ use Symfony\Component\HttpFoundation\Response; * * @author Fabien Potencier */ -class ExceptionController extends ContainerAware +class ExceptionController { + protected $twig; + protected $debug; + + public function __construct(\Twig_Environment $twig, $debug) + { + $this->twig = $twig; + $this->debug = $debug; + } + /** * Converts an Exception to a Response. * @@ -36,17 +45,16 @@ class ExceptionController extends ContainerAware * * @throws \InvalidArgumentException When the exception template does not exist */ - public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html') + public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html') { - $this->container->get('request')->setRequestFormat($format); + $request->setRequestFormat($format); - $currentContent = $this->getAndCleanOutputBuffering(); + $currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1)); - $templating = $this->container->get('templating'); $code = $exception->getStatusCode(); - return $templating->renderResponse( - $this->findTemplate($templating, $format, $code, $this->container->get('kernel')->isDebug()), + return new Response($this->twig->render( + $this->findTemplate($request, $format, $code, $this->debug), array( 'status_code' => $code, 'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '', @@ -54,19 +62,17 @@ class ExceptionController extends ContainerAware 'logger' => $logger, 'currentContent' => $currentContent, ) - ); + )); } /** * @return string */ - protected function getAndCleanOutputBuffering() + protected function getAndCleanOutputBuffering($startObLevel) { // ob_get_level() never returns 0 on some Windows configurations, so if // the level is the same two times in a row, the loop should be stopped. $previousObLevel = null; - $startObLevel = $this->container->get('request')->headers->get('X-Php-Ob-Level', -1); - $currentContent = ''; while (($obLevel = ob_get_level()) > $startObLevel && $obLevel !== $previousObLevel) { @@ -78,14 +84,14 @@ class ExceptionController extends ContainerAware } /** - * @param EngineInterface $templating - * @param string $format - * @param integer $code An HTTP response status code - * @param Boolean $debug + * @param Request $request + * @param string $format + * @param integer $code An HTTP response status code + * @param Boolean $debug * * @return TemplateReference */ - protected function findTemplate($templating, $format, $code, $debug) + protected function findTemplate(Request $request, $format, $code, $debug) { $name = $debug ? 'exception' : 'error'; if ($debug && 'html' == $format) { @@ -95,19 +101,19 @@ class ExceptionController extends ContainerAware // 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 ($templating->exists($template)) { + if ($this->twig->getLoader()->exists($template)) { return $template; } } // try to find a template for the given format $template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig'); - if ($templating->exists($template)) { + if ($this->twig->getLoader()->exists($template)) { return $template; } // default to a generic HTML exception - $this->container->get('request')->setRequestFormat('html'); + $request->setRequestFormat('html'); return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig'); } diff --git a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php index 3b3c8ed6e4..c327037e2b 100644 --- a/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php @@ -34,7 +34,7 @@ class Configuration implements ConfigurationInterface $rootNode ->children() - ->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction')->end() + ->scalarNode('exception_controller')->defaultValue('twig.controller.exception:showAction')->end() ->end() ; diff --git a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml index d54035b425..5b62b01c24 100644 --- a/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml +++ b/src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml @@ -21,6 +21,7 @@ Symfony\Bridge\Twig\Form\TwigRenderer Symfony\Bridge\Twig\Translation\TwigExtractor Symfony\Component\HttpKernel\EventListener\ExceptionListener + Symfony\Bundle\TwigBundle\Controller\ExceptionController @@ -111,5 +112,10 @@ %twig.exception_listener.controller% + + + + %kernel.debug% + diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php index f646d50a4c..0ff7492b7c 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php @@ -12,27 +12,37 @@ namespace Symfony\Bundle\WebProfilerBundle\Controller; use Symfony\Component\HttpKernel\Exception\FlattenException; -use Symfony\Component\HttpKernel\Log\DebugLoggerInterface; use Symfony\Component\HttpFoundation\Response; -use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController; /** * ExceptionController. * * @author Fabien Potencier */ -class ExceptionController extends BaseExceptionController +class ExceptionController { - /** - * {@inheritdoc} - */ - public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html') + protected $twig; + protected $debug; + + public function __construct(\Twig_Environment $twig, $debug) + { + $this->twig = $twig; + $this->debug = $debug; + } + + /** + * Converts an Exception to a Response. + * + * @param FlattenException $exception A FlattenException instance + * + * @return Response + */ + public function showAction(FlattenException $exception) { - $template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error'; $code = $exception->getStatusCode(); - return $this->container->get('templating')->renderResponse( - 'TwigBundle:Exception:'.$template.'.html.twig', + return new Response($this->twig->render( + '@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig', array( 'status_code' => $code, 'status_text' => Response::$statusTexts[$code], @@ -40,6 +50,6 @@ class ExceptionController extends BaseExceptionController 'logger' => null, 'currentContent' => '', ) - ); + )); } } diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml index c06f0dbd21..02aa6bd3e3 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml @@ -7,6 +7,7 @@ Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController Symfony\Bundle\WebProfilerBundle\Controller\RouterController + Symfony\Bundle\WebProfilerBundle\Controller\ExceptionController @@ -23,5 +24,10 @@ + + + + %kernel.debug% + diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig index 6dbfef293c..b498188be5 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig @@ -28,7 +28,7 @@

{% else %}
- {% render 'WebProfilerBundle:Exception:show' with { 'exception': collector.exception, 'format': 'html' } %} + {% render 'web_profiler.controller.exception:showAction' with { 'exception': collector.exception, 'format': 'html' } %}
{% endif %} {% endblock %}