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

  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 ?KernelInterface $kernel;
  24.     private ?Stopwatch $stopwatch;
  25.     public function __construct(KernelInterface $kernel nullStopwatch $stopwatch null)
  26.     {
  27.         $this->kernel $kernel;
  28.         $this->stopwatch $stopwatch;
  29.         $this->data = ['events' => [], 'stopwatch_installed' => false'start_time' => 0];
  30.     }
  31.     public function collect(Request $requestResponse $response\Throwable $exception null)
  32.     {
  33.         if (null !== $this->kernel) {
  34.             $startTime $this->kernel->getStartTime();
  35.         } else {
  36.             $startTime $request->server->get('REQUEST_TIME_FLOAT');
  37.         }
  38.         $this->data = [
  39.             'token' => $request->attributes->get('_stopwatch_token'),
  40.             'start_time' => $startTime 1000,
  41.             'events' => [],
  42.             'stopwatch_installed' => class_exists(Stopwatch::class, false),
  43.         ];
  44.     }
  45.     public function reset()
  46.     {
  47.         $this->data = ['events' => [], 'stopwatch_installed' => false'start_time' => 0];
  48.         $this->stopwatch?->reset();
  49.     }
  50.     public function lateCollect()
  51.     {
  52.         if (null !== $this->stopwatch && isset($this->data['token'])) {
  53.             $this->setEvents($this->stopwatch->getSectionEvents($this->data['token']));
  54.         }
  55.         unset($this->data['token']);
  56.     }
  57.     /**
  58.      * @param StopwatchEvent[] $events The request events
  59.      */
  60.     public function setEvents(array $events)
  61.     {
  62.         foreach ($events as $event) {
  63.             $event->ensureStopped();
  64.         }
  65.         $this->data['events'] = $events;
  66.     }
  67.     /**
  68.      * @return StopwatchEvent[]
  69.      */
  70.     public function getEvents(): array
  71.     {
  72.         return $this->data['events'];
  73.     }
  74.     /**
  75.      * Gets the request elapsed time.
  76.      */
  77.     public function getDuration(): float
  78.     {
  79.         if (!isset($this->data['events']['__section__'])) {
  80.             return 0;
  81.         }
  82.         $lastEvent $this->data['events']['__section__'];
  83.         return $lastEvent->getOrigin() + $lastEvent->getDuration() - $this->getStartTime();
  84.     }
  85.     /**
  86.      * Gets the initialization time.
  87.      *
  88.      * This is the time spent until the beginning of the request handling.
  89.      */
  90.     public function getInitTime(): float
  91.     {
  92.         if (!isset($this->data['events']['__section__'])) {
  93.             return 0;
  94.         }
  95.         return $this->data['events']['__section__']->getOrigin() - $this->getStartTime();
  96.     }
  97.     public function getStartTime(): float
  98.     {
  99.         return $this->data['start_time'];
  100.     }
  101.     public function isStopwatchInstalled(): bool
  102.     {
  103.         return $this->data['stopwatch_installed'];
  104.     }
  105.     public function getName(): string
  106.     {
  107.         return 'time';
  108.     }
  109. }