vendor/symfony/symfony/src/Symfony/Component/HttpKernel/Profiler/Profiler.php line 165

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. /**
  18.  * Profiler.
  19.  *
  20.  * @author Fabien Potencier <fabien@symfony.com>
  21.  */
  22. class Profiler
  23. {
  24.     private $storage;
  25.     /**
  26.      * @var DataCollectorInterface[]
  27.      */
  28.     private $collectors = [];
  29.     private $logger;
  30.     private $initiallyEnabled true;
  31.     private $enabled true;
  32.     /**
  33.      * @param bool $enable The initial enabled state
  34.      */
  35.     public function __construct(ProfilerStorageInterface $storageLoggerInterface $logger null$enable true)
  36.     {
  37.         $this->storage $storage;
  38.         $this->logger $logger;
  39.         $this->initiallyEnabled $this->enabled = (bool) $enable;
  40.     }
  41.     /**
  42.      * Disables the profiler.
  43.      */
  44.     public function disable()
  45.     {
  46.         $this->enabled false;
  47.     }
  48.     /**
  49.      * Enables the profiler.
  50.      */
  51.     public function enable()
  52.     {
  53.         $this->enabled true;
  54.     }
  55.     /**
  56.      * Loads the Profile for the given Response.
  57.      *
  58.      * @return Profile|null A Profile instance
  59.      */
  60.     public function loadProfileFromResponse(Response $response)
  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.      * @param string $token A token
  71.      *
  72.      * @return Profile|null A Profile instance
  73.      */
  74.     public function loadProfile($token)
  75.     {
  76.         return $this->storage->read($token);
  77.     }
  78.     /**
  79.      * Saves a Profile.
  80.      *
  81.      * @return bool
  82.      */
  83.     public function saveProfile(Profile $profile)
  84.     {
  85.         // late collect
  86.         foreach ($profile->getCollectors() as $collector) {
  87.             if ($collector instanceof LateDataCollectorInterface) {
  88.                 $collector->lateCollect();
  89.             }
  90.         }
  91.         if (!($ret $this->storage->write($profile)) && null !== $this->logger) {
  92.             $this->logger->warning('Unable to store the profiler information.', ['configured_storage' => \get_class($this->storage)]);
  93.         }
  94.         return $ret;
  95.     }
  96.     /**
  97.      * Purges all data from the storage.
  98.      */
  99.     public function purge()
  100.     {
  101.         $this->storage->purge();
  102.     }
  103.     /**
  104.      * Finds profiler tokens for the given criteria.
  105.      *
  106.      * @param string $ip         The IP
  107.      * @param string $url        The URL
  108.      * @param string $limit      The maximum number of tokens to return
  109.      * @param string $method     The request method
  110.      * @param string $start      The start date to search from
  111.      * @param string $end        The end date to search to
  112.      * @param string $statusCode The request status code
  113.      *
  114.      * @return array An array of tokens
  115.      *
  116.      * @see https://php.net/datetime.formats for the supported date/time formats
  117.      */
  118.     public function find($ip$url$limit$method$start$end$statusCode null)
  119.     {
  120.         return $this->storage->find($ip$url$limit$method$this->getTimestamp($start), $this->getTimestamp($end), $statusCode);
  121.     }
  122.     /**
  123.      * Collects data for the given Response.
  124.      *
  125.      * @return Profile|null A Profile instance or null if the profiler is disabled
  126.      */
  127.     public function collect(Request $requestResponse $response, \Exception $exception null)
  128.     {
  129.         if (false === $this->enabled) {
  130.             return null;
  131.         }
  132.         $profile = new Profile(substr(hash('sha256'uniqid(mt_rand(), true)), 06));
  133.         $profile->setTime(time());
  134.         $profile->setUrl($request->getUri());
  135.         $profile->setMethod($request->getMethod());
  136.         $profile->setStatusCode($response->getStatusCode());
  137.         try {
  138.             $profile->setIp($request->getClientIp());
  139.         } catch (ConflictingHeadersException $e) {
  140.             $profile->setIp('Unknown');
  141.         }
  142.         $response->headers->set('X-Debug-Token'$profile->getToken());
  143.         foreach ($this->collectors as $collector) {
  144.             $collector->collect($request$response$exception);
  145.             // we need to clone for sub-requests
  146.             $profile->addCollector(clone $collector);
  147.         }
  148.         return $profile;
  149.     }
  150.     public function reset()
  151.     {
  152.         foreach ($this->collectors as $collector) {
  153.             if (!method_exists($collector'reset')) {
  154.                 continue;
  155.             }
  156.             $collector->reset();
  157.         }
  158.         $this->enabled $this->initiallyEnabled;
  159.     }
  160.     /**
  161.      * Gets the Collectors associated with this profiler.
  162.      *
  163.      * @return array An array of collectors
  164.      */
  165.     public function all()
  166.     {
  167.         return $this->collectors;
  168.     }
  169.     /**
  170.      * Sets the Collectors associated with this profiler.
  171.      *
  172.      * @param DataCollectorInterface[] $collectors An array of collectors
  173.      */
  174.     public function set(array $collectors = [])
  175.     {
  176.         $this->collectors = [];
  177.         foreach ($collectors as $collector) {
  178.             $this->add($collector);
  179.         }
  180.     }
  181.     /**
  182.      * Adds a Collector.
  183.      */
  184.     public function add(DataCollectorInterface $collector)
  185.     {
  186.         if (!method_exists($collector'reset')) {
  187.             @trigger_error(sprintf('Implementing "%s" without the "reset()" method is deprecated since Symfony 3.4 and will be unsupported in 4.0 for class "%s".'DataCollectorInterface::class, \get_class($collector)), \E_USER_DEPRECATED);
  188.         }
  189.         $this->collectors[$collector->getName()] = $collector;
  190.     }
  191.     /**
  192.      * Returns true if a Collector for the given name exists.
  193.      *
  194.      * @param string $name A collector name
  195.      *
  196.      * @return bool
  197.      */
  198.     public function has($name)
  199.     {
  200.         return isset($this->collectors[$name]);
  201.     }
  202.     /**
  203.      * Gets a Collector by name.
  204.      *
  205.      * @param string $name A collector name
  206.      *
  207.      * @return DataCollectorInterface A DataCollectorInterface instance
  208.      *
  209.      * @throws \InvalidArgumentException if the collector does not exist
  210.      */
  211.     public function get($name)
  212.     {
  213.         if (!isset($this->collectors[$name])) {
  214.             throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.'$name));
  215.         }
  216.         return $this->collectors[$name];
  217.     }
  218.     /**
  219.      * @return int|null
  220.      */
  221.     private function getTimestamp($value)
  222.     {
  223.         if (null === $value || '' == $value) {
  224.             return null;
  225.         }
  226.         try {
  227.             $value = new \DateTime(is_numeric($value) ? '@'.$value $value);
  228.         } catch (\Exception $e) {
  229.             return null;
  230.         }
  231.         return $value->getTimestamp();
  232.     }
  233. }