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