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

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