PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Component/Stopwatch/Stopwatch.php

https://gitlab.com/Snizer/PI-DEV-TUNISIAMALL3A6-WEB
PHP | 332 lines | 122 code | 37 blank | 173 comment | 10 complexity | d2809091b1f55b8a9c660fb39e9d7bca 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 provides a way to profile code.
  13. *
  14. * @author Fabien Potencier <fabien@symfony.com>
  15. */
  16. class Stopwatch
  17. {
  18. /**
  19. * @var Section[]
  20. */
  21. private $sections;
  22. /**
  23. * @var array
  24. */
  25. private $activeSections;
  26. public function __construct()
  27. {
  28. $this->sections = $this->activeSections = array('__root__' => new Section('__root__'));
  29. }
  30. /**
  31. * Creates a new section or re-opens an existing section.
  32. *
  33. * @param string|null $id The id of the session to re-open, null to create a new one
  34. *
  35. * @throws \LogicException When the section to re-open is not reachable
  36. */
  37. public function openSection($id = null)
  38. {
  39. $current = end($this->activeSections);
  40. if (null !== $id && null === $current->get($id)) {
  41. throw new \LogicException(sprintf('The section "%s" has been started at an other level and can not be opened.', $id));
  42. }
  43. $this->start('__section__.child', 'section');
  44. $this->activeSections[] = $current->open($id);
  45. $this->start('__section__');
  46. }
  47. /**
  48. * Stops the last started section.
  49. *
  50. * The id parameter is used to retrieve the events from this section.
  51. *
  52. * @see getSectionEvents()
  53. *
  54. * @param string $id The identifier of the section
  55. *
  56. * @throws \LogicException When there's no started section to be stopped
  57. */
  58. public function stopSection($id)
  59. {
  60. $this->stop('__section__');
  61. if (1 == count($this->activeSections)) {
  62. throw new \LogicException('There is no started section to stop.');
  63. }
  64. $this->sections[$id] = array_pop($this->activeSections)->setId($id);
  65. $this->stop('__section__.child');
  66. }
  67. /**
  68. * Starts an event.
  69. *
  70. * @param string $name The event name
  71. * @param string $category The event category
  72. *
  73. * @return StopwatchEvent A StopwatchEvent instance
  74. */
  75. public function start($name, $category = null)
  76. {
  77. return end($this->activeSections)->startEvent($name, $category);
  78. }
  79. /**
  80. * Checks if the event was started.
  81. *
  82. * @param string $name The event name
  83. *
  84. * @return bool
  85. */
  86. public function isStarted($name)
  87. {
  88. return end($this->activeSections)->isEventStarted($name);
  89. }
  90. /**
  91. * Stops an event.
  92. *
  93. * @param string $name The event name
  94. *
  95. * @return StopwatchEvent A StopwatchEvent instance
  96. */
  97. public function stop($name)
  98. {
  99. return end($this->activeSections)->stopEvent($name);
  100. }
  101. /**
  102. * Stops then restarts an event.
  103. *
  104. * @param string $name The event name
  105. *
  106. * @return StopwatchEvent A StopwatchEvent instance
  107. */
  108. public function lap($name)
  109. {
  110. return end($this->activeSections)->stopEvent($name)->start();
  111. }
  112. /**
  113. * Returns a specific event by name
  114. *
  115. * @param string $name The event name
  116. *
  117. * @return StopwatchEvent A StopwatchEvent instance
  118. */
  119. public function getEvent($name)
  120. {
  121. return end($this->activeSections)->getEvent($name);
  122. }
  123. /**
  124. * Gets all events for a given section.
  125. *
  126. * @param string $id A section identifier
  127. *
  128. * @return StopwatchEvent[] An array of StopwatchEvent instances
  129. */
  130. public function getSectionEvents($id)
  131. {
  132. return isset($this->sections[$id]) ? $this->sections[$id]->getEvents() : array();
  133. }
  134. }
  135. /**
  136. * @internal This class is for internal usage only
  137. *
  138. * @author Fabien Potencier <fabien@symfony.com>
  139. */
  140. class Section
  141. {
  142. /**
  143. * @var StopwatchEvent[]
  144. */
  145. private $events = array();
  146. /**
  147. * @var null|float
  148. */
  149. private $origin;
  150. /**
  151. * @var string
  152. */
  153. private $id;
  154. /**
  155. * @var Section[]
  156. */
  157. private $children = array();
  158. /**
  159. * Constructor.
  160. *
  161. * @param float|null $origin Set the origin of the events in this section, use null to set their origin to their start time
  162. */
  163. public function __construct($origin = null)
  164. {
  165. $this->origin = is_numeric($origin) ? $origin : null;
  166. }
  167. /**
  168. * Returns the child section.
  169. *
  170. * @param string $id The child section identifier
  171. *
  172. * @return Section|null The child section or null when none found
  173. */
  174. public function get($id)
  175. {
  176. foreach ($this->children as $child) {
  177. if ($id === $child->getId()) {
  178. return $child;
  179. }
  180. }
  181. }
  182. /**
  183. * Creates or re-opens a child section.
  184. *
  185. * @param string|null $id null to create a new section, the identifier to re-open an existing one.
  186. *
  187. * @return Section A child section
  188. */
  189. public function open($id)
  190. {
  191. if (null === $session = $this->get($id)) {
  192. $session = $this->children[] = new self(microtime(true) * 1000);
  193. }
  194. return $session;
  195. }
  196. /**
  197. * @return string The identifier of the section
  198. */
  199. public function getId()
  200. {
  201. return $this->id;
  202. }
  203. /**
  204. * Sets the session identifier.
  205. *
  206. * @param string $id The session identifier
  207. *
  208. * @return Section The current section
  209. */
  210. public function setId($id)
  211. {
  212. $this->id = $id;
  213. return $this;
  214. }
  215. /**
  216. * Starts an event.
  217. *
  218. * @param string $name The event name
  219. * @param string $category The event category
  220. *
  221. * @return StopwatchEvent The event
  222. */
  223. public function startEvent($name, $category)
  224. {
  225. if (!isset($this->events[$name])) {
  226. $this->events[$name] = new StopwatchEvent($this->origin ?: microtime(true) * 1000, $category);
  227. }
  228. return $this->events[$name]->start();
  229. }
  230. /**
  231. * Checks if the event was started.
  232. *
  233. * @param string $name The event name
  234. *
  235. * @return bool
  236. */
  237. public function isEventStarted($name)
  238. {
  239. return isset($this->events[$name]) && $this->events[$name]->isStarted();
  240. }
  241. /**
  242. * Stops an event.
  243. *
  244. * @param string $name The event name
  245. *
  246. * @return StopwatchEvent The event
  247. *
  248. * @throws \LogicException When the event has not been started
  249. */
  250. public function stopEvent($name)
  251. {
  252. if (!isset($this->events[$name])) {
  253. throw new \LogicException(sprintf('Event "%s" is not started.', $name));
  254. }
  255. return $this->events[$name]->stop();
  256. }
  257. /**
  258. * Stops then restarts an event.
  259. *
  260. * @param string $name The event name
  261. *
  262. * @return StopwatchEvent The event
  263. *
  264. * @throws \LogicException When the event has not been started
  265. */
  266. public function lap($name)
  267. {
  268. return $this->stopEvent($name)->start();
  269. }
  270. /**
  271. * Returns a specific event by name
  272. *
  273. * @param string $name The event name
  274. *
  275. * @return StopwatchEvent The event
  276. *
  277. * @throws \LogicException When the event is not known
  278. */
  279. public function getEvent($name)
  280. {
  281. if (!isset($this->events[$name])) {
  282. throw new \LogicException(sprintf('Event "%s" is not known.', $name));
  283. }
  284. return $this->events[$name];
  285. }
  286. /**
  287. * Returns the events from this section.
  288. *
  289. * @return StopwatchEvent[] An array of StopwatchEvent instances
  290. */
  291. public function getEvents()
  292. {
  293. return $this->events;
  294. }
  295. }