Profile.php 5.3 KB

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