vendor/symfony/stopwatch/Section.php line 19

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