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