8000 [FrameworkBundle][HttpKernel] Add the ability to enable the profiler … · symfony/symfony@eecff07 · GitHub
[go: up one dir, main page]

Skip to content

Commit eecff07

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle][HttpKernel] Add the ability to enable the profiler using a parameter
1 parent b2fc70b commit eecff07

File tree

12 files changed

+102
-1
lines changed

12 files changed

+102
-1
lines changed

src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ CHANGELOG
99
* Add `set_content_language_from_locale` config option to automatically set the `Content-Language` HTTP response header based on the Request locale
1010
* Deprecate the `framework.translator.enabled_locales`, use `framework.enabled_locales` instead
1111
* Add autowiring alias for `HttpCache\StoreInterface`
12+
* Add the ability to enable the profiler using a request query parameter, body parameter or attribute
1213
* Deprecate the `AdapterInterface` autowiring alias, use `CacheItemPoolInterface` instead
1314
* Deprecate the public `profiler` service to private
1415
* 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
@@ -315,6 +315,7 @@ private function addProfilerSection(ArrayNodeDefinition $rootNode)
315315
->canBeEnabled()
316316
->children()
317317
->booleanNode('collect')->defaultTrue()->end()
318+
->scalarNode('collect_parameter')->defaultNull()->info('The name of the parameter to use to enable or disable collection on a per request basis')->end()
318319
->booleanNode('only_exceptions')->defaultFalse()->end()
319320
->booleanNode('only_main_requests')->defaultFalse()->end()
320321
->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
@@ -770,6 +770,9 @@ private function registerProfilerConfiguration(array $config, ContainerBuilder $
770770
$container->getDefinition('profiler')
771771
->addArgument($config['collect'])
772772
->addTag('kernel.reset', ['method' => 'reset']);
773+
774+
$container->getDefinition('profiler_listener')
775+
->addArgument($config['collect_parameter']);
773776
}
774777

775778
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
@@ -91,6 +91,7 @@
9191

9292
<xsd:complexType name="profiler">
9393
<xsd:attribute name="collect" type="xsd:string" />
94+
<xsd:attribute name="collect-parameter" type="xsd:string" />
9495
<xsd:attribute name="only-exceptions" type="xsd:string" />
9596
<xsd:attribute name="only-main-requests" type="xsd:string" />
9697
<xsd:attribute name="only-master-requests" 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
@@ -405,6 +405,7 @@ protected static function getBundleDefaultConfig()
405405
'only_main_requests' => false,
406406
'dsn' => 'file:%kernel.cache_dir%/profiler',
407407
'collect' => true,
408+
'collect_parameter' => null,
408409
],
409410
'translator' => [
410411
'enabled' => !class_exists(FullStack::class),

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 request query parameter, body parameter or attribute
78
* Deprecate `AbstractTestSessionListener::getSession` inject a session in the request instead
89
* Deprecate the `fileLinkFormat` parameter of `DebugHandlersListener`
910
* Add support for configuring log level, and status code by exception class

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

src/Symfony/Component/HttpKernel/Tests/EventListener/ProfilerListenerTest.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,42 @@ public function testKernelTerminate()
5959

6060
$listener->onKernelTerminate(new TerminateEvent($kernel, $mainRequest, $response));
6161
}
62+
63+
/**
64+
* @dataProvider collectRequestProvider
65+
*/
66+
public function testCollectParameter(Request $request, ?bool $enable)
67+
{
68+
$profile = new Profile('token');
69+
70+
$profiler = $this->createMock(Profiler::class);
71+
$profiler->expects($this->once())
72+
->method('collect')
73+
->willReturn($profile);
74+
75+
$profiler
76+
->expects(null === $enable ? $this->never() : $this->once())
77+
->method($enable ? 'enable' : 'disable');
78+
79+
$kernel = $this->createMock(HttpKernelInterface::class);
80+
$response = new Response();
81+
82+
$requestStack = new RequestStack();
83+
$requestStack->push($request);
84+
85+
$listener = new ProfilerListener($profiler, $requestStack, null, false, false, 'profile');
86+
87+
$listener->onKernelResponse(new ResponseEvent($kernel, $request, Kernel::MAIN_REQUEST, $response));
88+
}
89+
90+
public function collectRequestProvider(): iterable
91+
{
92+
yield [Request::create('/'), null];
93+
yield [Request::create('/', 'GET', ['profile' => '1']), true];
94+
yield [Request::create('/', 'GET', ['profile' => '0']), false];
95+
96+
$request = Request::create('/');
97+
$request->attributes->set('profile', true);
98+
yield [$request, true];
99+
}
62100
}

0 commit comments

Comments
 (0)
0