8000 always close open stopwatch section after handling kernel.request events · symfony/symfony@46e5d3b · GitHub
[go: up one dir, main page]

Skip to content

Commit 46e5d3b

Browse files
committed
always close open stopwatch section after handling kernel.request events
1 parent a239f68 commit 46e5d3b

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

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

Lines 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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ 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);

0 commit comments

Comments
 (0)
0