vendor/symfony/stopwatch/Section.php line 39

  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. /**
  12.  * Stopwatch section.
  13.  *
  14.  * @author Fabien Potencier <fabien@symfony.com>
  15.  */
  16. class Section
  17. {
  18.     /**
  19.      * @var StopwatchEvent[]
  20.      */
  21.     private array $events = [];
  22.     private ?float $origin;
  23.     private bool $morePrecision;
  24.     private ?string $id null;
  25.     /**
  26.      * @var Section[]
  27.      */
  28.     private array $children = [];
  29.     /**
  30.      * @param float|null $origin        Set the origin of the events in this section, use null to set their origin to their start time
  31.      * @param bool       $morePrecision If true, time is stored as float to keep the original microsecond precision
  32.      */
  33.     public function __construct(float $origin nullbool $morePrecision false)
  34.     {
  35.         $this->origin $origin;
  36.         $this->morePrecision $morePrecision;
  37.     }
  38.     /**
  39.      * Returns the child section.
  40.      */
  41.     public function get(string $id): ?self
  42.     {
  43.         foreach ($this->children as $child) {
  44.             if ($id === $child->getId()) {
  45.                 return $child;
  46.             }
  47.         }
  48.         return null;
  49.     }
  50.     /**
  51.      * Creates or re-opens a child section.
  52.      *
  53.      * @param string|null $id Null to create a new section, the identifier to re-open an existing one
  54.      */
  55.     public function open(?string $id): self
  56.     {
  57.         if (null === $id || null === $session $this->get($id)) {
  58.             $session $this->children[] = new self(microtime(true) * 1000$this->morePrecision);
  59.         }
  60.         return $session;
  61.     }
  62.     public function getId(): ?string
  63.     {
  64.         return $this->id;
  65.     }
  66.     /**
  67.      * Sets the session identifier.
  68.      *
  69.      * @return $this
  70.      */
  71.     public function setId(string $id): static
  72.     {
  73.         $this->id $id;
  74.         return $this;
  75.     }
  76.     /**
  77.      * Starts an event.
  78.      */
  79.     public function startEvent(string $name, ?string $category): StopwatchEvent
  80.     {
  81.         if (!isset($this->events[$name])) {
  82.             $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000$category$this->morePrecision$name);
  83.         }
  84.         return $this->events[$name]->start();
  85.     }
  86.     /**
  87.      * Checks if the event was started.
  88.      */
  89.     public function isEventStarted(string $name): bool
  90.     {
  91.         return isset($this->events[$name]) && $this->events[$name]->isStarted();
  92.     }
  93.     /**
  94.      * Stops an event.
  95.      *
  96.      * @throws \LogicException When the event has not been started
  97.      */
  98.     public function stopEvent(string $name): StopwatchEvent
  99.     {
  100.         if (!isset($this->events[$name])) {
  101.             throw new \LogicException(sprintf('Event "%s" is not started.'$name));
  102.         }
  103.         return $this->events[$name]->stop();
  104.     }
  105.     /**
  106.      * Stops then restarts an event.
  107.      *
  108.      * @throws \LogicException When the event has not been started
  109.      */
  110.     public function lap(string $name): StopwatchEvent
  111.     {
  112.         return $this->stopEvent($name)->start();
  113.     }
  114.     /**
  115.      * Returns a specific event by name.
  116.      *
  117.      * @throws \LogicException When the event is not known
  118.      */
  119.     public function getEvent(string $name): StopwatchEvent
  120.     {
  121.         if (!isset($this->events[$name])) {
  122.             throw new \LogicException(sprintf('Event "%s" is not known.'$name));
  123.         }
  124.         return $this->events[$name];
  125.     }
  126.     /**
  127.      * Returns the events from this section.
  128.      *
  129.      * @return StopwatchEvent[]
  130.      */
  131.     public function getEvents(): array
  132.     {
  133.         return $this->events;
  134.     }
  135. }