ソースを参照

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.
Fabien Potencier 14 年 前
コミット
3749ad43f4
42 ファイル変更82 行追加37 行削除
  1. 0 1
      src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php
  2. 0 1
      src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php
  3. 1 1
      src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php
  4. 0 8
      src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml
  5. 0 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.twig
  6. 0 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.twig
  7. 0 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.twig
  8. 0 3
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.twig
  9. 0 3
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.twig
  10. 0 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.rdf.twig
  11. 4 4
      src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php
  12. 37 0
      src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php
  13. 6 0
      src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php
  14. 2 0
      src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php
  15. 8 0
      src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml
  16. 1 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.atom.twig
  17. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.css.twig
  18. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.html.twig
  19. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.js.twig
  20. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.json.twig
  21. 1 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.rdf.twig
  22. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.txt.twig
  23. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.xml.twig
  24. 1 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.atom.twig
  25. 3 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.css.twig
  26. 2 2
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.html.twig
  27. 3 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.js.twig
  28. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.json.twig
  29. 1 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.rdf.twig
  30. 1 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.twig
  31. 1 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.twig
  32. 2 2
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception_full.html.twig
  33. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/logs.html.twig
  34. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/trace.html.twig
  35. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/Exception/trace.txt.twig
  36. 1 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.html.twig
  37. 1 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.txt.twig
  38. 1 1
      src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.xml.twig
  39. 0 0
      src/Symfony/Bundle/TwigBundle/Resources/views/layout.html.twig
  40. 2 0
      src/Symfony/Bundle/TwigBundle/TwigBundle.php
  41. 2 2
      src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php
  42. 1 1
      src/Symfony/Bundle/WebProfilerBundle/Resources/views/Profiler/toolbar_redirect.html.twig

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

@@ -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()

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

@@ -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']);
 

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/FrameworkBundle.php

@@ -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());

+ 0 - 8
src/Symfony/Bundle/FrameworkBundle/Resources/config/web.xml

@@ -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>

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.atom.twig

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

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.rdf.twig

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

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.atom.twig

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

+ 0 - 3
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.css.twig

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

+ 0 - 3
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.js.twig

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

+ 0 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.rdf.twig

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

+ 4 - 4
src/Symfony/Bundle/FrameworkBundle/Controller/ExceptionController.php

@@ -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');
     }
 }

+ 37 - 0
src/Symfony/Bundle/TwigBundle/DependencyInjection/Compiler/ExceptionListenerPass.php

@@ -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');
+        }
+    }
+}

+ 6 - 0
src/Symfony/Bundle/TwigBundle/DependencyInjection/Configuration.php

@@ -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);

+ 2 - 0
src/Symfony/Bundle/TwigBundle/DependencyInjection/TwigExtension.php

@@ -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'));
 

+ 8 - 0
src/Symfony/Bundle/TwigBundle/Resources/config/twig.xml

@@ -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>

+ 1 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.atom.twig

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

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.css.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.css.twig


src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.html.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.html.twig


src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.js.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.js.twig


src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.json.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.json.twig


+ 1 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.rdf.twig

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

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.txt.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.txt.twig


src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/error.xml.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/error.xml.twig


+ 1 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.atom.twig

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

+ 3 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.css.twig

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

+ 2 - 2
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.html.twig

@@ -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>

+ 3 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.js.twig

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

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.json.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.json.twig


+ 1 - 0
src/Symfony/Bundle/TwigBundle/Resources/views/Exception/exception.rdf.twig

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

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.txt.twig

@@ -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 %}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception.xml.twig

@@ -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>

+ 2 - 2
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/exception_full.html.twig

@@ -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 %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/logs.html.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/logs.html.twig


src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/trace.html.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/trace.html.twig


src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/trace.txt.twig → src/Symfony/Bundle/TwigBundle/Resources/views/Exception/trace.txt.twig


+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.html.twig

@@ -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>

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.txt.twig

@@ -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 %}

+ 1 - 1
src/Symfony/Bundle/FrameworkBundle/Resources/views/Exception/traces.xml.twig

@@ -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 %}

src/Symfony/Bundle/FrameworkBundle/Resources/views/layout.html.twig → src/Symfony/Bundle/TwigBundle/Resources/views/layout.html.twig


+ 2 - 0
src/Symfony/Bundle/TwigBundle/TwigBundle.php

@@ -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());
     }
 }

+ 2 - 2
src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php

@@ -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],

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

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