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