Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f2f96a0

Browse filesBrowse files
committed
bug #42331 [HttpKernel] always close open stopwatch section after handling kernel.request events (xabbuh)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpKernel] always close open stopwatch section after handling `kernel.request` events | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #36623 | License | MIT | Doc PR | Commits ------- 705f765 always close open stopwatch section after handling kernel.request events
2 parents 1d99dbf + 705f765 commit f2f96a0
Copy full SHA for f2f96a0

File tree

2 files changed

+19
-16
lines changed
Filter options

2 files changed

+19
-16
lines changed

‎src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Debug/TraceableEventDispatcher.php
+10-9Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ protected function beforeDispatch(string $eventName, $event)
3030
{
3131
switch ($eventName) {
3232
case KernelEvents::REQUEST:
33+
$event->getRequest()->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
3334
$this->stopwatch->openSection();
3435
break;
3536
case KernelEvents::VIEW:
@@ -40,8 +41,8 @@ protected function beforeDispatch(string $eventName, $event)
4041
}
4142
break;
4243
case KernelEvents::TERMINATE:
43-
$token = $event->getResponse()->headers->get('X-Debug-Token');
44-
if (null === $token) {
44+
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
45+
if (null === $sectionId) {
4546
break;
4647
}
4748
// There is a very special case when using built-in AppCache class as kernel wrapper, in the case
@@ -50,7 +51,7 @@ protected function beforeDispatch(string $eventName, $event)
5051
// is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
5152
// which must be caught.
5253
try {
53-
$this->stopwatch->openSection($token);
54+
$this->stopwatch->openSection($sectionId);
5455
} catch (\LogicException $e) {
5556
}
5657
break;
@@ -67,21 +68,21 @@ protected function afterDispatch(string $eventName, $event)
6768
$this->stopwatch->start('controller', 'section');
6869
break;
6970
case KernelEvents::RESPONSE:
70-
$token = $event->getResponse()->headers->get('X-Debug-Token');
71-
if (null === $token) {
71+
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
72+
if (null === $sectionId) {
7273
break;
7374
}
74-
$this->stopwatch->stopSection($token);
75+
$this->stopwatch->stopSection($sectionId);
7576
break;
7677
case KernelEvents::TERMINATE:
7778
// In the special case described in the `preDispatch` method above, the `$token` section
7879
// does not exist, then closing it throws an exception which must be caught.
79-
$token = $event->getResponse()->headers->get('X-Debug-Token');
80-
if (null === $token) {
80+
$sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
81+
if (null === $sectionId) {
8182
break;
8283
}
8384
try {
84-
$this->stopwatch->stopSection($token);
85+
$this->stopwatch->stopSection($sectionId);
8586
} catch (\LogicException $e) {
8687
}
8788
break;

‎src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpKernel/Tests/Debug/TraceableEventDispatcherTest.php
+9-7Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@ class TraceableEventDispatcherTest extends TestCase
2828
public function testStopwatchSections()
2929
{
3030
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch = new Stopwatch());
31-
$kernel = $this->getHttpKernel($dispatcher, function () { return new Response('', 200, ['X-Debug-Token' => '292e1e']); });
31+
$kernel = $this->getHttpKernel($dispatcher);
3232
$request = Request::create('/');
3333
$response = $kernel->handle($request);
3434
$kernel->terminate($request, $response);
3535

36-
$events = $stopwatch->getSectionEvents($response->headers->get('X-Debug-Token'));
36+
$events = $stopwatch->getSectionEvents($request->attributes->get('_stopwatch_token'));
3737
$this->assertEquals([
3838
'__section__',
3939
'kernel.request',
@@ -56,7 +56,7 @@ public function testStopwatchCheckControllerOnRequestEvent()
5656

5757
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
5858

59-
$kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
59+
$kernel = $this->getHttpKernel($dispatcher);
6060
$request = Request::create('/');
6161
$kernel->handle($request);
6262
}
@@ -69,12 +69,12 @@ public function testStopwatchStopControllerOnRequestEvent()
6969
$stopwatch->expects($this->once())
7070
->method('isStarted')
7171
->willReturn(true);
72-
$stopwatch->expects($this->once())
72+
$stopwatch->expects($this->exactly(3))
7373
->method('stop');
7474

7575
$dispatcher = new TraceableEventDispatcher(new EventDispatcher(), $stopwatch);
7676

77-
$kernel = $this->getHttpKernel($dispatcher, function () { return new Response(); });
77+
$kernel = $this->getHttpKernel($dispatcher);
7878
$request = Request::create('/');
7979
$kernel->handle($request);
8080
}
@@ -110,10 +110,12 @@ public function testListenerCanRemoveItselfWhenExecuted()
110110
$this->assertCount(1, $eventDispatcher->getListeners('foo'), 'expected listener1 to be removed');
111111
}
112112

113-
protected function getHttpKernel($dispatcher, $controller)
113+
protected function getHttpKernel($dispatcher)
114114
{
115115
$controllerResolver = $this->createMock(ControllerResolverInterface::class);
116-
$controllerResolver->expects($this->once())->method('getController')->willReturn($controller);
116+
$controllerResolver->expects($this->once())->method('getController')->willReturn(function () {
117+
return new Response();
118+
});
117119
$argumentResolver = $this->createMock(ArgumentResolverInterface::class);
118120
$argumentResolver->expects($this->once())->method('getArguments')->willReturn([]);
119121

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.