Brak opisu

Profile.php 5.4KB

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