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