Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 /*
|
Chris@0
|
4 * This file is part of the Symfony package.
|
Chris@0
|
5 *
|
Chris@0
|
6 * (c) Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
7 *
|
Chris@0
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@0
|
9 * file that was distributed with this source code.
|
Chris@0
|
10 */
|
Chris@0
|
11
|
Chris@0
|
12 namespace Symfony\Component\HttpKernel\DataCollector;
|
Chris@0
|
13
|
Chris@0
|
14 use Symfony\Component\HttpFoundation\Request;
|
Chris@0
|
15 use Symfony\Component\HttpFoundation\Response;
|
Chris@0
|
16 use Symfony\Component\HttpKernel\KernelInterface;
|
Chris@14
|
17 use Symfony\Component\Stopwatch\Stopwatch;
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * TimeDataCollector.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @author Fabien Potencier <fabien@symfony.com>
|
Chris@0
|
23 */
|
Chris@0
|
24 class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
|
Chris@0
|
25 {
|
Chris@0
|
26 protected $kernel;
|
Chris@0
|
27 protected $stopwatch;
|
Chris@0
|
28
|
Chris@14
|
29 public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
|
Chris@0
|
30 {
|
Chris@0
|
31 $this->kernel = $kernel;
|
Chris@0
|
32 $this->stopwatch = $stopwatch;
|
Chris@0
|
33 }
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * {@inheritdoc}
|
Chris@0
|
37 */
|
Chris@0
|
38 public function collect(Request $request, Response $response, \Exception $exception = null)
|
Chris@0
|
39 {
|
Chris@0
|
40 if (null !== $this->kernel) {
|
Chris@0
|
41 $startTime = $this->kernel->getStartTime();
|
Chris@0
|
42 } else {
|
Chris@14
|
43 $startTime = $request->server->get('REQUEST_TIME_FLOAT');
|
Chris@0
|
44 }
|
Chris@0
|
45
|
Chris@17
|
46 $this->data = [
|
Chris@0
|
47 'token' => $response->headers->get('X-Debug-Token'),
|
Chris@0
|
48 'start_time' => $startTime * 1000,
|
Chris@17
|
49 'events' => [],
|
Chris@18
|
50 'stopwatch_installed' => \class_exists(Stopwatch::class, false),
|
Chris@17
|
51 ];
|
Chris@0
|
52 }
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * {@inheritdoc}
|
Chris@0
|
56 */
|
Chris@14
|
57 public function reset()
|
Chris@14
|
58 {
|
Chris@17
|
59 $this->data = [];
|
Chris@14
|
60
|
Chris@14
|
61 if (null !== $this->stopwatch) {
|
Chris@14
|
62 $this->stopwatch->reset();
|
Chris@14
|
63 }
|
Chris@14
|
64 }
|
Chris@14
|
65
|
Chris@14
|
66 /**
|
Chris@14
|
67 * {@inheritdoc}
|
Chris@14
|
68 */
|
Chris@0
|
69 public function lateCollect()
|
Chris@0
|
70 {
|
Chris@0
|
71 if (null !== $this->stopwatch && isset($this->data['token'])) {
|
Chris@0
|
72 $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
|
Chris@0
|
73 }
|
Chris@0
|
74 unset($this->data['token']);
|
Chris@0
|
75 }
|
Chris@0
|
76
|
Chris@0
|
77 /**
|
Chris@0
|
78 * Sets the request events.
|
Chris@0
|
79 *
|
Chris@0
|
80 * @param array $events The request events
|
Chris@0
|
81 */
|
Chris@0
|
82 public function setEvents(array $events)
|
Chris@0
|
83 {
|
Chris@0
|
84 foreach ($events as $event) {
|
Chris@0
|
85 $event->ensureStopped();
|
Chris@0
|
86 }
|
Chris@0
|
87
|
Chris@0
|
88 $this->data['events'] = $events;
|
Chris@0
|
89 }
|
Chris@0
|
90
|
Chris@0
|
91 /**
|
Chris@0
|
92 * Gets the request events.
|
Chris@0
|
93 *
|
Chris@0
|
94 * @return array The request events
|
Chris@0
|
95 */
|
Chris@0
|
96 public function getEvents()
|
Chris@0
|
97 {
|
Chris@0
|
98 return $this->data['events'];
|
Chris@0
|
99 }
|
Chris@0
|
100
|
Chris@0
|
101 /**
|
Chris@0
|
102 * Gets the request elapsed time.
|
Chris@0
|
103 *
|
Chris@0
|
104 * @return float The elapsed time
|
Chris@0
|
105 */
|
Chris@0
|
106 public function getDuration()
|
Chris@0
|
107 {
|
Chris@0
|
108 if (!isset($this->data['events']['__section__'])) {
|
Chris@0
|
109 return 0;
|
Chris@0
|
110 }
|
Chris@0
|
111
|
Chris@0
|
112 $lastEvent = $this->data['events']['__section__'];
|
Chris@0
|
113
|
Chris@0
|
114 return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
|
Chris@0
|
115 }
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@0
|
118 * Gets the initialization time.
|
Chris@0
|
119 *
|
Chris@0
|
120 * This is the time spent until the beginning of the request handling.
|
Chris@0
|
121 *
|
Chris@0
|
122 * @return float The elapsed time
|
Chris@0
|
123 */
|
Chris@0
|
124 public function getInitTime()
|
Chris@0
|
125 {
|
Chris@0
|
126 if (!isset($this->data['events']['__section__'])) {
|
Chris@0
|
127 return 0;
|
Chris@0
|
128 }
|
Chris@0
|
129
|
Chris@0
|
130 return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
|
Chris@0
|
131 }
|
Chris@0
|
132
|
Chris@0
|
133 /**
|
Chris@0
|
134 * Gets the request time.
|
Chris@0
|
135 *
|
Chris@0
|
136 * @return int The time
|
Chris@0
|
137 */
|
Chris@0
|
138 public function getStartTime()
|
Chris@0
|
139 {
|
Chris@0
|
140 return $this->data['start_time'];
|
Chris@0
|
141 }
|
Chris@0
|
142
|
Chris@0
|
143 /**
|
Chris@18
|
144 * @return bool whether or not the stopwatch component is installed
|
Chris@18
|
145 */
|
Chris@18
|
146 public function isStopwatchInstalled()
|
Chris@18
|
147 {
|
Chris@18
|
148 return $this->data['stopwatch_installed'];
|
Chris@18
|
149 }
|
Chris@18
|
150
|
Chris@18
|
151 /**
|
Chris@0
|
152 * {@inheritdoc}
|
Chris@0
|
153 */
|
Chris@0
|
154 public function getName()
|
Chris@0
|
155 {
|
Chris@0
|
156 return 'time';
|
Chris@0
|
157 }
|
Chris@0
|
158 }
|