Section.php 4.2 KB

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