merged branch fabpot/profiler-activation (PR #7859)

This PR was merged into the master branch.

Discussion
----------

Profiler activation

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | yes
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #7064, #7071
| License       | MIT
| Doc PR        | symfony/symfony-docs#2565

As stated in #7071, there is no way to disable the profiler completely. Even when the `enabled` flag is set to `false`, the profiler is still registered but the data collectors are not activated.

Now, when `enabled` is `false`, the profiler is disabled. To get the old `false` behavior, you now need to set `enabled` to `true` and set the new `collect` flag to `false`.

Todo:

 - [x] update docs
 - [x] update Symfony SE -- not needed

Commits
-------

88ebd62 fixed the registration of the web profiler when the profiler is disabled
a11f901 [FrameworkBundle] added a way to disable the profiler
f675dd8 Truly disabled profiler in prod
This commit is contained in:
Fabien Potencier 2013-04-26 19:13:23 +02:00
commit 06d5fb171f
15 changed files with 110 additions and 14 deletions

View File

@ -4,6 +4,8 @@ CHANGELOG
2.3.0
-----
* [BC BREAK] added a way to disable the profiler (when disabling the profiler, it is now completely removed)
To get the same "disabled" behavior as before, set `enabled` to `true` and `collect` to `false`
* [BC BREAK] the `Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler\RegisterKernelListenersPass` was moved
to `Component\HttpKernel\DependencyInjection\RegisterListenersPass`
* added ControllerNameParser::build() which converts a controller short notation (a:b:c) to a class::method notation

View File

@ -139,6 +139,7 @@ class Configuration implements ConfigurationInterface
->info('profiler configuration')
->canBeEnabled()
->children()
->booleanNode('collect')->defaultTrue()->end()
->booleanNode('only_exceptions')->defaultFalse()->end()
->booleanNode('only_master_requests')->defaultFalse()->end()
->scalarNode('dsn')->defaultValue('file:%kernel.cache_dir%/profiler')->end()

View File

@ -209,6 +209,13 @@ class FrameworkExtension extends Extension
*/
private function registerProfilerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
{
if (!$this->isConfigEnabled($container, $config)) {
// this is needed for the WebProfiler to work even if the profiler is disabled
$container->setParameter('data_collector.templates', array());
return;
}
$loader->load('profiling.xml');
$loader->load('collectors.xml');
@ -254,7 +261,7 @@ class FrameworkExtension extends Extension
}
}
if (!$this->isConfigEnabled($container, $config)) {
if (!$config['collect']) {
$container->getDefinition('profiler')->addMethodCall('disable', array());
}
}

View File

@ -53,6 +53,7 @@
<xsd:element name="matcher" type="profiler_matcher" minOccurs="0" maxOccurs="1" />
</xsd:all>
<xsd:attribute name="collect" type="xsd:string" />
<xsd:attribute name="only-exceptions" type="xsd:string" />
<xsd:attribute name="only-master-requests" type="xsd:string" />
<xsd:attribute name="enabled" type="xsd:string" />

View File

