vendor/symfony/http-kernel/Profiler/Profiler.php line 127

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