Subversion-Projekte lars-tiefland.laravel_shop

Revision

Revision 148 | Details | Vergleich mit vorheriger | 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;
1663 lars 34
        $this->data = ['events' => [], 'stopwatch_installed' => false, 'start_time' => 0];
148 lars 35
    }
36
 
37
    public function collect(Request $request, Response $response, \Throwable $exception = null)
38
    {
39
        if (null !== $this->kernel) {
40
            $startTime = $this->kernel->getStartTime();
41
        } else {
42
            $startTime = $request->server->get('REQUEST_TIME_FLOAT');
43
        }
44
 
45
        $this->data = [
46
            'token' => $request->attributes->get('_stopwatch_token'),
47
            'start_time' => $startTime * 1000,
48
            'events' => [],
49
            'stopwatch_installed' => class_exists(Stopwatch::class, false),
50
        ];
51
    }
52
 
53
    public function reset()
54
    {
1663 lars 55
        $this->data = ['events' => [], 'stopwatch_installed' => false, 'start_time' => 0];
148 lars 56
 
57
        $this->stopwatch?->reset();
58
    }
59
 
60
    public function lateCollect()
61
    {
62
        if (null !== $this->stopwatch && isset($this->data['token'])) {
63
            $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
64
        }
65
        unset($this->data['token']);
66
    }
67
 
68
    /**
69
     * @param StopwatchEvent[] $events The request events
70
     */
71
    public function setEvents(array $events)
72
    {
73
        foreach ($events as $event) {
74
            $event->ensureStopped();
75
        }
76
 
77
        $this->data['events'] = $events;
78
    }
79
 
80
    /**
81
     * @return StopwatchEvent[]
82
     */
83
    public function getEvents(): array
84
    {
85
        return $this->data['events'];
86
    }
87
 
88
    /**
89
     * Gets the request elapsed time.
90
     */
91
    public function getDuration(): float
92
    {
93
        if (!isset($this->data['events']['__section__'])) {
94
            return 0;
95
        }
96
 
97
        $lastEvent = $this->data['events']['__section__'];
98
 
99
        return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
100
    }
101
 
102
    /**
103
     * Gets the initialization time.
104
     *
105
     * This is the time spent until the beginning of the request handling.
106
     */
107
    public function getInitTime(): float
108
    {
109
        if (!isset($this->data['events']['__section__'])) {
110
            return 0;
111
        }
112
 
113
        return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
114
    }
115
 
116
    public function getStartTime(): float
117
    {
118
        return $this->data['start_time'];
119
    }
120
 
121
    public function isStopwatchInstalled(): bool
122
    {
123
        return $this->data['stopwatch_installed'];
124
    }
125
 
126
    public function getName(): string
127
    {
128
        return 'time';
129
    }
130
}