Profile.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 int The time
  139. */
  140. public function getTime()
  141. {
  142. if (null === $this->time) {
  143. return 0;
  144. }
  145. return $this->time;
  146. }
  147. /**
  148. * @param int The time
  149. */
  150. public function setTime($time)
  151. {
  152. $this->time = $time;
  153. }
  154. /**
  155. * @param int $statusCode
  156. */
  157. public function setStatusCode($statusCode)
  158. {
  159. $this->statusCode = $statusCode;
  160. }
  161. /**
  162. * @return int
  163. */
  164. public function getStatusCode()
  165. {
  166. return $this->statusCode;
  167. }
  168. /**
  169. * Finds children profilers.
  170. *
  171. * @return self[]
  172. */
  173. public function getChildren()
  174. {
  175. return $this->children;
  176. }
  177. /**
  178. * Sets children profiler.
  179. *
  180. * @param Profile[] $children
  181. */
  182. public function setChildren(array $children)
  183. {
  184. $this->children = array();
  185. foreach ($children as $child) {
  186. $this->addChild($child);
  187. }
  188. }
  189. /**
  190. * Adds the child token.
  191. *
  192. * @param Profile $child
  193. */
  194. public function addChild(Profile $child)
  195. {
  196. $this->children[] = $child;
  197. $child->setParent($this);
  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 = array();
  232. foreach ($collectors as $collector) {
  233. $this->addCollector($collector);
  234. }
  235. }
  236. /**
  237. * Adds a Collector.
  238. *
  239. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  240. */
  241. public function addCollector(DataCollectorInterface $collector)
  242. {
  243. $this->collectors[$collector->getName()] = $collector;
  244. }
  245. /**
  246. * Returns true if a Collector for the given name exists.
  247. *
  248. * @param string $name A collector name
  249. *
  250. * @return bool
  251. */
  252. public function hasCollector($name)
  253. {
  254. return isset($this->collectors[$name]);
  255. }
  256. public function __sleep()
  257. {
  258. return array('token', 'parent', 'children', 'collectors', 'ip', 'method', 'url', 'time', 'statusCode');
  259. }
  260. }