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.
This commit is contained in:
Fabien Potencier 2011-07-21 19:19:30 +02:00
parent 08730b604b
commit 3749ad43f4
42 changed files with 82 additions and 37 deletions

View File

@ -49,7 +49,6 @@ class Configuration implements ConfigurationInterface
->scalarNode('charset')->end()
->scalarNode('trust_proxy_headers')->defaultFalse()->end()
->scalarNode('secret')->isRequired()->end()
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\FrameworkBundle\\Controller\\ExceptionController::showAction')->end()
->scalarNode('ide')->defaultNull()->end()
->booleanNode('test')->end()
->end()

View File

@ -62,7 +62,6 @@ class FrameworkExtension extends Extension
$container->setParameter('kernel.charset', $config['charset']);
}
$container->setParameter('kernel.secret', $config['secret']);
$container->setParameter('exception_listener.controller', $config['exception_controller']);
$container->setParameter('kernel.trust_proxy_headers', $config['trust_proxy_headers']);

View File

@ -50,7 +50,7 @@ class FrameworkBundle extends Bundle
$container->addCompilerPass(new RoutingResolverPass());
$container->addCompilerPass(new ProfilerPass());
$container->addCompilerPass(new RegisterKernelListenersPass());
$container->addCompilerPass(new RegisterKernelListenersPass(), PassConfig::TYPE_AFTER_REMOVING);
$container->addCompilerPass(new TemplatingPass());
$container->addCompilerPass(new AddConstraintValidatorsPass());
$container->addCompilerPass(new AddValidatorInitializersPass());

View File

@ -10,7 +10,6 @@
<parameter key="controller_resolver.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerResolver</parameter>
<parameter key="controller_name_converter.class">Symfony\Bundle\FrameworkBundle\Controller\ControllerNameParser</parameter>
<parameter key="response_listener.class">Symfony\Component\HttpKernel\EventListener\ResponseListener</parameter>
<parameter key="exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
</parameters>
<services>
@ -45,12 +44,5 @@
<tag name="kernel.event_listener" event="kernel.response" method="onKernelResponse" />
<argument>%kernel.charset%</argument>
</service>
<service id="exception_listener" class="%exception_listener.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-128" />
<tag name="monolog.logger" channel="request" />
<argument>%exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>

View File

@ -1 +0,0 @@
{% include 'FrameworkBundle:Exception:error.xml.twig' with { 'exception': exception } %}

View File

@ -1 +0,0 @@
{% include 'FrameworkBundle:Exception:error.xml.twig' with { 'exception': exception } %}

View File

@ -1 +0,0 @@
{% include 'FrameworkBundle:Exception:exception.xml.twig' with { 'exception': exception } %}

View File

@ -1,3 +0,0 @@
/*
{% include 'FrameworkBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
*/

View File

@ -1,3 +0,0 @@
/*
{% include 'FrameworkBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
*/

View File

@ -1 +0,0 @@
{% include 'FrameworkBundle:Exception:exception.xml.twig' with { 'exception': exception } %}

View File

@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\FrameworkBundle\Controller;
namespace Symfony\Bundle\TwigBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Templating\TemplateReference;
use Symfony\Component\DependencyInjection\ContainerAware;
@ -75,14 +75,14 @@ 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('FrameworkBundle', 'Exception', $name.$code, $format, 'twig');
$template = new TemplateReference('TwigBundle', 'Exception', $name.$code, $format, 'twig');
if ($templating->exists($template)) {
return $template;
}
}
// try to find a template for the given format
$template = new TemplateReference('FrameworkBundle', 'Exception', $name, $format, 'twig');
$template = new TemplateReference('TwigBundle', 'Exception', $name, $format, 'twig');
if ($templating->exists($template)) {
return $template;
}
@ -90,6 +90,6 @@ class ExceptionController extends ContainerAware
// default to a generic HTML exception
$this->container->get('request')->setRequestFormat('html');
return new TemplateReference('FrameworkBundle', 'Exception', $name, 'html', 'twig');
return new TemplateReference('TwigBundle', 'Exception', $name, 'html', 'twig');
}
}

View File

@ -0,0 +1,37 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bundle\TwigBundle\DependencyInjection\Compiler;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
/**
* Registers the Twig exception listener if Twig is registered as a templating engine.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class ExceptionListenerPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
if (false === $container->hasDefinition('twig')) {
return;
}
// register the exception controller only if Twig is enabled
$engines = $container->getParameter('templating.engines');
if (!in_array('twig', $engines)) {
$container->removeDefinition('twig.exception_listener');
}
}
}

View File

@ -32,6 +32,12 @@ class Configuration implements ConfigurationInterface
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('twig');
$rootNode
->children()
->scalarNode('exception_controller')->defaultValue('Symfony\\Bundle\\TwigBundle\\Controller\\ExceptionController::showAction')->end()
->end()
;
$this->addFormSection($rootNode);
$this->addGlobalsSection($rootNode);
$this->addTwigOptions($rootNode);

View File

@ -39,6 +39,8 @@ class TwigExtension extends Extension
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$container->setParameter('twig.exception_listener.controller', $config['exception_controller']);
$container->setParameter('twig.form.resources', $config['form']['resources']);
$container->getDefinition('twig.loader')->addMethodCall('addPath', array(__DIR__.'/../../../Bridge/Twig/Resources/views/Form'));

View File

@ -16,6 +16,7 @@
<parameter key="twig.extension.routing.class">Symfony\Bridge\Twig\Extension\RoutingExtension</parameter>
<parameter key="twig.extension.yaml.class">Symfony\Bridge\Twig\Extension\YamlExtension</parameter>
<parameter key="twig.extension.form.class">Symfony\Bridge\Twig\Extension\FormExtension</parameter>
<parameter key="twig.exception_listener.class">Symfony\Component\HttpKernel\EventListener\ExceptionListener</parameter>
</parameters>
<services>
@ -74,5 +75,12 @@
<tag name="twig.extension" />
<argument>%twig.form.resources%</argument>
</service>
<service id="twig.exception_listener" class="%twig.exception_listener.class%">
<tag name="kernel.event_listener" event="kernel.exception" method="onKernelException" priority="-128" />
<tag name="monolog.logger" channel="request" />
<argument>%twig.exception_listener.controller%</argument>
<argument type="service" id="logger" on-invalid="null" />
</service>
</services>
</container>

View File

@ -0,0 +1 @@
{% include 'TwigBundle:Exception:error.xml.twig' with { 'exception': exception } %}

View File

@ -0,0 +1 @@
{% include 'TwigBundle:Exception:error.xml.twig' with { 'exception': exception } %}

View File

@ -0,0 +1 @@
{% include 'TwigBundle:Exception:exception.xml.twig' with { 'exception': exception } %}

View File

@ -0,0 +1,3 @@
/*
{% include 'TwigBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
*/

