PageRenderTime 36ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 316 lines | 151 code | 39 blank | 126 comment | 8 complexity | 36f20530d204fb09da36106027f76466 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Symfony\Component\HttpKernel\Profiler;
  3. use Symfony\Component\HttpFoundation\Request;
  4. use Symfony\Component\HttpFoundation\Response;
  5. use Symfony\Component\HttpKernel\Profiler\ProfilerStorageInterface;
  6. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  7. use Symfony\Component\HttpKernel\Log\LoggerInterface;
  8. /*
  9. * This file is part of the Symfony framework.
  10. *
  11. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  12. *
  13. * This source file is subject to the MIT license that is bundled
  14. * with this source code in the file LICENSE.
  15. */
  16. /**
  17. * Profiler.
  18. *
  19. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  20. */
  21. class Profiler
  22. {
  23. protected $storage;
  24. protected $collectors;
  25. protected $logger;
  26. protected $enabled;
  27. protected $token;
  28. protected $data;
  29. protected $ip;
  30. protected $url;
  31. protected $time;
  32. protected $empty;
  33. /**
  34. * Constructor.
  35. *
  36. * @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
  37. * @param LoggerInterface $logger A LoggerInterface instance
  38. */
  39. public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
  40. {
  41. $this->storage = $storage;
  42. $this->logger = $logger;
  43. $this->collectors = array();
  44. $this->enabled = true;
  45. $this->empty = true;
  46. }
  47. /**
  48. * Disables the profiler.
  49. */
  50. public function disable()
  51. {
  52. $this->enabled = false;
  53. }
  54. /**
  55. * Loads a Profiler for the given Response.
  56. *
  57. * @param Response $response A Response instance
  58. *
  59. * @return Profiler A new Profiler instance
  60. */
  61. public function loadFromResponse(Response $response)
  62. {
  63. if (!$token = $response->headers->get('X-Debug-Token')) {
  64. return null;
  65. }
  66. return $this->loadFromToken($token);
  67. }
  68. /**
  69. * Loads a Profiler for the given token.
  70. *
  71. * @param string $token A token
  72. *
  73. * @return Profiler A new Profiler instance
  74. */
  75. public function loadFromToken($token)
  76. {
  77. $profiler = new self($this->storage, $this->logger);
  78. $profiler->setToken($token);
  79. return $profiler;
  80. }
  81. /**
  82. * Purges all data from the storage.
  83. */
  84. public function purge()
  85. {
  86. $this->storage->purge();
  87. }
  88. /**
  89. * Exports the current profiler data.
  90. *
  91. * @return string The exported data
  92. */
  93. public function export()
  94. {
  95. $unpack = unpack('H*', serialize(array($this->token, $this->collectors, $this->ip, $this->url, $this->time)));
  96. return $unpack[1];
  97. }
  98. /**
  99. * Imports data into the profiler storage.
  100. *
  101. * @param string $data A data string as exported by the export() method
  102. *
  103. * @return string The token associated with the imported data
  104. */
  105. public function import($data)
  106. {
  107. list($token, $collectors, $ip, $url, $time) = unserialize(pack('H*', $data));
  108. if (false !== $this->storage->read($token)) {
  109. return false;
  110. }
  111. $unpack = unpack('H*', serialize($this->collectors));
  112. $this->storage->write($token, $unpack[1], $ip, $url, $time);
  113. return $token;
  114. }
  115. /**
  116. * Sets the token.
  117. *
  118. * @param string $token The token
  119. */
  120. public function setToken($token)
  121. {
  122. $this->token = $token;
  123. if (false !== $items = $this->storage->read($token)) {
  124. list($data, $this->ip, $this->url, $this->time) = $items;
  125. $this->set(unserialize(pack('H*', $data)));
  126. $this->empty = false;
  127. } else {
  128. $this->empty = true;
  129. }
  130. }
  131. /**
  132. * Gets the token.
  133. *
  134. * @return string The token
  135. */
  136. public function getToken()
  137. {
  138. if (null === $this->token) {
  139. $this->token = uniqid();
  140. }
  141. return $this->token;
  142. }
  143. /**
  144. * Checks if the profiler is empty.
  145. *
  146. * @return Boolean Whether the profiler is empty or not
  147. */
  148. public function isEmpty()
  149. {
  150. return $this->empty;
  151. }
  152. /**
  153. * Returns the IP.
  154. *
  155. * @return string The IP
  156. */
  157. public function getIp()
  158. {
  159. return $this->ip;
  160. }
  161. /**
  162. * Returns the URL.
  163. *
  164. * @return string The URL
  165. */
  166. public function getUrl()
  167. {
  168. return $this->url;
  169. }
  170. /**
  171. * Returns the time.
  172. *
  173. * @return string The time
  174. */
  175. public function getTime()
  176. {
  177. return $this->time;
  178. }
  179. /**
  180. * Finds profiler tokens for the given criteria.
  181. *
  182. * @param string $ip The IP
  183. * @param string $url The URL
  184. * @param string $limit The maximum number of tokens to return
  185. *
  186. * @return array An array of tokens
  187. */
  188. public function find($ip, $url, $limit)
  189. {
  190. return $this->storage->find($ip, $url, $limit);
  191. }
  192. /**
  193. * Collects data for the given Response.
  194. *
  195. * @param Request $request A Request instance
  196. * @param Response $response A Response instance
  197. * @param \Exception $exception An exception instance if the request threw one
  198. */
  199. public function collect(Request $request, Response $response, \Exception $exception = null)
  200. {
  201. if (false === $this->enabled) {
  202. return;
  203. }
  204. $response = $response;
  205. $response->headers->set('X-Debug-Token', $this->getToken());
  206. foreach ($this->collectors as $collector) {
  207. $collector->collect($request, $response, $exception);
  208. }
  209. $this->ip = $request->server->get('REMOTE_ADDR');
  210. $this->url = $request->getUri();
  211. $this->time = time();
  212. $unpack = unpack('H*', serialize($this->collectors));
  213. try {
  214. $this->storage->write($this->token, $unpack[1], $this->ip, $this->url, $this->time);
  215. $this->empty = false;
  216. } catch (\Exception $e) {
  217. if (null !== $this->logger) {
  218. $this->logger->err(sprintf('Unable to store the profiler information (%s).', $e->getMessage()));
  219. }
  220. }
  221. }
  222. /**
  223. * Gets the Collectors associated with this profiler.
  224. *
  225. * @return array An array of collectors
  226. */
  227. public function all()
  228. {
  229. return $this->collectors;
  230. }
  231. /**
  232. * Sets the Collectors associated with this profiler.
  233. *
  234. * @param array $collectors An array of collectors
  235. */
  236. public function set(array $collectors = array())
  237. {
  238. $this->collectors = array();
  239. foreach ($collectors as $collector) {
  240. $this->add($collector);
  241. }
  242. }
  243. /**
  244. * Adds a Collector.
  245. *
  246. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  247. */
  248. public function add(DataCollectorInterface $collector)
  249. {
  250. $this->collectors[$collector->getName()] = $collector;
  251. }
  252. /**
  253. * Returns true if a Collector for the given name exists.
  254. *
  255. * @param string $name A collector name
  256. */
  257. public function has($name)
  258. {
  259. return isset($this->collectors[$name]);
  260. }
  261. /**
  262. * Gets a Collector by name.
  263. *
  264. * @param string $name A collector name
  265. *
  266. * @return DataCollectorInterface A DataCollectorInterface instance
  267. *
  268. * @throws \InvalidArgumentException if the collector does not exist
  269. */
  270. public function get($name)
  271. {
  272. if (!isset($this->collectors[$name])) {
  273. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  274. }
  275. return $this->collectors[$name];
  276. }
  277. }