Skip to content

Commit 5dcb5c5

Browse files
committed
[HttpKernel][FrameworkBundle] Add the ability to enable the profiler using a parameter
1 parent 31d81e2 commit 5dcb5c5

File tree

11 files changed

+101
-1
lines changed

11 files changed

+101
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
---
66

77
* Add autowiring alias for `HttpCache\StoreInterface`
8+
* Add the ability to enable the profiler using a parameter
89
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
910
* Deprecate the public `profiler` service to private
1011
* Deprecate `get()`, `has()`, `getDoctrine()`, and `dispatchMessage()` in `AbstractController`, use method/constructor injection instead

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ private function addProfilerSection(ArrayNodeDefinition $rootNode)
300300
->canBeEnabled()
301301
->children()
302302
->booleanNode('collect')->defaultTrue()->end()
303+
->scalarNode('collect_parameter')->defaultNull()->info('The name of the parameter to use to enable or disable collection on a per request basis')->end()
303304
->booleanNode('only_exceptions')->defaultFalse()->end()
304305
->booleanNode('only_main_requests')->defaultFalse()->end()
305306
->booleanNode('only_master_requests')->setDeprecated('symfony/framework-bundle', '5.3', 'Option "%node%" at "%path%" is deprecated, use "only_main_requests" instead.')->defaultFalse()->end()

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,9 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
742742
$container->getDefinition('profiler')
743743
->addArgument($config['collect'])
744744
->addTag('kernel.reset', ['method' => 'reset']);
745+
746+
$container->getDefinition('profiler_listener')
747+
->addArgument($config['collect_parameter']);
745748
}
746749

747750
private function registerWorkflowConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader)

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
@@ -87,6 +87,7 @@
8787

8888
<xsd:complexType name="profiler">
8989
<xsd:attribute name="collect" type="xsd:string" />
90+
<xsd:attribute name="collect-parameter" type="xsd:string" />
9091
<xsd:attribute name="only-exceptions" type="xsd:string" />
9192
<xsd:attribute name="only-main-requests" type="xsd:string" />
9293
<xsd:attribute name="only-master-requests" type="xsd:string" />

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/ProfilerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ public function testProfilerIsDisabled($insulate)
3636
$this->assertNull($client->getProfile());
3737
}
3838

39+
/**
40+
* @dataProvider getConfigs
41+
*/
42+
public function testProfilerCollectParameter($insulate)
43+
{
44+
$client = $this->createClient(['test_case' => 'ProfilerCollectParameter', 'root_config' => 'config.yml']);
45+
if ($insulate) {
46+
$client->insulate();
47+
}
48+
49+
$client->request('GET', '/profiler');
50+
$this->assertNull($client->getProfile());
51+
52+
// enable the profiler for the next request
53+
$client->request('GET', '/profiler?profile=1');
54+
$this->assertIsObject($client->getProfile());
55+
56+
$client->request('GET', '/profiler');
57+
$this->assertNull($client->getProfile());
58+
}
59+
3960
public function getConfigs()
4061
{
4162
return [
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
13+
use Symfony\Bundle\FrameworkBundle\Tests\Functional\Bundle\TestBundle\TestBundle;
14+
15+
return [
16+
new FrameworkBundle(),
17+
new TestBundle(),
18+
];
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
imports:
2+
- { resource: ../config/default.yml }
3+
4+
framework:
5+
profiler:
6+
enabled: true
7+
collect: false
8+
collect_parameter: profile
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
_sessiontest_bundle:
2+
resource: '@TestBundle/Resources/config/routing.yml'

src/Symfony/Component/HttpKernel/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ CHANGELOG
44
5.4
55
---
66

7+
* Add the ability to enable the profiler using a parameter
78
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
89
* Deprecate the `fileLinkFormat` parameter of `DebugHandlersListener`
910

src/Symfony/Component/HttpKernel/EventListener/ProfilerListener.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,14 @@ class ProfilerListener implements EventSubscriberInterface
3636
protected $exception;
3737
protected $profiles;
3838
protected $requestStack;
39+
protected $collectParameter;
3940
protected $parents;
4041

4142
/**
4243
* @param bool $onlyException True if the profiler only collects data when an exception occurs, false otherwise
4344
* @param bool $onlyMainRequests True if the profiler only collects data when the request is the main request, false otherwise
4445
*/
45-
public function __construct(Profiler $profiler, RequestStack $requestStack, RequestMatcherInterface $matcher = null, bool $onlyException = false, bool $onlyMainRequests = false)
46+
public function __construct(Profiler $profiler, RequestStack $requestStack, RequestMatcherInterface $matcher = null, bool $onlyException = false, bool $onlyMainRequests = false, string $collectParameter = null)
4647
{
4748
$this->profiler = $profiler;
4849
$this->matcher = $matcher;
@@ -51,6 +52,7 @@ public function __construct(Profiler $profiler, RequestStack $requestStack, Requ
5152
$this->profiles = new \SplObjectStorage();
5253
$this->parents = new \SplObjectStorage();
5354
$this->requestStack = $requestStack;
55+
$this->collectParameter = $collectParameter;
5456
}
5557

5658
/**
@@ -79,6 +81,10 @@ public function onKernelResponse(ResponseEvent $event)
7981
}
8082

8183
$request = $event->getRequest();
84+
if (null !== $this->collectParameter && null !== $collectParameterValue = $request->get($this->collectParameter)) {
85+
true === $collectParameterValue || filter_var($collectParameterValue, \FILTER_VALIDATE_BOOLEAN) ? $this->profiler->enable() : $this->profiler->disable();
86+
}
87+
8288
$exception = $this->exception;
8389
$this->exception = null;
8490

0 commit comments

Comments
 (0)