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@17
|
50 ];
|
Chris@0
|
51 }
|
Chris@0
|
52
|
Chris@0
|
53 /**
|
Chris@0
|
54 * {@inheritdoc}
|
Chris@0
|
55 */
|
Chris@14
|
56 public function reset()
|
Chris@14
|
57 {
|
Chris@17
|
58 $this->data = [];
|
Chris@14
|
59
|
Chris@14
|
60 if (null !== $this->stopwatch) {
|
Chris@14
|
61 $this->stopwatch->reset();
|
Chris@14
|
62 }
|
Chris@14
|
63 }
|
Chris@14
|
64
|
Chris@14
|
65 /**
|
Chris@14
|
66 * {@inheritdoc}
|
Chris@14
|
67 */
|
Chris@0
|
68 public function lateCollect()
|
Chris@0
|
69 {
|
Chris@0
|
70 if (null !== $this->stopwatch && isset($this->data['token'])) {
|
Chris@0
|
71 $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
|
Chris@0
|
72 }
|
Chris@0
|
73 unset($this->data['token']);
|
Chris@0
|
74 }
|
Chris@0
|
75
|
Chris@0
|
76 /**
|
Chris@0
|
77 * Sets the request events.
|
Chris@0
|
78 *
|
Chris@0
|
79 * @param array $events The request events
|
Chris@0
|
80 */
|
Chris@0
|
81 public function setEvents(array $events)
|
Chris@0
|
82 {
|
Chris@0
|
83 foreach ($events as $event) {
|
Chris@0
|
84 $event->ensureStopped();
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 $this->data['events'] = $events;
|
Chris@0
|
88 }
|
Chris@0
|
89
|
Chris@0
|
90 /**
|
Chris@0
|
91 * Gets the request events.
|
Chris@0
|
92 *
|
Chris@0
|
93 * @return array The request events
|
Chris@0
|
94 */
|
Chris@0
|
95 public function getEvents()
|
Chris@0
|
96 {
|
Chris@0
|
97 return $this->data['events'];
|
Chris@0
|
98 }
|
Chris@0
|
99
|
Chris@0
|
100 /**
|
Chris@0
|
101 * Gets the request elapsed time.
|
Chris@0
|
102 *
|
Chris@0
|
103 * @return float The elapsed time
|
Chris@0
|
104 */
|
Chris@0
|
105 public function getDuration()
|
Chris@0
|
106 {
|
Chris@0
|
107 if (!isset($this->data['events']['__section__'])) {
|
Chris@0
|
108 return 0;
|
Chris@0
|
109 }
|
Chris@0
|
110
|
Chris@0
|
111 $lastEvent = $this->data['events']['__section__'];
|
Chris@0
|
112
|
Chris@0
|
113 return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Gets the initialization time.
|
Chris@0
|
118 *
|
Chris@0
|
119 * This is the time spent until the beginning of the request handling.
|
Chris@0
|
120 *
|
Chris@0
|
121 * @return float The elapsed time
|
Chris@0
|
122 */
|
Chris@0
|
123 public function getInitTime()
|
Chris@0
|
124 {
|
Chris@0
|
125 if (!isset($this->data['events']['__section__'])) {
|
Chris@0
|
126 return 0;
|
Chris@0
|
127 }
|
Chris@0
|
128
|
Chris@0
|
129 return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Gets the request time.
|
Chris@0
|
134 *
|
Chris@0
|
135 * @return int The time
|
Chris@0
|
136 */
|
Chris@0
|
137 public function getStartTime()
|
Chris@0
|
138 {
|
Chris@0
|
139 return $this->data['start_time'];
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * {@inheritdoc}
|
Chris@0
|
144 */
|
Chris@0
|
145 public function getName()
|
Chris@0
|
146 {
|
Chris@0
|
147 return 'time';
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|