vendor/symfony/stopwatch/Stopwatch.php line 24

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