Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\Debug;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher;
|
Chris@17
|
15 use Symfony\Component\EventDispatcher\Event;
|
Chris@0
|
16 use Symfony\Component\HttpKernel\KernelEvents;
|
Chris@0
|
17
|
Chris@0
|
18 /**
|
Chris@0
|
19 * Collects some data about event listeners.
|
Chris@0
|
20 *
|
Chris@0
|
21 * This event dispatcher delegates the dispatching to another one.
|
Chris@0
|
22 *
|
Chris@0
|
23 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
24 */
|
Chris@0
|
25 class TraceableEventDispatcher extends BaseTraceableEventDispatcher
|
Chris@0
|
26 {
|
Chris@0
|
27 /**
|
Chris@0
|
28 * {@inheritdoc}
|
Chris@0
|
29 */
|
Chris@0
|
30 protected function preDispatch($eventName, Event $event)
|
Chris@0
|
31 {
|
Chris@0
|
32 switch ($eventName) {
|
Chris@0
|
33 case KernelEvents::REQUEST:
|
Chris@0
|
34 $this->stopwatch->openSection();
|
Chris@0
|
35 break;
|
Chris@0
|
36 case KernelEvents::VIEW:
|
Chris@0
|
37 case KernelEvents::RESPONSE:
|
Chris@0
|
38 // stop only if a controller has been executed
|
Chris@0
|
39 if ($this->stopwatch->isStarted('controller')) {
|
Chris@0
|
40 $this->stopwatch->stop('controller');
|
Chris@0
|
41 }
|
Chris@0
|
42 break;
|
Chris@0
|
43 case KernelEvents::TERMINATE:
|
Chris@0
|
44 $token = $event->getResponse()->headers->get('X-Debug-Token');
|
Chris@0
|
45 // There is a very special case when using built-in AppCache class as kernel wrapper, in the case
|
Chris@0
|
46 // of an ESI request leading to a `stale` response [B] inside a `fresh` cached response [A].
|
Chris@0
|
47 // In this case, `$token` contains the [B] debug token, but the open `stopwatch` section ID
|
Chris@0
|
48 // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
|
Chris@0
|
49 // which must be caught.
|
Chris@0
|
50 try {
|
Chris@0
|
51 $this->stopwatch->openSection($token);
|
Chris@0
|
52 } catch (\LogicException $e) {
|
Chris@0
|
53 }
|
Chris@0
|
54 break;
|
Chris@0
|
55 }
|
Chris@0
|
56 }
|
Chris@0
|
57
|
Chris@0
|
58 /**
|
Chris@0
|
59 * {@inheritdoc}
|
Chris@0
|
60 */
|
Chris@0
|
61 protected function postDispatch($eventName, Event $event)
|
Chris@0
|
62 {
|
Chris@0
|
63 switch ($eventName) {
|
Chris@0
|
64 case KernelEvents::CONTROLLER_ARGUMENTS:
|
Chris@0
|
65 $this->stopwatch->start('controller', 'section');
|
Chris@0
|
66 break;
|
Chris@0
|
67 case KernelEvents::RESPONSE:
|
Chris@0
|
68 $token = $event->getResponse()->headers->get('X-Debug-Token');
|
Chris@0
|
69 $this->stopwatch->stopSection($token);
|
Chris@0
|
70 break;
|
Chris@0
|
71 case KernelEvents::TERMINATE:
|
Chris@0
|
72 // In the special case described in the `preDispatch` method above, the `$token` section
|
Chris@0
|
73 // does not exist, then closing it throws an exception which must be caught.
|
Chris@0
|
74 $token = $event->getResponse()->headers->get('X-Debug-Token');
|
Chris@0
|
75 try {
|
Chris@0
|
76 $this->stopwatch->stopSection($token);
|
Chris@0
|
77 } catch (\LogicException $e) {
|
Chris@0
|
78 }
|
Chris@0
|
79 break;
|
Chris@0
|
80 }
|
Chris@0
|
81 }
|
Chris@0
|
82 }
|