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