From a19c336c180ab05f309bee0630a70f68d48e1183 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Sun, 26 Jun 2011 17:59:43 +0200 Subject: [PATCH] [WebProfilerBundle] fixed the profiler when the WDT is disabled --- .../Controller/ProfilerController.php | 2 +- .../WebProfilerExtension.php | 17 ++++++++++---- .../EventListener/WebDebugToolbarListener.php | 23 ++++++++++++++----- .../Resources/config/toolbar.xml | 2 +- .../WebProfilerExtensionTest.php | 17 +++++++------- 5 files changed, 40 insertions(+), 21 deletions(-) diff --git a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php index 9a8f0e0420..b06e54eafb 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php @@ -156,7 +156,7 @@ class ProfilerController extends ContainerAware 'profile' => $profile, 'templates' => $this->getTemplates($profiler), 'profiler_url' => $url, - 'verbose' => $this->container->get('web_profiler.debug_toolbar')->getVerbose() + 'verbose' => $this->container->get('web_profiler.debug_toolbar')->isVerbose() )); } diff --git a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php index 397e3f8927..2d93ab5091 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php +++ b/src/Symfony/Bundle/WebProfilerBundle/DependencyInjection/WebProfilerExtension.php @@ -16,6 +16,7 @@ use Symfony\Component\DependencyInjection\Loader\XmlFileLoader; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Config\FileLocator; use Symfony\Component\Config\Definition\Processor; +use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener; /** * WebProfilerExtension. @@ -43,13 +44,19 @@ class WebProfilerExtension extends Extension $configuration = new Configuration(); $config = $processor->processConfiguration($configuration, $configs); - if ($config['toolbar']) { - $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); - $loader->load('toolbar.xml'); + $loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); + $loader->load('toolbar.xml'); - $container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']); - $container->setParameter('web_profiler.debug_toolbar.verbose', $config['verbose']); + $container->setParameter('web_profiler.debug_toolbar.intercept_redirects', $config['intercept_redirects']); + + if (!$config['toolbar']) { + $mode = WebDebugToolbarListener::DISABLED; + } elseif ($config['verbose']) { + $mode = WebDebugToolbarListener::ENABLED; + } else { + $mode = WebDebugToolbarListener::ENABLED_MINIMAL; } + $container->setParameter('web_profiler.debug_toolbar.mode', $mode); } /** diff --git a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php index 80429832c1..26f9d60554 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php +++ b/src/Symfony/Bundle/WebProfilerBundle/EventListener/WebDebugToolbarListener.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\HttpKernelInterface; use Symfony\Component\HttpKernel\Event\FilterResponseEvent; use Symfony\Bundle\TwigBundle\TwigEngine; +use Symfony\Bundle\WebProfilerBundle\EventListener\WebDebugToolbarListener; /** * WebDebugToolbarListener injects the Web Debug Toolbar. @@ -29,20 +30,29 @@ use Symfony\Bundle\TwigBundle\TwigEngine; */ class WebDebugToolbarListener { + const DISABLED = 1; + const ENABLED = 2; + const ENABLED_MINIMAL = 3; + protected $templating; protected $interceptRedirects; - protected $verbose; + protected $mode; - public function __construct(TwigEngine $templating, $interceptRedirects = false, $verbose = true) + public function __construct(TwigEngine $templating, $interceptRedirects = false, $mode = self::ENABLED) { $this->templating = $templating; $this->interceptRedirects = (Boolean) $interceptRedirects; - $this->verbose = (Boolean) $verbose; + $this->mode = (integer) $mode; } - public function getVerbose() + public function isVerbose() { - return $this->verbose; + return self::ENABLED === $this->mode; + } + + public function isEnabled() + { + return self::DISABLED !== $this->mode; } public function onKernelResponse(FilterResponseEvent $event) @@ -70,7 +80,8 @@ class WebDebugToolbarListener $response->headers->remove('Location'); } - if (!$response->headers->has('X-Debug-Token') + if (self::DISABLED === $this->mode + || !$response->headers->has('X-Debug-Token') || '3' === substr($response->getStatusCode(), 0, 1) || ($response->headers->has('Content-Type') && false === strpos($response->headers->get('Content-Type'), 'html')) || 'html' !== $request->getRequestFormat() diff --git a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml index 874008a4b9..78a2a8d4f6 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml +++ b/src/Symfony/Bundle/WebProfilerBundle/Resources/config/toolbar.xml @@ -13,7 +13,7 @@ %web_profiler.debug_toolbar.intercept_redirects% - %web_profiler.debug_toolbar.verbose% + %web_profiler.debug_toolbar.mode% diff --git a/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php b/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php index 6688152d1d..6ae8012f09 100644 --- a/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php +++ b/src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php @@ -91,7 +91,7 @@ class WebProfilerExtensionTest extends TestCase $extension = new WebProfilerExtension(); $extension->load(array(array()), $this->container); - $this->assertFalse($this->container->has('web_profiler.debug_toolbar')); + $this->assertFalse($this->container->get('web_profiler.debug_toolbar')->isEnabled()); $this->assertSaneContainer($this->getDumpedContainer()); } @@ -99,14 +99,13 @@ class WebProfilerExtensionTest extends TestCase /** * @dataProvider getDebugModes */ - public function testToolbarConfig($debug) + public function testToolbarConfig($enabled, $verbose) { - $this->container->setParameter('kernel.debug', $debug); - $extension = new WebProfilerExtension(); - $extension->load(array(array('toolbar' => $debug)), $this->container); + $extension->load(array(array('toolbar' => $enabled, 'verbose' => $verbose)), $this->container); - $this->assertTrue($debug === $this->container->has('web_profiler.debug_toolbar'), '->load() registers web_profiler.debug_toolbar only when toolbar is true'); + $this->assertSame($enabled, $this->container->get('web_profiler.debug_toolbar')->isEnabled()); + $this->assertSame($enabled && $verbose, $this->container->get('web_profiler.debug_toolbar')->isVerbose()); $this->assertSaneContainer($this->getDumpedContainer()); } @@ -114,8 +113,10 @@ class WebProfilerExtensionTest extends TestCase public function getDebugModes() { return array( - array(true), - array(false), + array(true, true), + array(true, false), + array(false, false), + array(false, true), ); }