Profile.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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. /**
  31. * @var Profile[]
  32. */
  33. private array $children = [];
  34. public function __construct(string $token)
  35. {
  36. $this->token = $token;
  37. }
  38. public function setToken(string $token)
  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)
  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 ? $this->parent->getToken() : null;
  69. }
  70. /**
  71. * Returns the IP.
  72. */
  73. public function getIp(): ?string
  74. {
  75. return $this->ip;
  76. }
  77. public function setIp(?string $ip)
  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)
  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)
  100. {
  101. $this->url = $url;
  102. }
  103. public function getTime(): int
  104. {
  105. return $this->time ?? 0;
  106. }
  107. public function setTime(int $time)
  108. {
  109. $this->time = $time;
  110. }
  111. public function setStatusCode(int $statusCode)
  112. {
  113. $this->statusCode = $statusCode;
  114. }
  115. public function getStatusCode(): ?int
  116. {
  117. return $this->statusCode;
  118. }
  119. /**
  120. * Finds children profilers.
  121. *
  122. * @return self[]
  123. */
  124. public function getChildren(): array
  125. {
  126. return $this->children;
  127. }
  128. /**
  129. * Sets children profiler.
  130. *
  131. * @param Profile[] $children
  132. */
  133. public function setChildren(array $children)
  134. {
  135. $this->children = [];
  136. foreach ($children as $child) {
  137. $this->addChild($child);
  138. }
  139. }
  140. /**
  141. * Adds the child token.
  142. */
  143. public function addChild(self $child)
  144. {
  145. $this->children[] = $child;
  146. $child->setParent($this);
  147. }
  148. public function getChildByToken(string $token): ?self
  149. {
  150. foreach ($this->children as $child) {
  151. if ($token === $child->getToken()) {
  152. return $child;
  153. }
  154. }
  155. return null;
  156. }
  157. /**
  158. * Gets a Collector by name.
  159. *
  160. * @throws \InvalidArgumentException if the collector does not exist
  161. */
  162. public function getCollector(string $name): DataCollectorInterface
  163. {
  164. if (!isset($this->collectors[$name])) {
  165. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  166. }
  167. return $this->collectors[$name];
  168. }
  169. /**
  170. * Gets the Collectors associated with this profile.
  171. *
  172. * @return DataCollectorInterface[]
  173. */
  174. public function getCollectors(): array
  175. {
  176. return $this->collectors;
  177. }
  178. /**
  179. * Sets the Collectors associated with this profile.
  180. *
  181. * @param DataCollectorInterface[] $collectors
  182. */
  183. public function setCollectors(array $collectors)
  184. {
  185. $this->collectors = [];
  186. foreach ($collectors as $collector) {
  187. $this->addCollector($collector);
  188. }
  189. }
  190. /**
  191. * Adds a Collector.
  192. */
  193. public function addCollector(DataCollectorInterface $collector)
  194. {
  195. $this->collectors[$collector->getName()] = $collector;
  196. }
  197. public function hasCollector(string $name): bool
  198. {
  199. return isset($this->collectors[$name]);
  200. }
  201. public function __sleep(): array
  202. {
  203. return ['token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode'];
  204. }
  205. }