make the stopwatch service always available
Previously, one had to be careful to check if the debug.stopwatch service was available before using it. Otherwise, the application would break in the prod environment.
This commit is contained in:
parent
c812379479
commit
ffc4090d21
@ -23,9 +23,15 @@ class StopwatchExtension extends \Twig_Extension
|
|||||||
{
|
{
|
||||||
private $stopwatch;
|
private $stopwatch;
|
||||||
|
|
||||||
public function __construct(Stopwatch $stopwatch = null)
|
/**
|
||||||
|
* @var bool
|
||||||
|
*/
|
||||||
|
private $enabled;
|
||||||
|
|
||||||
|
public function __construct(Stopwatch $stopwatch = null, $enabled = true)
|
||||||
{
|
{
|
||||||
$this->stopwatch = $stopwatch;
|
$this->stopwatch = $stopwatch;
|
||||||
|
$this->enabled = $enabled;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getStopwatch()
|
public function getStopwatch()
|
||||||
@ -41,7 +47,7 @@ class StopwatchExtension extends \Twig_Extension
|
|||||||
* Some stuff which will be recorded on the timeline
|
* Some stuff which will be recorded on the timeline
|
||||||
* {% endstopwatch %}
|
* {% endstopwatch %}
|
||||||
*/
|
*/
|
||||||
new StopwatchTokenParser($this->stopwatch !== null),
|
new StopwatchTokenParser($this->stopwatch !== null && $this->enabled),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -6,14 +6,11 @@
|
|||||||
|
|
||||||
<parameters>
|
<parameters>
|
||||||
<parameter key="debug.event_dispatcher.class">Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher</parameter>
|
<parameter key="debug.event_dispatcher.class">Symfony\Component\HttpKernel\Debug\TraceableEventDispatcher</parameter>
|
||||||
<parameter key="debug.stopwatch.class">Symfony\Component\Stopwatch\Stopwatch</parameter>
|
|
||||||
<parameter key="debug.container.dump">%kernel.cache_dir%/%kernel.container_class%.xml</parameter>
|
<parameter key="debug.container.dump">%kernel.cache_dir%/%kernel.container_class%.xml</parameter>
|
||||||
<parameter key="debug.controller_resolver.class">Symfony\Component\HttpKernel\Controller\TraceableControllerResolver</parameter>
|
<parameter key="debug.controller_resolver.class">Symfony\Component\HttpKernel\Controller\TraceableControllerResolver</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
<service id="debug.stopwatch" class="%debug.stopwatch.class%" />
|
|
||||||
|
|
||||||
<service id="debug.event_dispatcher" class="%debug.event_dispatcher.class%">
|
<service id="debug.event_dispatcher" class="%debug.event_dispatcher.class%">
|
||||||
<tag name="monolog.logger" channel="event" />
|
<tag name="monolog.logger" channel="event" />
|
||||||
<argument type="service" id="debug.event_dispatcher.parent" />
|
<argument type="service" id="debug.event_dispatcher.parent" />
|
||||||
|
@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
<parameters>
|
<parameters>
|
||||||
<parameter key="debug.debug_handlers_listener.class">Symfony\Component\HttpKernel\EventListener\DebugHandlersListener</parameter>
|
<parameter key="debug.debug_handlers_listener.class">Symfony\Component\HttpKernel\EventListener\DebugHandlersListener</parameter>
|
||||||
|
<parameter key="debug.stopwatch.class">Symfony\Component\Stopwatch\Stopwatch</parameter>
|
||||||
</parameters>
|
</parameters>
|
||||||
|
|
||||||
<services>
|
<services>
|
||||||
@ -17,5 +18,7 @@
|
|||||||
<argument /><!-- Log levels map for enabled error levels -->
|
<argument /><!-- Log levels map for enabled error levels -->
|
||||||
<argument>%kernel.debug%</argument>
|
<argument>%kernel.debug%</argument>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
|
<service id="debug.stopwatch" class="%debug.stopwatch.class%" />
|
||||||
</services>
|
</services>
|
||||||
</container>
|
</container>
|
||||||
|
@ -0,0 +1,3 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
$container->loadFromExtension('framework', array());
|
@ -0,0 +1,9 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8" ?>
|
||||||
|
<container xmlns="http://symfony.com/schema/dic/services"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns:framework="http://symfony.com/schema/dic/symfony"
|
||||||
|
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
|
||||||
|
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
|
||||||
|
|
||||||
|
<framework:config />
|
||||||
|
</container>
|
@ -0,0 +1 @@
|
|||||||
|
framework: ~
|
@ -402,6 +402,25 @@ abstract class FrameworkExtensionTest extends TestCase
|
|||||||
$this->assertEquals('_custom_form', $container->getParameter('form.type_extension.csrf.field_name'));
|
$this->assertEquals('_custom_form', $container->getParameter('form.type_extension.csrf.field_name'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testStopwatchEnabledWithDebugModeEnabled()
|
||||||
|
{
|
||||||
|
$container = $this->createContainerFromFile('default_config', array(
|
||||||
|
'kernel.container_class' => 'foo',
|
||||||
|
'kernel.debug' => true,
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertTrue($container->has('debug.stopwatch'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testStopwatchEnabledWithDebugModeDisabled()
|
||||||
|
{
|
||||||
|
$container = $this->createContainerFromFile('default_config', array(
|
||||||
|
'kernel.container_class' => 'foo',
|
||||||
|
));
|
||||||
|
|
||||||
|
$this->assertTrue($container->has('debug.stopwatch'));
|
||||||
|
}
|
||||||
|
|
||||||
protected function createContainer(array $data = array())
|
protected function createContainer(array $data = array())
|
||||||
{
|
{
|
||||||
return new ContainerBuilder(new ParameterBag(array_merge(array(
|
return new ContainerBuilder(new ParameterBag(array_merge(array(
|
||||||
|
@ -92,6 +92,7 @@
|
|||||||
<service id="twig.extension.debug.stopwatch" class="%twig.extension.debug.stopwatch.class%" public="false">
|
<service id="twig.extension.debug.stopwatch" class="%twig.extension.debug.stopwatch.class%" public="false">
|
||||||
<tag name="twig.extension" />
|
<tag name="twig.extension" />
|
||||||
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
|
<argument type="service" id="debug.stopwatch" on-invalid="ignore" />
|
||||||
|
<argument>%kernel.debug%</argument>
|
||||||
</service>
|
</service>
|
||||||
|
|
||||||
<service id="twig.extension.expression" class="%twig.extension.expression.class%" public="false">
|
<service id="twig.extension.expression" class="%twig.extension.expression.class%" public="false">
|
||||||
|
@ -179,6 +179,37 @@ class TwigExtensionTest extends TestCase
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider stopwatchExtensionAvailabilityProvider
|
||||||
|
*/
|
||||||
|
public function testStopwatchExtensionAvailability($debug, $stopwatchEnabled, $expected)
|
||||||
|
{
|
||||||
|
$container = $this->createContainer();
|
||||||
|
$container->setParameter('kernel.debug', $debug);
|
||||||
|
if ($stopwatchEnabled) {
|
||||||
|
$container->register('debug.stopwatch', 'Symfony\Component\Stopwatch\Stopwatch');
|
||||||
|
}
|
||||||
|
$container->registerExtension(new TwigExtension());
|
||||||
|
$container->loadFromExtension('twig', array());
|
||||||
|
$this->compileContainer($container);
|
||||||
|
|
||||||
|
$tokenParsers = $container->get('twig.extension.debug.stopwatch')->getTokenParsers();
|
||||||
|
$stopwatchIsAvailable = new \ReflectionProperty($tokenParsers[0], 'stopwatchIsAvailable');
|
||||||
|
$stopwatchIsAvailable->setAccessible(true);
|
||||||
|
|
||||||
|
$this->assertSame($expected, $stopwatchIsAvailable->getValue($tokenParsers[0]));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function stopwatchExtensionAvailabilityProvider()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
'debug-and-stopwatch-enabled' => array(true, true, true),
|
||||||
|
'only-stopwatch-enabled' => array(false, true, false),
|
||||||
|
'only-debug-enabled' => array(true, false, false),
|
||||||
|
'debug-and-stopwatch-disabled' => array(false, false, false),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
private function createContainer()
|
private function createContainer()
|
||||||
{
|
{
|
||||||
$container = new ContainerBuilder(new ParameterBag(array(
|
$container = new ContainerBuilder(new ParameterBag(array(
|
||||||
|
Reference in New Issue
Block a user