Profiler.php 6.7KB

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