Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Symfony\Component\HttpKernel\Debug;
13
 
14
use Symfony\Component\EventDispatcher\Debug\TraceableEventDispatcher as BaseTraceableEventDispatcher;
15
use Symfony\Component\HttpKernel\KernelEvents;
16
 
17
/**
18
 * Collects some data about event listeners.
19
 *
20
 * This event dispatcher delegates the dispatching to another one.
21
 *
22
 * @author Fabien Potencier <fabien@symfony.com>
23
 */
24
class TraceableEventDispatcher extends BaseTraceableEventDispatcher
25
{
26
    protected function beforeDispatch(string $eventName, object $event)
27
    {
28
        switch ($eventName) {
29
            case KernelEvents::REQUEST:
30
                $event->getRequest()->attributes->set('_stopwatch_token', substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
31
                $this->stopwatch->openSection();
32
                break;
33
            case KernelEvents::VIEW:
34
            case KernelEvents::RESPONSE:
35
                // stop only if a controller has been executed
36
                if ($this->stopwatch->isStarted('controller')) {
37
                    $this->stopwatch->stop('controller');
38
                }
39
                break;
40
            case KernelEvents::TERMINATE:
41
                $sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
42
                if (null === $sectionId) {
43
                    break;
44
                }
45
                // There is a very special case when using built-in AppCache class as kernel wrapper, in the case
46
                // of an ESI request leading to a `stale` response [B]  inside a `fresh` cached response [A].
47
                // In this case, `$token` contains the [B] debug token, but the  open `stopwatch` section ID
48
                // is equal to the [A] debug token. Trying to reopen section with the [B] token throws an exception
49
                // which must be caught.
50
                try {
51
                    $this->stopwatch->openSection($sectionId);
52
                } catch (\LogicException) {
53
                }
54
                break;
55
        }
56
    }
57
 
58
    protected function afterDispatch(string $eventName, object $event)
59
    {
60
        switch ($eventName) {
61
            case KernelEvents::CONTROLLER_ARGUMENTS:
62
                $this->stopwatch->start('controller', 'section');
63
                break;
64
            case KernelEvents::RESPONSE:
65
                $sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
66
                if (null === $sectionId) {
67
                    break;
68
                }
69
                $this->stopwatch->stopSection($sectionId);
70
                break;
71
            case KernelEvents::TERMINATE:
72
                // In the special case described in the `preDispatch` method above, the `$token` section
73
                // does not exist, then closing it throws an exception which must be caught.
74
                $sectionId = $event->getRequest()->attributes->get('_stopwatch_token');
75
                if (null === $sectionId) {
76
                    break;
77
                }
78
                try {
79
                    $this->stopwatch->stopSection($sectionId);
80
                } catch (\LogicException) {
81
                }
82
                break;
83
        }
84
    }
85
}