You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

274 lines
5.1 KiB

3 years ago
  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\HttpKernel\Profiler;
  11. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  12. /**
  13. * Profile.
  14. *
  15. * @author Fabien Potencier <fabien@symfony.com>
  16. */
  17. class Profile
  18. {
  19. private $token;
  20. /**
  21. * @var DataCollectorInterface[]
  22. */
  23. private $collectors = [];
  24. private $ip;
  25. private $method;
  26. private $url;
  27. private $time;
  28. private $statusCode;
  29. /**
  30. * @var Profile
  31. */
  32. private $parent;
  33. /**
  34. * @var Profile[]
  35. */
  36. private $children = [];
  37. public function __construct(string $token)
  38. {
  39. $this->token = $token;
  40. }
  41. public function setToken(string $token)
  42. {
  43. $this->token = $token;
  44. }
  45. /**
  46. * Gets the token.
  47. *
  48. * @return string The token
  49. */
  50. public function getToken()
  51. {
  52. return $this->token;
  53. }
  54. /**
  55. * Sets the parent token.
  56. */
  57. public function setParent(self $parent)
  58. {
  59. $this->parent = $parent;
  60. }
  61. /**
  62. * Returns the parent profile.
  63. *
  64. * @return self
  65. */
  66. public function getParent()
  67. {
  68. return $this->parent;
  69. }
  70. /**
  71. * Returns the parent token.
  72. *
  73. * @return string|null The parent token
  74. */
  75. public function getParentToken()
  76. {
  77. return $this->parent ? $this->parent->getToken() : null;
  78. }
  79. /**
  80. * Returns the IP.
  81. *
  82. * @return string|null The IP
  83. */
  84. public function getIp()
  85. {
  86. return $this->ip;
  87. }
  88. public function setIp(?string $ip)
  89. {
  90. $this->ip = $ip;
  91. }
  92. /**
  93. * Returns the request method.
  94. *
  95. * @return string|null The request method
  96. */
  97. public function getMethod()
  98. {
  99. return $this->method;
  100. }
  101. public function setMethod(string $method)
  102. {
  103. $this->method = $method;
  104. }
  105. /**
  106. * Returns the URL.
  107. *
  108. * @return string|null The URL
  109. */
  110. public function getUrl()
  111. {
  112. return $this->url;
  113. }
  114. public function setUrl(?string $url)
  115. {
  116. $this->url = $url;
  117. }
  118. /**
  119. * @return int The time
  120. */
  121. public function getTime()
  122. {
  123. if (null === $this->time) {
  124. return 0;
  125. }
  126. return $this->time;
  127. }
  128. public function setTime(int $time)
  129. {
  130. $this->time = $time;
  131. }
  132. public function setStatusCode(int $statusCode)
  133. {
  134. $this->statusCode = $statusCode;
  135. }
  136. /**
  137. * @return int|null
  138. */
  139. public function getStatusCode()
  140. {
  141. return $this->statusCode;
  142. }
  143. /**
  144. * Finds children profilers.
  145. *
  146. * @return self[]
  147. */
  148. public function getChildren()
  149. {
  150. return $this->children;
  151. }
  152. /**
  153. * Sets children profiler.
  154. *
  155. * @param Profile[] $children
  156. */
  157. public function setChildren(array $children)
  158. {
  159. $this->children = [];
  160. foreach ($children as $child) {
  161. $this->addChild($child);
  162. }
  163. }
  164. /**
  165. * Adds the child token.
  166. */
  167. public function addChild(self $child)
  168. {
  169. $this->children[] = $child;
  170. $child->setParent($this);
  171. }
  172. public function getChildByToken(string $token): ?self
  173. {
  174. foreach ($this->children as $child) {
  175. if ($token === $child->getToken()) {
  176. return $child;
  177. }
  178. }
  179. return null;
  180. }
  181. /**
  182. * Gets a Collector by name.
  183. *
  184. * @return DataCollectorInterface A DataCollectorInterface instance
  185. *
  186. * @throws \InvalidArgumentException if the collector does not exist
  187. */
  188. public function getCollector(string $name)
  189. {
  190. if (!isset($this->collectors[$name])) {
  191. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  192. }
  193. return $this->collectors[$name];
  194. }
  195. /**
  196. * Gets the Collectors associated with this profile.
  197. *
  198. * @return DataCollectorInterface[]
  199. */
  200. public function getCollectors()
  201. {
  202. return $this->collectors;
  203. }
  204. /**
  205. * Sets the Collectors associated with this profile.
  206. *
  207. * @param DataCollectorInterface[] $collectors
  208. */
  209. public function setCollectors(array $collectors)
  210. {
  211. $this->collectors = [];
  212. foreach ($collectors as $collector) {
  213. $this->addCollector($collector);
  214. }
  215. }
  216. /**
  217. * Adds a Collector.
  218. */
  219. public function addCollector(DataCollectorInterface $collector)
  220. {
  221. $this->collectors[$collector->getName()] = $collector;
  222. }
  223. /**
  224. * @return bool
  225. */
  226. public function hasCollector(string $name)
  227. {
  228. return isset($this->collectors[$name]);
  229. }
  230. /**
  231. * @return array
  232. */
  233. public function __sleep()
  234. {
  235. return ['token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode'];
  236. }
  237. }