4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\DataCollector;
14 use Symfony\Component\HttpFoundation\Request;
15 use Symfony\Component\HttpFoundation\Response;
16 use Symfony\Component\HttpKernel\KernelInterface;
17 use Symfony\Component\Stopwatch\Stopwatch;
22 * @author Fabien Potencier <fabien@symfony.com>
24 class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
29 public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null)
31 $this->kernel = $kernel;
32 $this->stopwatch = $stopwatch;
38 public function collect(Request $request, Response $response, \Exception $exception = null)
40 if (null !== $this->kernel) {
41 $startTime = $this->kernel->getStartTime();
43 $startTime = $request->server->get('REQUEST_TIME_FLOAT');
47 'token' => $response->headers->get('X-Debug-Token'),
48 'start_time' => $startTime * 1000,
56 public function reset()
58 $this->data = array();
60 if (null !== $this->stopwatch) {
61 $this->stopwatch->reset();
68 public function lateCollect()
70 if (null !== $this->stopwatch && isset($this->data['token'])) {
71 $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
73 unset($this->data['token']);
77 * Sets the request events.
79 * @param array $events The request events
81 public function setEvents(array $events)
83 foreach ($events as $event) {
84 $event->ensureStopped();
87 $this->data['events'] = $events;
91 * Gets the request events.
93 * @return array The request events
95 public function getEvents()
97 return $this->data['events'];
101 * Gets the request elapsed time.
103 * @return float The elapsed time
105 public function getDuration()
107 if (!isset($this->data['events']['__section__'])) {
111 $lastEvent = $this->data['events']['__section__'];
113 return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
117 * Gets the initialization time.
119 * This is the time spent until the beginning of the request handling.
121 * @return float The elapsed time
123 public function getInitTime()
125 if (!isset($this->data['events']['__section__'])) {
129 return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
133 * Gets the request time.
135 * @return int The time
137 public function getStartTime()
139 return $this->data['start_time'];
145 public function getName()