8000 merged branch fabpot/profiler-activation (PR #7859) · symfony/symfony@06d5fb1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 06d5fb1

Browse files
committed
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 8000 is disabled a11f901 [FrameworkBundle] added a way to disable the profiler f675dd8 Truly disabled profiler in prod
2 parents 3c90abf + 88ebd62 commit 06d5fb1

File tree

15 files changed

+110
-14
lines changed

15 files changed

+110
-14
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ CHANGELOG
44
2.3.0
55
-----
66

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

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@ private function addProfilerSection(ArrayNodeDefinition $rootNode)
139139
->info('profiler configuration')
140140
->canBeEnabled()
141141
->children()
142+
->booleanNode('collect')->defaultTrue()->end()
142143
->booleanNode('only_exceptions')->defaultFalse()->end()
143144
->booleanNode('only_master_requests')->defaultFalse()->end()
144145
->scalarNode('dsn')->defaultValue('file:%kernel.cache_dir%/profiler')->end()

src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,13 @@ private function registerFragmentsConfiguration(array $config, ContainerBuilder
209209
*/
210210
private function registerProfilerConfiguration(array $config, ContainerBuilder $container, XmlFileLoader $loader)
211211
{
212+
if (!$this->isConfigEnabled($container, $config)) {
213+
// this is needed for the WebProfiler to work even if the profiler is disabled
214+
$container->setParameter('data_collector.templates', array());
215+
216+
return;
217+
}
218+
212219
$loader->load('profiling.xml');
213220
$loader->load('collectors.xml');
214221

@@ -254,7 +261,7 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
254261
}
255262
}
256263

257-
if (!$this->isConfigEnabled($container, $config)) {
264+
if (!$config['collect']) {
258265
$container->getDefinition('profiler')->addMethodCall('disable', array());
259266
}
260267
}

src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<xsd:element name="matcher" type="profiler_matcher" minOccurs="0" maxOccurs="1" />
5454
</xsd:all>
5555

56+
<xsd:attribute name="collect" type="xsd:string" />
5657
<xsd:attribute name="only-exceptions" type="xsd:string" />
5758
<xsd:attribute name="only-master-requests" type="xsd:string" />
5859
<xsd:attribute name="enabled" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@ protected static function getBundleDefaultConfig()
111111
'username' => '',
112112
'password' => '',
113113
'lifetime' => 86400,
114+
'collect' => true,
114115
),
115116
'translator' => array(
116117
'enabled' => false,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
$container->loadFromExtension('framework', array(
4+
'profiler' => array(
5+
'enabled' => true,
6+
),
7+
));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xmlns:framework="http://symfony.com/schema/dic/symfony"
6+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd
7+
http://symfony.com/schema/dic/symfony http://symfony.com/schema/dic/symfony/symfony-1.0.xsd">
8+
9+
<framework:config>
10+
<framework:profiler enabled="true" />
11+
</framework:config>
12+
</container>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
framework:
2+
profiler:
3+
enabled: true

src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,20 @@ public function testEsi()
5454
$this->assertTrue($container->hasDefinition('esi'), '->registerEsiConfiguration() loads esi.xml');
5555
}
5656

