Skip to content

Commit 35d63df

Browse files
committed
removed the dependency on the container for exception handling
1 parent 2f4ac5e commit 35d63df

File tree

7 files changed

+60
-31
lines changed

7 files changed

+60
-31
lines changed

src/Symfony/Bundle/TwigBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
2.2.0
55
-----
66

7+
* moved the exception controller to be a service (`twig.controller.exception:showAction` vs `Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction`)
78
* added support for multiple loaders via the "twig.loader" tag.
89
* added automatic registration of namespaced paths for registered bundles
910
* added support for namespaced paths

src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,27 @@
1313

1414
use Symfony\Bundle\FrameworkBundle\Templating\EngineInterface;
1515
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
16-
use Symfony\Component\DependencyInjection\ContainerAware;
1716
use Symfony\Component\HttpKernel\Exception\FlattenException;
1817
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
18+
use Symfony\Component\HttpFoundation\Request;
1919
use Symfony\Component\HttpFoundation\Response;
2020

2121
/**
2222
* ExceptionController.
2323
*
2424
* @author Fabien Potencier <fabien@symfony.com>
2525
*/
26-
class ExceptionController extends ContainerAware
26+
class ExceptionController
2727
{
28+
protected $twig;
29+
protected $debug;
30+
31+
public function __construct(\Twig_Environment $twig, $debug)
32+
{
33+
$this->twig = $twig;
34+
$this->debug = $debug;
35+
}
36+
2837
/**
2938
* Converts an Exception to a Response.
3039
*
@@ -36,37 +45,34 @@ class ExceptionController extends ContainerAware
3645
*
3746
* @throws \InvalidArgumentException When the exception template does not exist
3847
*/
39-
public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
48+
public function showAction(Request $request, FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
4049
{
41-
$this->container->get('request')->setRequestFormat($format);
50+
$request->setRequestFormat($format);
4251

43-
$currentContent = $this->getAndCleanOutputBuffering();
52+
$currentContent = $this->getAndCleanOutputBuffering($request->headers->get('X-Php-Ob-Level', -1));
4453

45-
$templating = $this->container->get('templating');
4654
$code = $exception->getStatusCode();
4755

48-
return $templating->renderResponse(
49-
$this->findTemplate($templating, $format, $code, $this->container->get('kernel')->isDebug()),
56+
return new Response($this->twig->render(
57+
$this->findTemplate($request, $format, $code, $this->debug),
5058
array(
5159
'status_code' => $code,
5260
'status_text' => isset(Response::$statusTexts[$code]) ? Response::$statusTexts[$code] : '',
5361
'exception' => $exception,
5462
'logger' => $logger,
5563
'currentContent' => $currentContent,
5664
)
57-
);
65+
));
5866
}
5967

6068
/**
6169
* @return string
6270
*/
63-
protected function getAndCleanOutputBuffering()
71+
protected function getAndCleanOutputBuffering($startObLevel)
6472
{
6573
// ob_get_level() never returns 0 on some Windows configurations, so if
6674
// the level is the same two times in a row, the loop should be stopped.
6775
$previousObLevel = null;
68-
$startObLevel = $this->container->get('request')->headers->get('X-Php-Ob-Level', -1);
69-
7076
$currentContent = '';
7177

7278
while (($obLevel = ob_get_level()) > $startObLevel && $obLevel !== $previousObLevel) {
@@ -78,14 +84,14 @@ protected function getAndCleanOutputBuffering()
7884
}
7985

8086
/**
81-
* @param EngineInterface $templating
82-
* @param string $format
83-
* @param integer $code An HTTP response status code
84-
* @param Boolean $debug
87+
* @param Request $request
88+
* @param string $format
89+
* @param integer $code An HTTP response status code
90+
* @param Boolean $debug
8591
*
8692
* @return TemplateReference
8793
*/
88-
protected function findTemplate($templating, $format, $code, $debug)
94+
protected function findTemplate(Request $request, $format, $code, $debug)
8995
{
9096
$name = $debug ? 'exception' : 'error';
9197
if ($debug && 'html' == $format) {
@@ -95,19 +101,19 @@ protected function findTemplate($templating, $format, $code, $debug)
95101
// when not in debug, try to find a template for the specific HTTP status code and format
96102
if (!$debug) {
97103
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig');
98-
if ($templating->exists($template)) {
104+
if ($this->twig->getLoader()->exists($template)) {
99105
return $template;
100106
}
101107
}
102108

103109
// try to find a template for the given format
104110
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
105-
if ($templating->exists($template)) {
111+
if ($this->twig->getLoader()->exists($template)) {
106112
return $template;
107113
}
108114

109115
// default to a generic HTML exception
110-
$this->container->get('request')->setRequestFormat('html');
116+
$request->setRequestFormat('html');
111117

112118
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig');
113119
}

src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function getConfigTreeBuilder()
3434

3535
$rootNode
3636
->children()
37-
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction')->end()
37+
->scalarNode('exception_controller')->defaultValue('twig.controller.exception:showAction')->end()
3838
->end()
3939
;
4040

src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<parameter key="twig.form.renderer.class">Symfony\Bridge\Twig\Form\TwigRenderer</parameter>
2222
<parameter key="twig.translation.extractor.class">Symfony\Bridge\Twig\Translation\TwigExtractor</parameter>
2323
<parameter key="twig.exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
24+
<parameter key="twig.controller.exception.class">Symfony\Bundle\TwigBundle\Controller\ExceptionController</parameter>
2425
</parameters>
2526

2627
<services>
@@ -111,5 +112,10 @@
111112
<argument>%twig.exception_listener.controller%</argument>
112113
<argument type="service" id="logger" on-invalid="null" />
113114
</service>
115+
116+
<service id="twig.controller.exception" class="%twig.controller.exception.class%">
117+
<argument type="service" id="twig" />
118+
<argument>%kernel.debug%</argument>
119+
</service>
114120
</services>
115121
</container>

src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,44 @@
1212
namespace Symfony\Bundle\WebProfilerBundle\Controller;
1313

1414
use Symfony\Component\HttpKernel\Exception\FlattenException;
15-
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1615
use Symfony\Component\HttpFoundation\Response;
17-
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
1816

1917
/**
2018
* ExceptionController.
2119
*
2220
* @author Fabien Potencier <fabien@symfony.com>
2321
*/
24-
class ExceptionController extends BaseExceptionController
22+
class ExceptionController
2523
{
24+
protected $twig;
25+
protected $debug;
26+
27+
public function __construct(\Twig_Environment $twig, $debug)
28+
{
29+
$this->twig = $twig;
30+
$this->debug = $debug;
31+
}
32+
2633
/**
27-
* {@inheritdoc}
34+
* Converts an Exception to a Response.
35+
*
36+
* @param FlattenException $exception A FlattenException instance
37+
*
38+
* @return Response
2839
*/
29-
public function showAction(FlattenException $exception, DebugLoggerInterface $logger = null, $format = 'html')
40+
public function showAction(FlattenException $exception)
3041
{
31-
$template = $this->container->get('kernel')->isDebug() ? 'exception' : 'error';
3242
$code = $exception->getStatusCode();
3343

34-
return $this->container->get('templating')->renderResponse(
35-
'TwigBundle:Exception:'.$template.'.html.twig',
44+
return new Response($this->twig->render(
45+
'@Twig/Exception/'.($this->debug ? 'exception' : 'error').'.html.twig',
3646
array(
3747
'status_code' => $code,
3848
'status_text' => Response::$statusTexts[$code],
3949
'exception' => $exception,
4050
'logger' => null,
4151
'currentContent' => '',
4252
)
43-
);
53+
));
4454
}
4555
}

src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<parameters>
88
<parameter key="web_profiler.controller.profiler.class">Symfony\Bundle\WebProfilerBundle\Controller\ProfilerController</parameter>
99
<parameter key="web_profiler.controller.router.class">Symfony\Bundle\WebProfilerBundle\Controller\RouterController</parameter>
10+
<parameter key="web_profiler.controller.exception.class">Symfony\Bundle\WebProfilerBundle\Controller\ExceptionController</parameter>
1011
</parameters>
1112

1213
<services>
@@ -23,5 +24,10 @@
2324
<argument type="service" id="twig" />
2425
<argument type="service" id="router" on-invalid="null" />
2526
</service>
27+
28+
<service id="web_profiler.controller.exception" class="%web_profiler.controller.exception.class%">
29+
<argument type="service" id="twig" />
30+
<argument>%kernel.debug%</argument>
31+
</service>
2632
</services>
2733
</container>

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Collector/exception.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
</p>
2929
{% else %}
3030
<div class="sf-reset">
31-
{% render 'WebProfilerBundle:Exception:show' with { 'exception': collector.exception, 'format': 'html' } %}
31+
{% render 'web_profiler.controller.exception:showAction' with { 'exception': collector.exception, 'format': 'html' } %}
3232
</div>
3333
{% endif %}
3434
{% endblock %}

0 commit comments

Comments
 (0)