Profiler.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 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 Boolean
  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 Boolean
  95. */
  96. public function saveProfile(Profile $profile)
  97. {
  98. if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
  99. $this->logger->warning('Unable to store the profiler information.');
  100. }
  101. return $ret;
  102. }
  103. /**
  104. * Purges all data from the storage.
  105. */
  106. public function purge()
  107. {
  108. $this->storage->purge();
  109. }
  110. /**
  111. * Exports the current profiler data.
  112. *
  113. * @param Profile $profile A Profile instance
  114. *
  115. * @return string The exported data
  116. */
  117. public function export(Profile $profile)
  118. {
  119. return base64_encode(serialize($profile));
  120. }
  121. /**
  122. * Imports data into the profiler storage.
  123. *
  124. * @param string $data A data string as exported by the export() method
  125. *
  126. * @return Profile A Profile instance
  127. */
  128. public function import($data)
  129. {
  130. $profile = unserialize(base64_decode($data));
  131. if ($this->storage->read($profile->getToken())) {
  132. return false;
  133. }
  134. $this->saveProfile($profile);
  135. return $profile;
  136. }
  137. /**
  138. * Finds profiler tokens for the given criteria.
  139. *
  140. * @param string $ip The IP
  141. * @param string $url The URL
  142. * @param string $limit The maximum number of tokens to return
  143. * @param string $method The request method
  144. * @param string $start The start date to search from
  145. * @param string $end The end date to search to
  146. *
  147. * @return array An array of tokens
  148. *
  149. * @see http://fr2.php.net/manual/en/datetime.formats.php for the supported date/time formats
  150. */
  151. public function find($ip, $url, $limit, $method, $start, $end)
  152. {
  153. if ('' != $start && null !== $start) {
  154. $start = new \DateTime($start);
  155. $start = $start->getTimestamp();
  156. } else {
  157. $start = null;
  158. }
  159. if ('' != $end && null !== $end) {
  160. $end = new \DateTime($end);
  161. $end = $end->getTimestamp();
  162. } else {
  163. $end = null;
  164. }
  165. return $this->storage->find($ip, $url, $limit, $method, $start, $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(sha1(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. $response->headers->set('X-Debug-Token', $profile->getToken());
  187. foreach ($this->collectors as $collector) {
  188. $collector->collect($request, $response, $exception);
  189. // forces collectors to become "read/only" (they loose their object dependencies)
  190. $profile->addCollector(unserialize(serialize($collector)));
  191. }
  192. return $profile;
  193. }
  194. /**
  195. * Gets the Collectors associated with this profiler.
  196. *
  197. * @return array An array of collectors
  198. */
  199. public function all()
  200. {
  201. return $this->collectors;
  202. }
  203. /**
  204. * Sets the Collectors associated with this profiler.
  205. *
  206. * @param DataCollectorInterface[] $collectors An array of collectors
  207. */
  208. public function set(array $collectors = array())
  209. {
  210. $this->collectors = array();
  211. foreach ($collectors as $collector) {
  212. $this->add($collector);
  213. }
  214. }
  215. /**
  216. * Adds a Collector.
  217. *
  218. * @param DataCollectorInterface $collector A DataCollectorInterface instance
  219. */
  220. public function add(DataCollectorInterface $collector)
  221. {
  222. $this->collectors[$collector->getName()] = $collector;
  223. }
  224. /**
  225. * Returns true if a Collector for the given name exists.
  226. *
  227. * @param string $name A collector name
  228. *
  229. * @return Boolean
  230. */
  231. public function has($name)
  232. {
  233. return isset($this->collectors[$name]);
  234. }
  235. /**
  236. * Gets a Collector by name.
  237. *
  238. * @param string $name A collector name
  239. *
  240. * @return DataCollectorInterface A DataCollectorInterface instance
  241. *
  242. * @throws \InvalidArgumentException if the collector does not exist
  243. */
  244. public function get($name)
  245. {
  246. if (!isset($this->collectors[$name])) {
  247. throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
  248. }
  249. return $this->collectors[$name];
  250. }
  251. }