Section.php 4.1 KB

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