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

/vendor/symfony/http-kernel/Profiler/Profiler.php

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