/vendor/vendor_full/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php

https://github.com/l3l0/BehatExamples · PHP · 329 lines · 157 code · 41 blank · 131 comment · 10 complexity · 9321ac8dd2e453bcd229315563494796 MD5 · raw file

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