@ -111,6 +111,7 @@ class ConfigurationTest extends \PHPUnit_Framework_TestCase
'username' => '',
'password' => '',
'lifetime' => 86400,
'collect' => true,
),
'translator' => array(
'enabled' => false,

View File

@ -0,0 +1,7 @@
<?php
$container->loadFromExtension('framework', array(
'profiler' => array(
'enabled' => true,
),
));

View File

@ -0,0 +1,12 @@
<?xml version="1.0" ?>
<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>
<framework:profiler enabled="true" />
</framework:config>
</container>

View File

@ -0,0 +1,3 @@
framework:
profiler:
enabled: true

View File

@ -54,17 +54,20 @@ abstract class FrameworkExtensionTest extends TestCase
$this->assertTrue($container->hasDefinition('esi'), '->registerEsiConfiguration() loads esi.xml');
}
public function testProfiler()
public function testEnabledProfiler()
{
$container = $this->createContainerFromFile('full');
$container = $this->createContainerFromFile('profiler');
$this->assertTrue($container->hasDefinition('profiler'), '->registerProfilerConfiguration() loads profiling.xml');
$this->assertTrue($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() loads collectors.xml');
$this->assertTrue($container->getParameter('profiler_listener.only_exceptions'));
$this->assertEquals('%profiler_listener.only_exceptions%', $container->getDefinition('profiler_listener')->getArgument(2));
}
$calls = $container->getDefinition('profiler')->getMethodCalls();
$this->assertEquals('disable', $calls[0][0]);
public function testDisabledProfiler()
{
$container = $this->createContainerFromFile('full');
$this->assertFalse($container->hasDefinition('profiler'), '->registerProfilerConfiguration() does not load profiling.xml');
$this->assertFalse($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() does not load collectors.xml');
}
public function testRouter()

View File

@ -3,4 +3,5 @@ imports:
framework:
profiler:
enabled: false
enabled: true
collect: false

View File

@ -13,6 +13,7 @@ namespace Symfony\Bundle\WebProfilerBundle\Controller;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\HttpKernel\Debug\ExceptionHandler;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpFoundation\Response;
/**
@ -26,7 +27,7 @@ class ExceptionController
protected $debug;
protected $profiler;
public function __construct(Profiler $profiler, \Twig_Environment $twig, $debug)
public function __construct(Profiler $profiler = null, \Twig_Environment $twig, $debug)
{
$this->profiler = $profiler;
$this->twig = $twig;
@ -42,6 +43,10 @@ class ExceptionController
*/
public function showAction($token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
@ -76,6 +81,10 @@ class ExceptionController
*/
public function cssAction($token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();

View File

@ -43,7 +43,7 @@ class ProfilerController
* @param array $templates The templates
* @param string $toolbarPosition The toolbar position (top, bottom, normal, or null -- use the configuration)
*/
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
{
$this->generator = $generator;
$this->profiler = $profiler;
@ -59,6 +59,10 @@ class ProfilerController
*/
public function homeAction()
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
return new RedirectResponse($this->generator->generate('_profiler_search_results', array('token' => 'empty', 'limit' => 10)));
@ -76,6 +80,10 @@ class ProfilerController
*/
public function panelAction(Request $request, $token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$panel = $request->query->get('panel', 'request');
@ -112,6 +120,10 @@ class ProfilerController
*/
public function exportAction($token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
if (!$profile = $this->profiler->loadProfile($token)) {
@ -131,6 +143,10 @@ class ProfilerController
*/
public function purgeAction()
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$this->profiler->purge();
@ -146,6 +162,10 @@ class ProfilerController
*/
public function importAction(Request $request)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$file = $request->files->get('file');
@ -170,6 +190,10 @@ class ProfilerController
*/
public function infoAction($about)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
@ -187,6 +211,10 @@ class ProfilerController
*/
public function toolbarAction(Request $request, $token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$session = $request->getSession();
if (null !== $session && $session->getFlashBag() instanceof AutoExpireFlashBag) {
@ -234,6 +262,10 @@ class ProfilerController
*/
public function searchBarAction(Request $request)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
if (null === $session = $request->getSession()) {
@ -275,6 +307,10 @@ class ProfilerController
*/
public function searchResultsAction(Request $request, $token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$profile = $this->profiler->loadProfile($token);
@ -309,6 +345,10 @@ class ProfilerController
*/
public function searchAction(Request $request)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
@ -353,6 +393,10 @@ class ProfilerController
*/
public function phpinfoAction()
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
ob_start();

View File

@ -30,7 +30,7 @@ class RouterController
private $matcher;
private $routes;
public function __construct(Profiler $profiler, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, RouteCollection $routes = null)
public function __construct(Profiler $profiler = null, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, RouteCollection $routes = null)
{
$this->profiler = $profiler;
$this->twig = $twig;
@ -51,6 +51,10 @@ class RouterController
*/
public function panelAction($token)
{
if (null === $this->profiler) {
throw new NotFoundHttpException('The profiler must be enabled.');
}
$this->profiler->disable();
if (null === $this->matcher || null === $this->routes) {

View File

@ -13,20 +13,20 @@
<services>
<service id="web_profiler.controller.profiler" class="%web_profiler.controller.profiler.class%">
<argument type="service" id="router" on-invalid="null" />
<argument type="service" id="profiler" />
<argument type="service" id="profiler" on-invalid="null" />
<argument type="service" id="twig" />
<argument>%data_collector.templates%</argument>
<argument>%web_profiler.debug_toolbar.position%</argument>
</service>
<service id="web_profiler.controller.router" class="%web_profiler.controller.router.class%">
<argument type="service" id="profiler" />
<argument type="service" id="profiler" on-invalid="null" />
<argument type="service" id="twig" />
<argument type="service" id="router" on-invalid="null" />
</service>
<service id="web_profiler.controller.exception" class="%web_profiler.controller.exception.class%">
<argument type="service" id="profiler" />
<argument type="service" id="profiler" on-invalid="null" />
<argument type="service" id="twig" />
<argument>%kernel.debug%</argument>
</service>

View File

@ -61,6 +61,7 @@ class WebProfilerExtensionTest extends TestCase
$this->container->setParameter('kernel.cache_dir', __DIR__);
$this->container->setParameter('kernel.debug', false);
$this->container->setParameter('kernel.root_dir', __DIR__);
$this->container->setParameter('profiler.class', array('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'));
$this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
$this->container->setParameter('data_collector.templates', array());