暂无描述

Profile.php 5.4KB

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