Profile.php 5.2 KB

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