/src/Symfony/Component/Stopwatch/Section.php

https://github.com/deviantintegral/symfony · PHP · 198 lines · 72 code · 23 blank · 103 comment · 6 complexity · f9ad068dd0678058efe2e4d66f505f09 MD5 · raw file

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