Profiler.php 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. * @deprecated since Symfony 2.8, to be removed in 3.0.
  124. */
  125. public function export(Profile $profile)
  126. {
  127. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  128. return base64_encode(serialize($profile));
  129. }
  130. /**
  131. * Imports data into the profiler storage.
  132. *
  133. * @param string $data A data string as exported by the export() method
  134. *
  135. * @return Profile A Profile instance
  136. *
  137. * @deprecated since Symfony 2.8, to be removed in 3.0.
  138. */
  139. public function import($data)
  140. {
  141. @trigger_error('The '.__METHOD__.' method is deprecated since version 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
  142. $profile = unserialize(base64_decode($data));
  143. if ($this->storage->read($profile->getToken())) {
  144. return false;
  145. }
  146. $this->saveProfile($profile);
  147. return $profile;
  148. }
  149. /**
  150. * Finds profiler tokens for the given criteria.
  151. *
  152. * @param string $ip The IP
  153. * @param string $url The URL
  154. * @param string $limit The maximum number of tokens to return
  155. * @param string $method The request method
  156. * @param string $start The start date to search from
  157. * @param string $end The end date to search to
  158. *
  159. * @return array An array of tokens
  160. *
  161. * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
  162. */
  163. public function find($ip, $url, $limit, $method, $start, $end)
  164. {
  165. return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
  166. }
  167. /**
  168. * Collects data for the given Response.
  169. *
  170. * @param Request $request A Request instance
  171. * @param Response $response A Response instance
  172. * @param \Exception $exception An exception instance if the request threw one
  173. *
  174. * @return Profile|null A Profile instance or null if the profiler is disabled
  175. */
  176. public function collect(Request $request, Response $response, \Exception $exception = null)
  177. {
  178. if (false === $this->enabled) {
  179. return;
  180. }
  181. $profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
  182. $profile->setTime(time());
  183. $profile->setUrl($request->getUri());
  184. $profile->setIp($request->getClientIp());
  185. $profile->setMethod($request->getMethod());
  186. $profile->setStatusCode($response->getStatusCode());
  187. $response->headers->set('X-Debug-Token', $profile->getToken());
  188. foreach ($this->collectors as $collector) {
  189. $collector->collect($request, $response, $exception);
  190. // we need to clone for sub-requests
  191. $profile->addCollector(clone $collector);
  192. }
  193. return $profile;
  194. }
  195. /**
  196. * Gets the Collectors associated with this profiler.
  197. *
  198. * @return array An array of collectors
  199. */
  200. public function all()
  201. {
  202. return $this->collectors;
  203. }
  204. /**
  205. * Sets the Collectors associated with this profiler.
  206. *
  207. * @param DataCollectorInterface[] $collectors An array of collectors
  208. */
  209. public function set(array $collectors = array())
  210. {
  211. $this->collectors = array();
  212. foreach ($collectors as $collector) {
  213. $this->add($collector);
  214. }
  215. }
  216. /**
  217. * Adds a Collector.
  218. *
  219. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  220. */
  221. public function add(DataCollectorInterface $collector)
  222. {
  223. $this->collectors[$collector->getName()] = $collector;
  224. }
  225. /**
  226. * Returns true if a Collector for the given name exists.
  227. *
  228. * @param string $name A collector name
  229. *
  230. * @return bool
  231. */
  232. public function has($name)
  233. {
  234. return isset($this->collectors[$name]);
  235. }
  236. /**
  237. * Gets a Collector by name.
  238. *
  239. * @param string $name A collector name
  240. *
  241. * @return DataCollectorInterface A DataCollectorInterface instance
  242. *
  243. * @throws \InvalidArgumentException if the collector does not exist
  244. */
  245. public function get($name)
  246. {
  247. if (!isset($this->collectors[$name])) {
  248. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  249. }
  250. return $this->collectors[$name];
  251. }
  252. private function getTimestamp($value)
  253. {
  254. if (null === $value || '' == $value) {
  255. return;
  256. }
  257. try {
  258. $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
  259. } catch (\Exception $e) {
  260. return;
  261. }
  262. return $value->getTimestamp();
  263. }
  264. }