Profile.php 5.0 KB

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