57-
public function testProfiler()
57+
public function testEnabledProfiler()
5858
{
59-
$container = $this->createContainerFromFile('full');
59+
$container = $this->createContainerFromFile('profiler');
6060

6161
$this->assertTrue($container->hasDefinition('profiler'), '->registerProfilerConfiguration() loads profiling.xml');
6262
$this->assertTrue($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() loads collectors.xml');
63-
$this->assertTrue($container->getParameter('profiler_listener.only_exceptions'));
64-
$this->assertEquals('%profiler_listener.only_exceptions%', $container->getDefinition('profiler_listener')->getArgument(2));
63+
}
64+
65+
public function testDisabledProfiler()
66+
{
67+
$container = $this->createContainerFromFile('full');
6568

66-
$calls = $container->getDefinition('profiler')->getMethodCalls();
67-
$this->assertEquals('disable', $calls[0][0]);
69+
$this->assertFalse($container->hasDefinition('profiler'), '->registerProfilerConfiguration() does not load profiling.xml');
70+
$this->assertFalse($container->hasDefinition('data_collector.config'), '->registerProfilerConfiguration() does not load collectors.xml');
6871
}
6972

7073
public function testRouter()

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/app/Profiler/config.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ imports:
33

44
framework:
55
profiler:
6-
enabled: false
6+
enabled: true
7+
collect: false

src/Symfony/Bundle/WebProfilerBundle/Controller/ExceptionController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Symfony\Component\HttpKernel\Profiler\Profiler;
1515
use Symfony\Component\HttpKernel\Debug\ExceptionHandler;
16+
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
1617
use Symfony\Component\HttpFoundation\Response;
1718

1819
/**
@@ -26,7 +27,7 @@ class ExceptionController
2627
protected $debug;
2728
protected $profiler;
2829

29-
public function __construct(Profiler $profiler, \Twig_Environment $twig, $debug)
30+
public function __construct(Profiler $profiler = null, \Twig_Environment $twig, $debug)
3031
{
3132
$this->profiler = $profiler;
3233
$this->twig = $twig;
@@ -42,6 +43,10 @@ public function __construct(Profiler $profiler, \Twig_Environment $twig, $debug)
4243
*/
4344
public function showAction($token)
4445
{
46+
if (null === $this->profiler) {
47+
throw new NotFoundHttpException('The profiler must be enabled.');
48+
}
49+
4550
$this->profiler->disable();
4651

4752
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();
@@ -76,6 +81,10 @@ public function showAction($token)
7681
*/
7782
public function cssAction($token)
7883
{
84+
if (null === $this->profiler) {
85+
throw new NotFoundHttpException('The profiler must be enabled.');
86+
}
87+
7988
$this->profiler->disable();
8089

8190
$exception = $this->profiler->loadProfile($token)->getCollector('exception')->getException();

src/Symfony/Bundle/WebProfilerBundle/Controller/ProfilerController.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class ProfilerController
4343
* @param array $templates The templates
4444
* @param string $toolbarPosition The toolbar position (top, bottom, normal, or null -- use the configuration)
4545
*/
46-
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
46+
public function __construct(UrlGeneratorInterface $generator, Profiler $profiler = null, \Twig_Environment $twig, array $templates, $toolbarPosition = 'normal')
4747
{
4848
$this->generator = $generator;
4949
$this->profiler = $profiler;
@@ -59,6 +59,10 @@ public function __construct(UrlGeneratorInterface $generator, Profiler $profiler
5959
*/
6060
public function homeAction()
6161
{
62+
if (null === $this->profiler) {
63+
throw new NotFoundHttpException('The profiler must be enabled.');
64+
}
65+
6266
$this->profiler->disable();
6367

6468
return new RedirectResponse($this->generator->generate('_profiler_search_results', array('token' => 'empty', 'limit' => 10)));
@@ -76,6 +80,10 @@ public function homeAction()
7680
*/
7781
public function panelAction(Request $request, $token)
7882
{
83+
if (null === $this->profiler) {
84+
throw new NotFoundHttpException('The profiler must be enabled.');
85+
}
86+
7987
$this->profiler->disable();
8088

8189
$panel = $request->query->get('panel', 'request');
@@ -112,6 +120,10 @@ public function panelAction(Request $request, $token)
112120
*/
113121
public function exportAction($token)
114122
{
123+
if (null === $this->profiler) {
124+
throw new NotFoundHttpException('The profiler must be enabled.');
125+
}
126+
115127
$this->profiler->disable();
116128

117129
if (!$profile = $this->profiler->loadProfile($token)) {
@@ -131,6 +143,10 @@ public function exportAction($token)
131143
*/
132144
public function purgeAction()
133145
{
146+
if (null === $this->profiler) {
147+
throw new NotFoundHttpException('The profiler must be enabled.');
148+
}
149+
134150
$this->profiler->disable();
135151
$this->profiler->purge();
136152

@@ -146,6 +162,10 @@ public function purgeAction()
146162
*/
147163
public function importAction(Request $request)
148164
{
165+
if (null === $this->profiler) {
166+
throw new NotFoundHttpException('The profiler must be enabled.');
167+
}
168+
149169
$this->profiler->disable();
150170

151171
$file = $request->files->get('file');
@@ -170,6 +190,10 @@ public function importAction(Request $request)
170190
*/
171191
public function infoAction($about)
172192
{
193+
if (null === $this->profiler) {
194+
throw new NotFoundHttpException('The profiler must be enabled.');
195+
}
196+
173197
$this->profiler->disable();
174198

175199
return new Response($this->twig->render('@WebProfiler/Profiler/info.html.twig', array(
@@ -187,6 +211,10 @@ public function infoAction($about)
187211
*/
188212
public function toolbarAction(Request $request, $token)
189213
{
214+
if (null === $this->profiler) {
215+
throw new NotFoundHttpException('The profiler must be enabled.');
216+
}
217+
190218
$session = $request->getSession();
191219

192220
if (null !== $session && $session->getFlashBag() instanceof AutoExpireFlashBag) {
@@ -234,6 +262,10 @@ public function toolbarAction(Request $request, $token)
234262
*/
235263
public function searchBarAction(Request $request)
236264
{
265+
if (null === $this->profiler) {
266+
throw new NotFoundHttpException('The profiler must be enabled.');
267+
}
268+
237269
$this->profiler->disable();
238270

239271
if (null === $session = $request->getSession()) {
@@ -275,6 +307,10 @@ public function searchBarAction(Request $request)
275307
*/
276308
public function searchResultsAction(Request $request, $token)
277309
{
310+
if (null === $this->profiler) {
311+
throw new NotFoundHttpException('The profiler must be enabled.');
312+
}
313+
278314
$this->profiler->disable();
279315

280316
$profile = $this->profiler->loadProfile($token);
@@ -309,6 +345,10 @@ public function searchResultsAction(Request $request, $token)
309345
*/
310346
public function searchAction(Request $request)
311347
{
348+
if (null === $this->profiler) {
349+
throw new NotFoundHttpException('The profiler must be enabled.');
350+
}
351+
312352
$this->profiler->disable();
313353

314354
$ip = preg_replace('/[^:\d\.]/', '', $request->query->get('ip'));
@@ -353,6 +393,10 @@ public function searchAction(Request $request)
353393
*/
354394
public function phpinfoAction()
355395
{
396+
if (null === $this->profiler) {
397+
throw new NotFoundHttpException('The profiler must be enabled.');
398+
}
399+
356400
$this->profiler->disable();
357401

358402
ob_start();

src/Symfony/Bundle/WebProfilerBundle/Controller/RouterController.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class RouterController
3030
private $matcher;
3131
private $routes;
3232

33-
public function __construct(Profiler $profiler, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, RouteCollection $routes = null)
33+
public function __construct(Profiler $profiler = null, \Twig_Environment $twig, UrlMatcherInterface $matcher = null, RouteCollection $routes = null)
3434
{
3535
$this->profiler = $profiler;
3636
$this->twig = $twig;
@@ -51,6 +51,10 @@ public function __construct(Profiler $profiler, \Twig_Environment $twig, UrlMatc
5151
*/
5252
public function panelAction($token)
5353
{
54+
if (null === $this->profiler) {
55+
throw new NotFoundHttpException('The profiler must be enabled.');
56+
}
57+
5458
$this->profiler->disable();
5559

5660
if (null === $this->matcher || null === $this->routes) {

src/Symfony/Bundle/WebProfilerBundle/Resources/config/profiler.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@
1313
<services>
1414
<service id="web_profiler.controller.profiler" class="%web_profiler.controller.profiler.class%">
1515
<argument type="service" id="router" on-invalid="null" />
16-
<argument type="service" id="profiler" />
16+
<argument type="service" id="profiler" on-invalid="null" />
1717
<argument type="service" id="twig" />
1818
<argument>%data_collector.templates%</argument>
1919
<argument>%web_profiler.debug_toolbar.position%</argument>
2020
</service>
2121

2222
<service id="web_profiler.controller.router" class="%web_profiler.controller.router.class%">
23-
<argument type="service" id="profiler" />
23+
<argument type="service" id="profiler" on-invalid="null" />
2424
<argument type="service" id="twig" />
2525
<argument type="service" id="router" on-invalid="null" />
2626
</service>
2727

2828
<service id="web_profiler.controller.exception" class="%web_profiler.controller.exception.class%">
29-
<argument type="service" id="profiler" />
29+
<argument type="service" id="profiler" on-invalid="null" />
3030
<argument type="service" id="twig" />
3131
<argument>%kernel.debug%</argument>
3232
</service>

src/Symfony/Bundle/WebProfilerBundle/Tests/DependencyInjection/WebProfilerExtensionTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ protected function setUp()
6161
$this->container->setParameter('kernel.cache_dir', __DIR__);
6262
$this->container->setParameter('kernel.debug', false);
6363
$this->container->setParameter('kernel.root_dir', __DIR__);
64+
$this->container->setParameter('profiler.class', array('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'));
6465
$this->container->register('profiler', $this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\Profiler'))
6566
->addArgument(new Definition($this->getMockClass('Symfony\\Component\\HttpKernel\\Profiler\\ProfilerStorageInterface')));
6667
$this->container->setParameter('data_collector.templates', array());

0 commit comments

Comments
 (0)
0