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