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