PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://gitlab.com/pthapa81/MeroSaaman-1.0
PHP | 295 lines | 126 code | 38 blank | 131 comment | 10 complexity | 6b8efd715e55b8dab765334c7db4d542 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.');
  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. $response->headers->set('X-Debug-Token', $profile->getToken());
  181. foreach ($this->collectors as $collector) {
  182. $collector->collect($request, $response, $exception);
  183. // we need to clone for sub-requests
  184. $profile->addCollector(clone $collector);
  185. }
  186. return $profile;
  187. }
  188. /**
  189. * Gets the Collectors associated with this profiler.
  190. *
  191. * @return array An array of collectors
  192. */
  193. public function all()
  194. {
  195. return $this->collectors;
  196. }
  197. /**
  198. * Sets the Collectors associated with this profiler.
  199. *
  200. * @param DataCollectorInterface[] $collectors An array of collectors
  201. */
  202. public function set(array $collectors = array())
  203. {
  204. $this->collectors = array();
  205. foreach ($collectors as $collector) {
  206. $this->add($collector);
  207. }
  208. }
  209. /**
  210. * Adds a Collector.
  211. *
  212. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  213. */
  214. public function add(DataCollectorInterface $collector)
  215. {
  216. $this->collectors[$collector->getName()] = $collector;
  217. }
  218. /**
  219. * Returns true if a Collector for the given name exists.
  220. *
  221. * @param string $name A collector name
  222. *
  223. * @return bool
  224. */
  225. public function has($name)
  226. {
  227. return isset($this->collectors[$name]);
  228. }
  229. /**
  230. * Gets a Collector by name.
  231. *
  232. * @param string $name A collector name
  233. *
  234. * @return DataCollectorInterface A DataCollectorInterface instance
  235. *
  236. * @throws \InvalidArgumentException if the collector does not exist
  237. */
  238. public function get($name)
  239. {
  240. if (!isset($this->collectors[$name])) {
  241. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  242. }
  243. return $this->collectors[$name];
  244. }
  245. private function getTimestamp($value)
  246. {
  247. if (null === $value || '' == $value) {
  248. return;
  249. }
  250. try {
  251. $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
  252. } catch (\Exception $e) {
  253. return;
  254. }
  255. return $value->getTimestamp();
  256. }
  257. }