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

https://github.com/xbojer/gfw · PHP · 243 lines · 107 code · 30 blank · 106 comment · 6 complexity · 2aba27cbdb64d8046d538d01fbd1bc50 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\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@symfony.com>
  20. */
  21. class Profiler
  22. {
  23. private $storage;
  24. private $collectors;
  25. private $logger;
  26. private $enabled;
  27. /**
  28. * Constructor.
  29. *
  30. * @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
  31. * @param LoggerInterface $logger A LoggerInterface instance
  32. */
  33. public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
  34. {
  35. $this->storage = $storage;
  36. $this->logger = $logger;
  37. $this->collectors = array();
  38. $this->enabled = true;
  39. }
  40. /**
  41. * Disables the profiler.
  42. */
  43. public function disable()
  44. {
  45. $this->enabled = false;
  46. }
  47. /**
  48. * Loads the Profile for the given Response.
  49. *
  50. * @param Response $response A Response instance
  51. *
  52. * @return Profile A Profile instance
  53. */
  54. public function loadProfileFromResponse(Response $response)
  55. {
  56. if (!$token = $response->headers->get('X-Debug-Token')) {
  57. return false;
  58. }
  59. return $this->loadProfile($token);
  60. }
  61. /**
  62. * Loads the Profile for the given token.
  63. *
  64. * @param string $token A token
  65. *
  66. * @return Profile A Profile instance
  67. */
  68. public function loadProfile($token)
  69. {
  70. return $this->storage->read($token);
  71. }
  72. /**
  73. * Saves a Profile.
  74. *
  75. * @param Profile $profile A Profile instance
  76. *
  77. * @return Boolean
  78. */
  79. public function saveProfile(Profile $profile)
  80. {
  81. if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
  82. $this->logger->warn('Unable to store the profiler information.');
  83. }
  84. return $ret;
  85. }
  86. /**
  87. * Purges all data from the storage.
  88. */
  89. public function purge()
  90. {
  91. $this->storage->purge();
  92. }
  93. /**
  94. * Exports the current profiler data.
  95. *
  96. * @return string The exported data
  97. */
  98. public function export(Profile $profile)
  99. {
  100. return base64_encode(serialize($profile));
  101. }
  102. /**
  103. * Imports data into the profiler storage.
  104. *
  105. * @param string $data A data string as exported by the export() method
  106. *
  107. * @return Profile A Profile instance
  108. */
  109. public function import($data)
  110. {
  111. $profile = unserialize(base64_decode($data));
  112. if ($this->storage->read($profile->getToken())) {
  113. return false;
  114. }
  115. $this->saveProfile($profile);
  116. return $profile;
  117. }
  118. /**
  119. * Finds profiler tokens for the given criteria.
  120. *
  121. * @param string $ip The IP
  122. * @param string $url The URL
  123. * @param string $limit The maximum number of tokens to return
  124. *
  125. * @return array An array of tokens
  126. */
  127. public function find($ip, $url, $limit)
  128. {
  129. return $this->storage->find($ip, $url, $limit);
  130. }
  131. /**
  132. * Collects data for the given Response.
  133. *
  134. * @param Request $request A Request instance
  135. * @param Response $response A Response instance
  136. * @param \Exception $exception An exception instance if the request threw one
  137. *
  138. * @return Profile|false A Profile instance or false if the profiler is disabled
  139. */
  140. public function collect(Request $request, Response $response, \Exception $exception = null)
  141. {
  142. if (false === $this->enabled) {
  143. return;
  144. }
  145. $profile = new Profile(uniqid());
  146. $profile->setTime(time());
  147. $profile->setUrl($request->getUri());
  148. $profile->setIp($request->server->get('REMOTE_ADDR'));
  149. $response->headers->set('X-Debug-Token', $profile->getToken());
  150. $collectors = array();
  151. foreach ($this->collectors as $name => $collector) {
  152. $collector->collect($request, $response, $exception);
  153. // forces collectors to become "read/only" (they loose their object dependencies)
  154. $profile->addCollector(unserialize(serialize($collector)));
  155. }
  156. return $profile;
  157. }
  158. /**
  159. * Gets the Collectors associated with this profiler.
  160. *
  161. * @return array An array of collectors
  162. */
  163. public function all()
  164. {
  165. return $this->collectors;
  166. }
  167. /**
  168. * Sets the Collectors associated with this profiler.
  169. *
  170. * @param array $collectors An array of collectors
  171. */
  172. public function set(array $collectors = array())
  173. {
  174. $this->collectors = array();
  175. foreach ($collectors as $collector) {
  176. $this->add($collector);
  177. }
  178. }
  179. /**
  180. * Adds a Collector.
  181. *
  182. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  183. */
  184. public function add(DataCollectorInterface $collector)
  185. {
  186. $this->collectors[$collector->getName()] = $collector;
  187. }
  188. /**
  189. * Returns true if a Collector for the given name exists.
  190. *
  191. * @param string $name A collector name
  192. */
  193. public function has($name)
  194. {
  195. return isset($this->collectors[$name]);
  196. }
  197. /**
  198. * Gets a Collector by name.
  199. *
  200. * @param string $name A collector name
  201. *
  202. * @return DataCollectorInterface A DataCollectorInterface instance
  203. *
  204. * @throws \InvalidArgumentException if the collector does not exist
  205. */
  206. public function get($name)
  207. {
  208. if (!isset($this->collectors[$name])) {
  209. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  210. }
  211. return $this->collectors[$name];
  212. }
  213. }