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