8000 moved the Exception listener from FrameworkBundle to TwigBundle as it… · symfony/symfony@3749ad4 · GitHub
[go: up one dir, main page]

Skip to content

Commit 3749ad4

Browse files
committed
moved the Exception listener from FrameworkBundle to TwigBundle as it relies on Twig being enabled
This commit also fixes exception pages when Twig is not enabled as a templating engine. Instead of just displaying the raw Twig template as before, we now fallback to the default exception handler introduced some time ago.
1 parent 08730b6 commit 3749ad4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+82
-37
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ public function getConfigTreeBuilder()
4949
->scalarNode('charset')->end()
5050
->scalarNode('trust_proxy_headers')->defaultFalse()->end()
5151
->scalarNode('secret')->isRequired()->end()
52-
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\FrameworkBundle\\Controller\\ExceptionController::showAction')->end()
5352
->scalarNode('ide')->defaultNull()->end()
5453
->booleanNode('test')->end()
5554
->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public function load(array $configs, ContainerBuilder $container)
6262
$container->setParameter('kernel.charset', $config['charset']);
6363
}
6464
$container->setParameter('kernel.secret', $config['secret']);
65-
$container->setParameter('exception_listener.controller', $config['exception_controller']);
6665

6766
$container->setParameter('kernel.trust_proxy_headers', $config['trust_proxy_headers']);
6867

src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function build(ContainerBuilder $container)
5050

5151
$container->addCompilerPass(new RoutingResolverPass());
5252
$container->addCompilerPass(new ProfilerPass());
53-
$container->addCompilerPass(new RegisterKernelListenersPass());
53+
$container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
5454
$container->addCompilerPass(new TemplatingPass());
5555
$container->addCompilerPass(new AddConstraintValidatorsPass());
5656
$container->addCompilerPass(new AddValidatorInitializersPass());

src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
<parameter key="controller_resolver.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver</parameter>
1111
<parameter key="controller_name_converter.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser</parameter>
1212
<parameter key="response_listener.class">Symfony\Component\HttpKernel\EventListener\ResponseListener</parameter>
13-
<parameter key="exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
1413
</parameters>
1514

1615
<services>
@@ -45,12 +44,5 @@
4544
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
4645
<argument>%kernel.charset%</argument>
4746
</service>
48-
49-
<service id="exception_listener" class="%exception_listener.class%">
50-
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-128" />
51-
<tag name="monolog.logger" channel="request" />
52-
<argument>%exception_listener.controller%</argument>
53-
<argument type="service" id="logger" on-invalid="null" />
54-
</service>
5547
</services>
5648
</container>

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.twig

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.twig

Lines changed: 0 additions & 3 deletions
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.rdf.twig

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php renamed to src/Symfony/Bundle/TwigBundle/Controller/ExceptionController.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Bundle\FrameworkBundle\Controller;
12+
namespace Symfony\Bundle\TwigBundle\Controller;
1313

1414
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
1515
use Symfony\Component\DependencyInjection\ContainerAware;
@@ -75,21 +75,21 @@ protected function findTemplate($templating, $format, $code, $debug)
7575

7676
// when not in debug, try to find a template for the specific HTTP status code and format
7777
if (!$debug) {
78-
$template = new TemplateReference('FrameworkBundle', 'Exception', $name.$code, $format, 'twig');
78+
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig');
7979
if ($templating->exists($template)) {
8080
return $template;
8181
}
8282
}
8383

8484
// try to find a template for the given format
85-
$template = new TemplateReference('FrameworkBundle', 'Exception', $name, $format, 'twig');
85+
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
8686
if ($templating->exists($template)) {
8787
return $template;
8888
}
8989

9090
// default to a generic HTML exception
9191
$this->container->get('request')->setRequestFormat('html');
9292

93-
return new TemplateReference('FrameworkBundle', 'Exception', $name, 'html', 'twig');
93+
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig');
9494
}
9595
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
13+
14+
use Symfony\Component\DependencyInjection\Reference;
15+
use Symfony\Component\DependencyInjection\ContainerBuilder;
16+
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
17+
18+
/**
19+
* Registers the Twig exception listener if Twig is registered as a templating engine.
20+
*
21+
* @author Fabien Potencier <fabien@symfony.com>
22+
*/
23+
class ExceptionListenerPass implements CompilerPassInterface
24+
{
25+
public function process(ContainerBuilder $container)
26+
{
27+
if (false === $container->hasDefinition('twig')) {
28+
return;
29+
}
30+
31+
// register the exception controller only if Twig is enabled
32+
$engines = $container->getParameter('templating.engines');
33+
if (!in_array('twig', $engines)) {
34+
$container->removeDefinition('twig.exception_listener');
35+
}
36+
}
37+
}

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ public function getConfigTreeBuilder()
3232
$treeBuilder = new TreeBuilder();
3333
$rootNode = $treeBuilder->root('twig');
3434

35+
$rootNode
36+
->children()
37+
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction')->end()
38+
->end()
39+
;
40+
3541
$this->addFormSection($rootNode);
3642
$this->addGlobalsSection($rootNode);
3743
$this->addTwigOptions($rootNode);

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public function load(array $configs, ContainerBuilder $container)
3939
$configuration = new Configuration();
4040
$config = $this->processConfiguration($configuration, $configs);
4141

