菜谱项目

Profile.php 5.2KB

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