vendor/symfony/http-kernel/DataCollector/TimeDataCollector.php line 39

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\HttpKernel\DataCollector;
  11. use Symfony\Component\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\KernelInterface;
  14. use Symfony\Component\Stopwatch\Stopwatch;
  15. use Symfony\Component\Stopwatch\StopwatchEvent;
  16. /**
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  *
  19.  * @final
  20.  */
  21. class TimeDataCollector extends DataCollector implements LateDataCollectorInterface
  22. {
  23.     private $kernel;
  24.     private $stopwatch;
  25.     public function __construct(KernelInterface $kernel nullStopwatch $stopwatch null)
  26.     {
  27.         $this->kernel $kernel;
  28.         $this->stopwatch $stopwatch;
  29.     }
  30.     /**
  31.      * {@inheritdoc}
  32.      */
  33.     public function collect(Request $requestResponse $response\Throwable $exception null)
  34.     {
  35.         if (null !== $this->kernel) {
  36.             $startTime $this->kernel->getStartTime();
  37.         } else {
  38.             $startTime $request->server->get('REQUEST_TIME_FLOAT');
  39.         }
  40.         $this->data = [
  41.             'token' => $request->attributes->get('_stopwatch_token'),
  42.             'start_time' => $startTime 1000,
  43.             'events' => [],
  44.             'stopwatch_installed' => class_exists(Stopwatch::class, false),
  45.         ];
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function reset()
  51.     {
  52.         $this->data = [];
  53.         if (null !== $this->stopwatch) {
  54.             $this->stopwatch->reset();
  55.         }
  56.     }
  57.     /**
  58.      * {@inheritdoc}
  59.      */
  60.     public function lateCollect()
  61.     {
  62.         if (null !== $this->stopwatch && isset($this->data['token'])) {
  63.             $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
  64.         }
  65.         unset($this->data['token']);
  66.     }
  67.     /**
  68.      * @param StopwatchEvent[] $events The request events
  69.      */
  70.     public function setEvents(array $events)
  71.     {
  72.         foreach ($events as $event) {
  73.             $event->ensureStopped();
  74.         }
  75.         $this->data['events'] = $events;
  76.     }
  77.     /**
  78.      * @return StopwatchEvent[]
  79.      */
  80.     public function getEvents(): array
  81.     {
  82.         return $this->data['events'];
  83.     }
  84.     /**
  85.      * Gets the request elapsed time.
  86.      */
  87.     public function getDuration(): float
  88.     {
  89.         if (!isset($this->data['events']['__section__'])) {
  90.             return 0;
  91.         }
  92.         $lastEvent $this->data['events']['__section__'];
  93.         return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
  94.     }
  95.     /**
  96.      * Gets the initialization time.
  97.      *
  98.      * This is the time spent until the beginning of the request handling.
  99.      */
  100.     public function getInitTime(): float
  101.     {
  102.         if (!isset($this->data['events']['__section__'])) {
  103.             return 0;
  104.         }
  105.         return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
  106.     }
  107.     public function getStartTime(): float
  108.     {
  109.         return $this->data['start_time'];
  110.     }
  111.     public function isStopwatchInstalled(): bool
  112.     {
  113.         return $this->data['stopwatch_installed'];
  114.     }
  115.     /**
  116.      * {@inheritdoc}
  117.      */
  118.     public function getName(): string
  119.     {
  120.         return 'time';
  121.     }
  122. }