Version 1
[yaffs-website] / web / modules / contrib / devel / webprofiler / src / TraceableEventDispatcher.php
1 <?php
2
3 namespace Drupal\webprofiler;
4
5 use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher;
6 use Symfony\Component\HttpKernel\KernelEvents;
7 use Symfony\Component\EventDispatcher\Event;
8
9 /**
10  * Class TraceableEventDispatcher
11  */
12 class TraceableEventDispatcher extends BaseTraceableEventDispatcher {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected function preDispatch($eventName, Event $event) {
18     switch ($eventName) {
19       case KernelEvents::VIEW:
20       case KernelEvents::RESPONSE:
21         // stop only if a controller has been executed
22         if ($this->stopwatch->isStarted('controller')) {
23           $this->stopwatch->stop('controller');
24         }
25         break;
26     }
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function postDispatch($eventName, Event $event) {
33     switch ($eventName) {
34       case KernelEvents::CONTROLLER:
35         $this->stopwatch->start('controller', 'section');
36         break;
37       case KernelEvents::RESPONSE:
38         $token = $event->getResponse()->headers->get('X-Debug-Token');
39         try {
40           $this->stopwatch->stopSection($token);
41         } catch (\LogicException $e) {
42         }
43         break;
44       case KernelEvents::TERMINATE:
45         // In the special case described in the `preDispatch` method above, the `$token` section
46         // does not exist, then closing it throws an exception which must be caught.
47         $token = $event->getResponse()->headers->get('X-Debug-Token');
48         try {
49           $this->stopwatch->stopSection($token);
50         } catch (\LogicException $e) {
51         }
52         break;
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getListenerPriority($eventName, $listener) {
60     if (!isset($this->listeners[$eventName])) {
61       return;
62     }
63     foreach ($this->listeners[$eventName] as $priority => $listeners) {
64       if (FALSE !== ($key = array_search($listener, $listeners, TRUE))) {
65         return $priority;
66       }
67     }
68   }
69
70 }