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;
21 * @author Fabien Potencier <fabien@symfony.com>
23 class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
28 public function __construct(KernelInterface $kernel = null, $stopwatch = null)
30 $this->kernel = $kernel;
31 $this->stopwatch = $stopwatch;
37 public function collect(Request $request, Response $response, \Exception $exception = null)
39 if (null !== $this->kernel) {
40 $startTime = $this->kernel->getStartTime();
42 $startTime = $request->server->get('REQUEST_TIME_FLOAT', $request->server->get('REQUEST_TIME'));
46 'token' => $response->headers->get('X-Debug-Token'),
47 'start_time' => $startTime * 1000,
55 public function lateCollect()
57 if (null !== $this->stopwatch && isset($this->data['token'])) {
58 $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
60 unset($this->data['token']);
64 * Sets the request events.
66 * @param array $events The request events
68 public function setEvents(array $events)
70 foreach ($events as $event) {
71 $event->ensureStopped();
74 $this->data['events'] = $events;
78 * Gets the request events.
80 * @return array The request events
82 public function getEvents()
84 return $this->data['events'];
88 * Gets the request elapsed time.
90 * @return float The elapsed time
92 public function getDuration()
94 if (!isset($this->data['events']['__section__'])) {
98 $lastEvent = $this->data['events']['__section__'];
100 return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
104 * Gets the initialization time.
106 * This is the time spent until the beginning of the request handling.
108 * @return float The elapsed time
110 public function getInitTime()
112 if (!isset($this->data['events']['__section__'])) {
116 return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
120 * Gets the request time.
122 * @return int The time
124 public function getStartTime()
126 return $this->data['start_time'];
132 public function getName()