Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen 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\DataCollector;
13
 
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
use Symfony\Component\HttpKernel\KernelInterface;
17
use Symfony\Component\Stopwatch\Stopwatch;
18
use Symfony\Component\Stopwatch\StopwatchEvent;
19
 
20
/**
21
 * @author Fabien Potencier <fabien@symfony.com>
22
 *
23
 * @final
24
 */
25
class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
26
{
27
    private ?KernelInterface $kernel;
28
    private ?Stopwatch $stopwatch;
29
 
30
    public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
31
    {
32
        $this->kernel = $kernel;
33
        $this->stopwatch = $stopwatch;
34
    }
35
 
36
    public function collect(Request $request, Response $response, \Throwable $exception = null)
37
    {
38
        if (null !== $this->kernel) {
39
            $startTime = $this->kernel->getStartTime();
40
        } else {
41
            $startTime = $request->server->get('REQUEST_TIME_FLOAT');
42
        }
43
 
44
        $this->data = [
45
            'token' => $request->attributes->get('_stopwatch_token'),
46
            'start_time' => $startTime * 1000,
47
            'events' => [],
48
            'stopwatch_installed' => class_exists(Stopwatch::class, false),
49
        ];
50
    }
51
 
52
    public function reset()
53
    {
54
        $this->data = [];
55
 
56
        $this->stopwatch?->reset();
57
    }
58
 
59
    public function lateCollect()
60
    {
61
        if (null !== $this->stopwatch && isset($this->data['token'])) {
62
            $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
63
        }
64
        unset($this->data['token']);
65
    }
66
 
67
    /**
68
     * @param StopwatchEvent[] $events The request events
69
     */
70
    public function setEvents(array $events)
71
    {
72
        foreach ($events as $event) {
73
            $event->ensureStopped();
74
        }
75
 
76
        $this->data['events'] = $events;
77
    }
78
 
79
    /**
80
     * @return StopwatchEvent[]
81
     */
82
    public function getEvents(): array
83
    {
84
        return $this->data['events'];
85
    }
86
 
87
    /**
88
     * Gets the request elapsed time.
89
     */
90
    public function getDuration(): float
91
    {
92
        if (!isset($this->data['events']['__section__'])) {
93
            return 0;
94
        }
95
 
96
        $lastEvent = $this->data['events']['__section__'];
97
 
98
        return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
99
    }
100
 
101
    /**
102
     * Gets the initialization time.
103
     *
104
     * This is the time spent until the beginning of the request handling.
105
     */
106
    public function getInitTime(): float
107
    {
108
        if (!isset($this->data['events']['__section__'])) {
109
            return 0;
110
        }
111
 
112
        return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
113
    }
114
 
115
    public function getStartTime(): float
116
    {
117
        return $this->data['start_time'];
118
    }
119
 
120
    public function isStopwatchInstalled(): bool
121
    {
122
        return $this->data['stopwatch_installed'];
123
    }
124
 
125
    public function getName(): string
126
    {
127
        return 'time';
128
    }
129
}