Skip to content

Commit eb2be83

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

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

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

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

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)