Section.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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 = null, bool $morePrecision = false)
  43. {
  44. $this->origin = $origin;
  45. $this->morePrecision = $morePrecision;
  46. }
  47. /**
  48. * Returns the child section.
  49. *
  50. * @param string $id The child section identifier
  51. *
  52. * @return self|null The child section or null when none found
  53. */
  54. public function get($id)
  55. {
  56. if (null === $id) {
  57. @trigger_error(sprintf('Passing "null" as the first argument of the "%s()" method is deprecated since Symfony 4.4, pass a valid child section identifier instead.', __METHOD__), E_USER_DEPRECATED);
  58. }
  59. foreach ($this->children as $child) {
  60. if ($id === $child->getId()) {
  61. return $child;
  62. }
  63. }
  64. return null;
  65. }
  66. /**
  67. * Creates or re-opens a child section.
  68. *
  69. * @param string|null $id Null to create a new section, the identifier to re-open an existing one
  70. *
  71. * @return self
  72. */
  73. public function open($id)
  74. {
  75. if (null === $id || null === $session = $this->get($id)) {
  76. $session = $this->children[] = new self(microtime(true) * 1000, $this->morePrecision);
  77. }
  78. return $session;
  79. }
  80. /**
  81. * @return string The identifier of the section
  82. */
  83. public function getId()
  84. {
  85. return $this->id;
  86. }
  87. /**
  88. * Sets the session identifier.
  89. *
  90. * @param string $id The session identifier
  91. *
  92. * @return $this
  93. */
  94. public function setId($id)
  95. {
  96. $this->id = $id;
  97. return $this;
  98. }
  99. /**
  100. * Starts an event.
  101. *
  102. * @param string $name The event name
  103. * @param string|null $category The event category
  104. *
  105. * @return StopwatchEvent The event
  106. */
  107. public function startEvent($name, $category)
  108. {
  109. if (!isset($this->events[$name])) {
  110. $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category, $this->morePrecision);
  111. }
  112. return $this->events[$name]->start();
  113. }
  114. /**
  115. * Checks if the event was started.
  116. *
  117. * @param string $name The event name
  118. *
  119. * @return bool
  120. */
  121. public function isEventStarted($name)
  122. {
  123. return isset($this->events[$name]) && $this->events[$name]->isStarted();
  124. }
  125. /**
  126. * Stops an event.
  127. *
  128. * @param string $name The event name
  129. *
  130. * @return StopwatchEvent The event
  131. *
  132. * @throws \LogicException When the event has not been started
  133. */
  134. public function stopEvent($name)
  135. {
  136. if (!isset($this->events[$name])) {
  137. throw new \LogicException(sprintf('Event "%s" is not started.', $name));
  138. }
  139. return $this->events[$name]->stop();
  140. }
  141. /**
  142. * Stops then restarts an event.
  143. *
  144. * @param string $name The event name
  145. *
  146. * @return StopwatchEvent The event
  147. *
  148. * @throws \LogicException When the event has not been started
  149. */
  150. public function lap($name)
  151. {
  152. return $this->stopEvent($name)->start();
  153. }
  154. /**
  155. * Returns a specific event by name.
  156. *
  157. * @param string $name The event name
  158. *
  159. * @return StopwatchEvent The event
  160. *
  161. * @throws \LogicException When the event is not known
  162. */
  163. public function getEvent($name)
  164. {
  165. if (!isset($this->events[$name])) {
  166. throw new \LogicException(sprintf('Event "%s" is not known.', $name));
  167. }
  168. return $this->events[$name];
  169. }
  170. /**
  171. * Returns the events from this section.
  172. *
  173. * @return StopwatchEvent[] An array of StopwatchEvent instances
  174. */
  175. public function getEvents()
  176. {
  177. return $this->events;
  178. }
  179. }