[WebProfilerBundle] decoupled the bundle from TwigBundle

This commit is contained in:
Fabien Potencier 2012-12-13 16:01:47 +01:00
parent 35d63df044
commit f005649315
3 changed files with 52 additions and 8 deletions

View File

@ -12,6 +12,8 @@
namespace Symfony\Bundle\WebProfilerBundle\Controller; namespace Symfony\Bundle\WebProfilerBundle\Controller;
use Symfony\Component\HttpKernel\Exception\FlattenException; use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\Debug\ExceptionHandler;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
/** /**
@ -23,26 +25,39 @@ class ExceptionController
{ {
protected $twig; protected $twig;
protected $debug; protected $debug;
protected $profiler;
public function __construct(\Twig_Environment $twig, $debug) public function __construct(Profiler $profiler, \Twig_Environment $twig, $debug)
{ {
$this->profiler = $profiler;
$this->twig = $twig; $this->twig = $twig;
$this->debug = $debug; $this->debug = $debug;
} }
/** /**
* Converts an Exception to a Response. * Renders the exception panel for the given token.
* *
* @param FlattenException $exception A FlattenException instance * @param string $token The profiler token
* *
* @return Response * @return Response A Response instance
*/ */
public function showAction(FlattenException $exception) public function showAction($token)
{ {
$this->profiler->disable();
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
$template = $this->getTemplate();
if (!$this->twig->getLoader()->exists($template)) {
$handler = new ExceptionHandler();
return new Response($handler->getContent($exception));
}
$code = $exception->getStatusCode(); $code = $exception->getStatusCode();
return new Response($this->twig->render( return new Response($this->twig->render(
'@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig', $template,
array( array(
'status_code' => $code, 'status_code' => $code,
'status_text' => Response::$statusTexts[$code], 'status_text' => Response::$statusTexts[$code],
@ -52,4 +67,32 @@ class ExceptionController
) )
)); ));
} }
/**
* Renders the exception panel stylesheet for the given token.
*
* @param string $token The profiler token
*
* @return Response A Response instance
*/
public function cssAction($token)
{
$this->profiler->disable();
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
$template = $this->getTemplate();
if (!$this->twig->getLoader()->exists($template)) {
$handler = new ExceptionHandler();
return new Response($handler->getStylesheet($exception));
}
return new Response($this->twig->render('@WebProfiler/Collector/exception.css.twig'));
}
protected function getTemplate()
{
return '@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig';
}
} }

View File

@ -26,6 +26,7 @@
</service> </service>
<service id="web_profiler.controller.exception" class="%web_profiler.controller.exception.class%"> <service id="web_profiler.controller.exception" class="%web_profiler.controller.exception.class%">
<argument type="service" id="profiler" />
<argument type="service" id="twig" /> <argument type="service" id="twig" />
<argument>%kernel.debug%</argument> <argument>%kernel.debug%</argument>
</service> </service>

View File

@ -2,7 +2,7 @@
{% block head %} {% block head %}
<style type="text/css"> <style type="text/css">
{% include '@WebProfiler/Collector/exception.css.twig' %} {% render 'web_profiler.controller.exception:cssAction' with { 'token': token } %}
</style> </style>
{{ parent() }} {{ parent() }}
{% endblock %} {% endblock %}
@ -28,7 +28,7 @@
</p> </p>
{% else %} {% else %}
<div class="sf-reset"> <div class="sf-reset">
{% render 'web_profiler.controller.exception:showAction' with { 'exception': collector.exception, 'format': 'html' } %} {% render 'web_profiler.controller.exception:showAction' with { 'token': token } %}
</div> </div>
{% endif %} {% endif %}
{% endblock %} {% endblock %}