View File

@ -41,7 +41,7 @@
</div>
{% for position, e in exception.toarray %}
{% include 'FrameworkBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
{% include 'TwigBundle:Exception:traces.html.twig' with { 'exception': e, 'position': position, 'count': previous_count } only %}
{% endfor %}
{% if logger %}
@ -68,7 +68,7 @@
</div>
<div id="logs">
{% include 'FrameworkBundle:Exception:logs.html.twig' with { 'logs': logger.logs } only %}
{% include 'TwigBundle:Exception:logs.html.twig' with { 'logs': logger.logs } only %}
</div>
</div>

View File

@ -0,0 +1,3 @@
/*
{% include 'TwigBundle:Exception:exception.txt.twig' with { 'exception': exception } %}
*/

View File

@ -0,0 +1 @@
{% include 'TwigBundle:Exception:exception.xml.twig' with { 'exception': exception } %}

View File

@ -2,6 +2,6 @@
[message] {{ exception.message }}
{% for i, e in exception.toarray %}
[{{ i + 1 }}] {{ e.class }}: {{ e.message }}
{% include 'FrameworkBundle:Exception:traces.txt.twig' with { 'exception': e } only %}
{% include 'TwigBundle:Exception:traces.txt.twig' with { 'exception': e } only %}
{% endfor %}

View File

@ -3,7 +3,7 @@
<error code="{{ status_code }}" message="{{ status_text }}">
{% for e in exception.toarray %}
<exception class="{{ e.class }}" message="{{ e.message }}">
{% include 'FrameworkBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
{% include 'TwigBundle:Exception:traces.xml.twig' with { 'exception': e } only %}
</exception>
{% endfor %}
</error>

View File

@ -1,9 +1,9 @@
{% extends 'FrameworkBundle::layout.html.twig' %}
{% extends 'TwigBundle::layout.html.twig' %}
{% block title %}
{{ exception.message }} ({{ status_code }} {{ status_text }})
{% endblock %}
{% block body %}
{% include 'FrameworkBundle:Exception:exception.html.twig' %}
{% include 'TwigBundle:Exception:exception.html.twig' %}
{% endblock %}

View File

@ -18,7 +18,7 @@
<ol class="traces list_exception" id="traces_{{ position }}" style="display: {{ 0 == count ? 'block' : 'none' }}">
{% for i, trace in exception.trace %}
<li>
{% include 'FrameworkBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.html.twig' with { 'prefix': position, 'i': i, 'trace': trace } only %}
</li>
{% endfor %}
</ol>

View File

@ -1,6 +1,6 @@
{% if exception.trace|length %}
{% for trace in exception.trace %}
{% include 'FrameworkBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% endfor %}
{% endif %}

View File

@ -1,7 +1,7 @@
<traces>
{% for trace in exception.trace %}
<trace>
{% include 'FrameworkBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
{% include 'TwigBundle:Exception:trace.txt.twig' with { 'trace': trace } only %}
</trace>
{% endfor %}

View File

@ -14,6 +14,7 @@ namespace Symfony\Bundle\TwigBundle;
use Symfony\Component\HttpKernel\Bundle\Bundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\TwigEnvironmentPass;
use Symfony\Bundle\TwigBundle\DependencyInjection\Compiler\ExceptionListenerPass;
/**
* Bundle.
@ -27,5 +28,6 @@ class TwigBundle extends Bundle
parent::build($container);
$container->addCompilerPass(new TwigEnvironmentPass());
$container->addCompilerPass(new ExceptionListenerPass());
}
}

View File

@ -15,7 +15,7 @@ use Symfony\Component\DependencyInjection\ContainerAware;
use Symfony\Component\HttpKernel\Exception\FlattenException;
use Symfony\Component\HttpKernel\Log\DebugLoggerInterface;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Bundle\FrameworkBundle\Controller\ExceptionController as BaseExceptionController;
use Symfony\Bundle\TwigBundle\Controller\ExceptionController as BaseExceptionController;
/**
* ExceptionController.
@ -33,7 +33,7 @@ class ExceptionController extends BaseExceptionController
$code = $exception->getStatusCode();
return $this->container->get('templating')->renderResponse(
'FrameworkBundle:Exception:'.$template.'.html.twig',
'TwigBundle:Exception:'.$template.'.html.twig',
array(
'status_code' => $code,
'status_text' => Response::$statusTexts[$code],

View File

@ -1,4 +1,4 @@
{% extends 'FrameworkBundle::layout.html.twig' %}
{% extends 'TwigBundle::layout.html.twig' %}
{% block title 'Redirection Intercepted' %}