Section.php 4.1 KB

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