vendor/symfony/stopwatch/Stopwatch.php line 99

  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\Stopwatch;
  11. use Symfony\Contracts\Service\ResetInterface;
  12. // Help opcache.preload discover always-needed symbols
  13. class_exists(Section::class);
  14. /**
  15.  * Stopwatch provides a way to profile code.
  16.  *
  17.  * @author Fabien Potencier <fabien@symfony.com>
  18.  */
  19. class Stopwatch implements ResetInterface
  20. {
  21.     private bool $morePrecision;
  22.     /**
  23.      * @var Section[]
  24.      */
  25.     private array $sections;
  26.     /**
  27.      * @var Section[]
  28.      */
  29.     private array $activeSections;
  30.     /**
  31.      * @param bool $morePrecision If true, time is stored as float to keep the original microsecond precision
  32.      */
  33.     public function __construct(bool $morePrecision false)
  34.     {
  35.         $this->morePrecision $morePrecision;
  36.         $this->reset();
  37.     }
  38.     /**
  39.      * @return Section[]
  40.      */
  41.     public function getSections(): array
  42.     {
  43.         return $this->sections;
  44.     }
  45.     /**
  46.      * Creates a new section or re-opens an existing section.
  47.      *
  48.      * @param string|null $id The id of the session to re-open, null to create a new one
  49.      *
  50.      * @throws \LogicException When the section to re-open is not reachable
  51.      */
  52.     public function openSection(string $id null)
  53.     {
  54.         $current end($this->activeSections);
  55.         if (null !== $id && null === $current->get($id)) {
  56.             throw new \LogicException(sprintf('The section "%s" has been started at an other level and cannot be opened.'$id));
  57.         }
  58.         $this->start('__section__.child''section');
  59.         $this->activeSections[] = $current->open($id);
  60.         $this->start('__section__');
  61.     }
  62.     /**
  63.      * Stops the last started section.
  64.      *
  65.      * The id parameter is used to retrieve the events from this section.
  66.      *
  67.      * @see getSectionEvents()
  68.      *
  69.      * @throws \LogicException When there's no started section to be stopped
  70.      */
  71.     public function stopSection(string $id)
  72.     {
  73.         $this->stop('__section__');
  74.         if (== \count($this->activeSections)) {
  75.             throw new \LogicException('There is no started section to stop.');
  76.         }
  77.         $this->sections[$id] = array_pop($this->activeSections)->setId($id);
  78.         $this->stop('__section__.child');
  79.     }
  80.     /**
  81.      * Starts an event.
  82.      */
  83.     public function start(string $namestring $category null): StopwatchEvent
  84.     {
  85.         return end($this->activeSections)->startEvent($name$category);
  86.     }
  87.     /**
  88.      * Checks if the event was started.
  89.      */
  90.     public function isStarted(string $name): bool
  91.     {
  92.         return end($this->activeSections)->isEventStarted($name);
  93.     }
  94.     /**
  95.      * Stops an event.
  96.      */
  97.     public function stop(string $name): StopwatchEvent
  98.     {
  99.         return end($this->activeSections)->stopEvent($name);
  100.     }
  101.     /**
  102.      * Stops then restarts an event.
  103.      */
  104.     public function lap(string $name): StopwatchEvent
  105.     {
  106.         return end($this->activeSections)->stopEvent($name)->start();
  107.     }
  108.     /**
  109.      * Returns a specific event by name.
  110.      */
  111.     public function getEvent(string $name): StopwatchEvent
  112.     {
  113.         return end($this->activeSections)->getEvent($name);
  114.     }
  115.     /**
  116.      * Gets all events for a given section.
  117.      *
  118.      * @return StopwatchEvent[]
  119.      */
  120.     public function getSectionEvents(string $id): array
  121.     {
  122.         return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : [];
  123.     }
  124.     /**
  125.      * Resets the stopwatch to its original state.
  126.      */
  127.     public function reset()
  128.     {
  129.         $this->sections $this->activeSections = ['__root__' => new Section(null$this->morePrecision)];
  130.     }
  131. }