42+
$container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
43+
4244
$container->setParameter('twig.form.resources', $config['form']['resources']);
4345
$container->getDefinition('twig.loader')->addMethodCall('addPath', array(__DIR__.'/../../../Bridge/Twig/Resources/views/Form'));
4446

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<parameter key="twig.extension.routing.class">Symfony\Bridge\Twig\Extension\RoutingExtension</parameter>
1717
<parameter key="twig.extension.yaml.class">Symfony\Bridge\Twig\Extension\YamlExtension</parameter>
1818
<parameter key="twig.extension.form.class">Symfony\Bridge\Twig\Extension\FormExtension</parameter>
19+
<parameter key="twig.exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
1920
</parameters>
2021

2122
<services>
@@ -74,5 +75,12 @@
7475
<tag name="twig.extension" />
7576
<argument>%twig.form.resources%</argument>
7677
</service>
78+
79+
<service id="twig.exception_listener" class="%twig.exception_listener.class%">
80+
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-128" />
81+
<tag name="monolog.logger" channel="request" />
82+
<argument>%twig.exception_listener.controller%</argument>
83+
<argument type="service" id="logger" on-invalid="null" />
84+
</service>
7785
</services>
7886
</container>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include 'TwigBundle:Exception:error.xml.twig' with { 'exception': exception } %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include 'TwigBundle:Exception:error.xml.twig' with { 'exception': exception } %}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include 'TwigBundle:Exception:exception.xml.twig' with { 'exception': exception } %}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
{% include 'TwigBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
3+
*/

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.html.twig renamed to src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.html.twig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
</div>
4242

4343
{% for position, e in exception.toarray %}
44-
{% include 'FrameworkBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
44+
{% include 'TwigBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
4545
{% endfor %}
4646

4747
{% if logger %}
@@ -68,7 +68,7 @@
6868
</div>
6969

7070
<div id="logs">
71-
{% include 'FrameworkBundle:Exception:logs.html.twig' with { 'logs': logger.logs } only %}
71+
{% include 'TwigBundle:Exception:logs.html.twig' with { 'logs': logger.logs } only %}
7272
</div>
7373

7474
</div>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/*
2+
{% include 'TwigBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
3+
*/
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{% include 'TwigBundle:Exception:exception.xml.twig' with { 'exception': exception } %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.twig renamed to src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.txt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
[message] {{ exception.message }}
33
{% for i, e in exception.toarray %}
44
[{{ i + 1 }}] {{ e.class }}: {{ e.message }}
5-
{% include 'FrameworkBundle:Exception:traces.txt.twig' with { 'exception': e } only %}
5+
{% include 'TwigBundle:Exception:traces.txt.twig' with { 'exception': e } only %}
66

77
{% endfor %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.twig renamed to src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.xml.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<error code="{{ status_code }}" message="{{ status_text }}">
44
{% for e in exception.toarray %}
55
<exception class="{{ e.class }}" message="{{ e.message }}">
6-
{% include 'FrameworkBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
6+
{% include 'TwigBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
77
</exception>
88
{% endfor %}
99
</error>
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
{% extends 'FrameworkBundle::layout.html.twig' %}
1+
{% extends 'TwigBundle::layout.html.twig' %}
22

33
{% block title %}
44
{{ exception.message }} ({{ status_code }} {{ status_text }})
55
{% endblock %}
66

77
{% block body %}
8-
{% include 'FrameworkBundle:Exception:exception.html.twig' %}
8+
{% include 'TwigBundle:Exception:exception.html.twig' %}
99
{% endblock %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.html.twig renamed to src/Symfony/Bundle/TwigBundle/Resources/views/Exception/traces.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<ol class="traces list_exception" id="traces_{{ position }}" style="display: {{ 0 == count ? 'block' : 'none' }}">
1919
{% for i, trace in exception.trace %}
2020
<li>
21-
{% include 'FrameworkBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
21+
{% include 'TwigBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
2222
</li>
2323
{% endfor %}
2424
</ol>
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% if exception.trace|length %}
22
{% for trace in exception.trace %}
3-
{% include 'FrameworkBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
3+
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
44

55
{% endfor %}
66
{% endif %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.xml.twig renamed to src/Symfony/Bundle/TwigBundle/Resources/views/Exception/traces.xml.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<traces>
22
{% for trace in exception.trace %}
33
<trace>
4-
{% include 'FrameworkBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
4+
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
55

66
</trace>
77
{% endfor %}

src/Symfony/Bundle/TwigBundle/TwigBundle.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\HttpKernel\Bundle\Bundle;
1515
use Symfony\Component\DependencyInjection\ContainerBuilder;
1616
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
17+
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;
1718

1819
/**
1920
* Bundle.
@@ -27,5 +28,6 @@ public function build(ContainerBuilder $container)
2728
parent::build($container);
2829

2930
$container->addCompilerPass(new TwigEnvironmentPass());
31+
$container->addCompilerPass(new ExceptionListenerPass());
3032
}
3133
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Symfony\Component\HttpKernel\Exception\FlattenException;
1616
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
1717
use Symfony\Component\HttpFoundation\Response;
18-
use Symfony\Bundle\FrameworkBundle\Controller\ExceptionController as BaseExceptionController;
18+
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
1919

2020
/**
2121
* ExceptionController.
@@ -33,7 +33,7 @@ public function showAction(FlattenException $exception, DebugLoggerInterface $lo
3333
$code = $exception->getStatusCode();
3434

3535
return $this->container->get('templating')->renderResponse(
36-
'FrameworkBundle:Exception:'.$template.'.html.twig',
36+
'TwigBundle:Exception:'.$template.'.html.twig',
3737
array(
3838
'status_code' => $code,
3939
'status_text' => Response::$statusTexts[$code],

src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% extends 'FrameworkBundle::layout.html.twig' %}
1+
{% extends 'TwigBundle::layout.html.twig' %}
22

33
{% block title 'Redirection Intercepted' %}
44

0 commit comments

Comments
 (0)
0