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

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