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

  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 ProfilerStorageInterface $storage;
  26.     /**
  27.      * @var DataCollectorInterface[]
  28.      */
  29.     private array $collectors = [];
  30.     private ?LoggerInterface $logger;
  31.     private bool $initiallyEnabled true;
  32.     private bool $enabled true;
  33.     public function __construct(ProfilerStorageInterface $storageLoggerInterface $logger nullbool $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.      * @return void
  43.      */
  44.     public function disable()
  45.     {
  46.         $this->enabled false;
  47.     }
  48.     /**
  49.      * Enables the profiler.
  50.      *
  51.      * @return void
  52.      */
  53.     public function enable()
  54.     {
  55.         $this->enabled true;
  56.     }
  57.     public function isEnabled(): bool
  58.     {
  59.         return $this->enabled;
  60.     }
  61.     /**
  62.      * Loads the Profile for the given Response.
  63.      */
  64.     public function loadProfileFromResponse(Response $response): ?Profile
  65.     {
  66.         if (!$token $response->headers->get('X-Debug-Token')) {
  67.             return null;
  68.         }
  69.         return $this->loadProfile($token);
  70.     }
  71.     /**
  72.      * Loads the Profile for the given token.
  73.      */
  74.     public function loadProfile(string $token): ?Profile
  75.     {
  76.         return $this->storage->read($token);
  77.     }
  78.     /**
  79.      * Saves a Profile.
  80.      */
  81.     public function saveProfile(Profile $profile): bool
  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.', ['configured_storage' => $this->storage::class]);
  91.         }
  92.         return $ret;
  93.     }
  94.     /**
  95.      * Purges all data from the storage.
  96.      *
  97.      * @return void
  98.      */
  99.     public function purge()
  100.     {
  101.         $this->storage->purge();
  102.     }
  103.     /**
  104.      * Finds profiler tokens for the given criteria.
  105.      *
  106.      * @param int|null    $limit The maximum number of tokens to return
  107.      * @param string|null $start The start date to search from
  108.      * @param string|null $end   The end date to search to
  109.      *
  110.      * @see https://php.net/datetime.formats for the supported date/time formats
  111.      */
  112.     public function find(?string $ip, ?string $url, ?int $limit, ?string $method, ?string $start, ?string $endstring $statusCode null): array
  113.     {
  114.         return $this->storage->find($ip$url$limit$method$this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
  115.     }
  116.     /**
  117.      * Collects data for the given Response.
  118.      */
  119.     public function collect(Request $requestResponse $response\Throwable $exception null): ?Profile
  120.     {
  121.         if (false === $this->enabled) {
  122.             return null;
  123.         }
  124.         $profile = new Profile(substr(hash('sha256'uniqid(mt_rand(), true)), 06));
  125.         $profile->setTime(time());
  126.         $profile->setUrl($request->getUri());
  127.         $profile->setMethod($request->getMethod());
  128.         $profile->setStatusCode($response->getStatusCode());
  129.         try {
  130.             $profile->setIp($request->getClientIp());
  131.         } catch (ConflictingHeadersException) {
  132.             $profile->setIp('Unknown');
  133.         }
  134.         if ($prevToken $response->headers->get('X-Debug-Token')) {
  135.             $response->headers->set('X-Previous-Debug-Token'$prevToken);
  136.         }
  137.         $response->headers->set('X-Debug-Token'$profile->getToken());
  138.         foreach ($this->collectors as $collector) {
  139.             $collector->collect($request$response$exception);
  140.             // we need to clone for sub-requests
  141.             $profile->addCollector(clone $collector);
  142.         }
  143.         return $profile;
  144.     }
  145.     /**
  146.      * @return void
  147.      */
  148.     public function reset()
  149.     {
  150.         foreach ($this->collectors as $collector) {
  151.             $collector->reset();
  152.         }
  153.         $this->enabled $this->initiallyEnabled;
  154.     }
  155.     /**
  156.      * Gets the Collectors associated with this profiler.
  157.      */
  158.     public function all(): array
  159.     {
  160.         return $this->collectors;
  161.     }
  162.     /**
  163.      * Sets the Collectors associated with this profiler.
  164.      *
  165.      * @param DataCollectorInterface[] $collectors An array of collectors
  166.      *
  167.      * @return void
  168.      */
  169.     public function set(array $collectors = [])
  170.     {
  171.         $this->collectors = [];
  172.         foreach ($collectors as $collector) {
  173.             $this->add($collector);
  174.         }
  175.     }
  176.     /**
  177.      * Adds a Collector.
  178.      *
  179.      * @return void
  180.      */
  181.     public function add(DataCollectorInterface $collector)
  182.     {
  183.         $this->collectors[$collector->getName()] = $collector;
  184.     }
  185.     /**
  186.      * Returns true if a Collector for the given name exists.
  187.      *
  188.      * @param string $name A collector name
  189.      */
  190.     public function has(string $name): bool
  191.     {
  192.         return isset($this->collectors[$name]);
  193.     }
  194.     /**
  195.      * Gets a Collector by name.
  196.      *
  197.      * @param string $name A collector name
  198.      *
  199.      * @throws \InvalidArgumentException if the collector does not exist
  200.      */
  201.     public function get(string $name): DataCollectorInterface
  202.     {
  203.         if (!isset($this->collectors[$name])) {
  204.             throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.'$name));
  205.         }
  206.         return $this->collectors[$name];
  207.     }
  208.     private function getTimestamp(?string $value): ?int
  209.     {
  210.         if (null === $value || '' === $value) {
  211.             return null;
  212.         }
  213.         try {
  214.             $value = new \DateTimeImmutable(is_numeric($value) ? '@'.$value $value);
  215.         } catch (\Exception) {
  216.             return null;
  217.         }
  218.         return $value->getTimestamp();
  219.     }
  220. }