Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\HttpKernel\DataCollector; Chris@0: Chris@0: use Symfony\Component\HttpFoundation\Request; Chris@0: use Symfony\Component\HttpFoundation\Response; Chris@0: use Symfony\Component\HttpKernel\KernelInterface; Chris@14: use Symfony\Component\Stopwatch\Stopwatch; Chris@0: Chris@0: /** Chris@0: * TimeDataCollector. Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class TimeDataCollector extends DataCollector implements LateDataCollectorInterface Chris@0: { Chris@0: protected $kernel; Chris@0: protected $stopwatch; Chris@0: Chris@14: public function __construct(KernelInterface $kernel = null, Stopwatch $stopwatch = null) Chris@0: { Chris@0: $this->kernel = $kernel; Chris@0: $this->stopwatch = $stopwatch; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function collect(Request $request, Response $response, \Exception $exception = null) Chris@0: { Chris@0: if (null !== $this->kernel) { Chris@0: $startTime = $this->kernel->getStartTime(); Chris@0: } else { Chris@14: $startTime = $request->server->get('REQUEST_TIME_FLOAT'); Chris@0: } Chris@0: Chris@17: $this->data = [ Chris@0: 'token' => $response->headers->get('X-Debug-Token'), Chris@0: 'start_time' => $startTime * 1000, Chris@17: 'events' => [], Chris@18: 'stopwatch_installed' => \class_exists(Stopwatch::class, false), Chris@17: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@14: public function reset() Chris@14: { Chris@17: $this->data = []; Chris@14: Chris@14: if (null !== $this->stopwatch) { Chris@14: $this->stopwatch->reset(); Chris@14: } Chris@14: } Chris@14: Chris@14: /** Chris@14: * {@inheritdoc} Chris@14: */ Chris@0: public function lateCollect() Chris@0: { Chris@0: if (null !== $this->stopwatch && isset($this->data['token'])) { Chris@0: $this->setEvents($this->stopwatch->getSectionEvents($this->data['token'])); Chris@0: } Chris@0: unset($this->data['token']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the request events. Chris@0: * Chris@0: * @param array $events The request events Chris@0: */ Chris@0: public function setEvents(array $events) Chris@0: { Chris@0: foreach ($events as $event) { Chris@0: $event->ensureStopped(); Chris@0: } Chris@0: Chris@0: $this->data['events'] = $events; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the request events. Chris@0: * Chris@0: * @return array The request events Chris@0: */ Chris@0: public function getEvents() Chris@0: { Chris@0: return $this->data['events']; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the request elapsed time. Chris@0: * Chris@0: * @return float The elapsed time Chris@0: */ Chris@0: public function getDuration() Chris@0: { Chris@0: if (!isset($this->data['events']['__section__'])) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: $lastEvent = $this->data['events']['__section__']; Chris@0: Chris@0: return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the initialization time. Chris@0: * Chris@0: * This is the time spent until the beginning of the request handling. Chris@0: * Chris@0: * @return float The elapsed time Chris@0: */ Chris@0: public function getInitTime() Chris@0: { Chris@0: if (!isset($this->data['events']['__section__'])) { Chris@0: return 0; Chris@0: } Chris@0: Chris@0: return $this->data['events']['__section__']->getOrigin() - $this->getStartTime(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Gets the request time. Chris@0: * Chris@0: * @return int The time Chris@0: */ Chris@0: public function getStartTime() Chris@0: { Chris@0: return $this->data['start_time']; Chris@0: } Chris@0: Chris@0: /** Chris@18: * @return bool whether or not the stopwatch component is installed Chris@18: */ Chris@18: public function isStopwatchInstalled() Chris@18: { Chris@18: return $this->data['stopwatch_installed']; Chris@18: } Chris@18: Chris@18: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function getName() Chris@0: { Chris@0: return 'time'; Chris@0: } Chris@0: }