PageRenderTime 26ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Profiler/Profiler.php

https://gitlab.com/oytunistrator/92five
PHP | 287 lines | 120 code | 37 blank | 130 comment | 9 complexity | 01491e5db5aead4a31eba3dbc3c81b32 MD5 | raw file
  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\HttpFoundation\Request;
  12. use Symfony\Component\HttpFoundation\Response;
  13. use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  14. use Psr\Log\LoggerInterface;
  15. /**
  16. * Profiler.
  17. *
  18. * @author Fabien Potencier <fabien@symfony.com>
  19. */
  20. class Profiler
  21. {
  22. /**
  23. * @var ProfilerStorageInterface
  24. */
  25. private $storage;
  26. /**
  27. * @var DataCollectorInterface[]
  28. */
  29. private $collectors = array();
  30. /**
  31. * @var LoggerInterface
  32. */
  33. private $logger;
  34. /**
  35. * @var bool
  36. */
  37. private $enabled = true;
  38. /**
  39. * Constructor.
  40. *
  41. * @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
  42. * @param LoggerInterface $logger A LoggerInterface instance
  43. */
  44. public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
  45. {
  46. $this->storage = $storage;
  47. $this->logger = $logger;
  48. }
  49. /**
  50. * Disables the profiler.
  51. */
  52. public function disable()
  53. {
  54. $this->enabled = false;
  55. }
  56. /**
  57. * Enables the profiler.
  58. */
  59. public function enable()
  60. {
  61. $this->enabled = true;
  62. }
  63. /**
  64. * Loads the Profile for the given Response.
  65. *
  66. * @param Response $response A Response instance
  67. *
  68. * @return Profile A Profile instance
  69. */
  70. public function loadProfileFromResponse(Response $response)
  71. {
  72. if (!$token = $response->headers->get('X-Debug-Token')) {
  73. return false;
  74. }
  75. return $this->loadProfile($token);
  76. }
  77. /**
  78. * Loads the Profile for the given token.
  79. *
  80. * @param string $token A token
  81. *
  82. * @return Profile A Profile instance
  83. */
  84. public function loadProfile($token)
  85. {
  86. return $this->storage->read($token);
  87. }
  88. /**
  89. * Saves a Profile.
  90. *
  91. * @param Profile $profile A Profile instance
  92. *
  93. * @return bool
  94. */
  95. public function saveProfile(Profile $profile)
  96. {
  97. if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
  98. $this->logger->warning('Unable to store the profiler information.');
  99. }
  100. return $ret;
  101. }
  102. /**
  103. * Purges all data from the storage.
  104. */
  105. public function purge()
  106. {
  107. $this->storage->purge();
  108. }
  109. /**
  110. * Exports the current profiler data.
  111. *
  112. * @param Profile $profile A Profile instance
  113. *
  114. * @return string The exported data
  115. */
  116. public function export(Profile $profile)
  117. {
  118. return base64_encode(serialize($profile));
  119. }
  120. /**
  121. * Imports data into the profiler storage.
  122. *
  123. * @param string $data A data string as exported by the export() method
  124. *
  125. * @return Profile A Profile instance
  126. */
  127. public function import($data)
  128. {
  129. $profile = unserialize(base64_decode($data));
  130. if ($this->storage->read($profile->getToken())) {
  131. return false;
  132. }
  133. $this->saveProfile($profile);
  134. return $profile;
  135. }
  136. /**
  137. * Finds profiler tokens for the given criteria.
  138. *
  139. * @param string $ip The IP
  140. * @param string $url The URL
  141. * @param string $limit The maximum number of tokens to return
  142. * @param string $method The request method
  143. * @param string $start The start date to search from
  144. * @param string $end The end date to search to
  145. *
  146. * @return array An array of tokens
  147. *
  148. * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
  149. */
  150. public function find($ip, $url, $limit, $method, $start, $end)
  151. {
  152. return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
  153. }
  154. /**
  155. * Collects data for the given Response.
  156. *
  157. * @param Request $request A Request instance
  158. * @param Response $response A Response instance
  159. * @param \Exception $exception An exception instance if the request threw one
  160. *
  161. * @return Profile|null A Profile instance or null if the profiler is disabled
  162. */
  163. public function collect(Request $request, Response $response, \Exception $exception = null)
  164. {
  165. if (false === $this->enabled) {
  166. return;
  167. }
  168. $profile = new Profile(substr(sha1(uniqid(mt_rand(), true)), 0, 6));
  169. $profile->setTime(time());
  170. $profile->setUrl($request->getUri());
  171. $profile->setIp($request->getClientIp());
  172. $profile->setMethod($request->getMethod());
  173. $response->headers->set('X-Debug-Token', $profile->getToken());
  174. foreach ($this->collectors as $collector) {
  175. $collector->collect($request, $response, $exception);
  176. // forces collectors to become "read/only" (they loose their object dependencies)
  177. $profile->addCollector(unserialize(serialize($collector)));
  178. }
  179. return $profile;
  180. }
  181. /**
  182. * Gets the Collectors associated with this profiler.
  183. *
  184. * @return array An array of collectors
  185. */
  186. public function all()
  187. {
  188. return $this->collectors;
  189. }
  190. /**
  191. * Sets the Collectors associated with this profiler.
  192. *
  193. * @param DataCollectorInterface[] $collectors An array of collectors
  194. */
  195. public function set(array $collectors = array())
  196. {
  197. $this->collectors = array();
  198. foreach ($collectors as $collector) {
  199. $this->add($collector);
  200. }
  201. }
  202. /**
  203. * Adds a Collector.
  204. *
  205. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  206. */
  207. public function add(DataCollectorInterface $collector)
  208. {
  209. $this->collectors[$collector->getName()] = $collector;
  210. }
  211. /**
  212. * Returns true if a Collector for the given name exists.
  213. *
  214. * @param string $name A collector name
  215. *
  216. * @return bool
  217. */
  218. public function has($name)
  219. {
  220. return isset($this->collectors[$name]);
  221. }
  222. /**
  223. * Gets a Collector by name.
  224. *
  225. * @param string $name A collector name
  226. *
  227. * @return DataCollectorInterface A DataCollectorInterface instance
  228. *
  229. * @throws \InvalidArgumentException if the collector does not exist
  230. */
  231. public function get($name)
  232. {
  233. if (!isset($this->collectors[$name])) {
  234. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  235. }
  236. return $this->collectors[$name];
  237. }
  238. private function getTimestamp($value)
  239. {
  240. if (null === $value || '' == $value) {
  241. return;
  242. }
  243. try {
  244. $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
  245. } catch (\Exception $e) {
  246. return;
  247. }
  248. return $value->getTimestamp();
  249. }
  250. }