Profiler.php 7.2 KB

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