Drupal investigation

Profiler.php 8.0KB

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