[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Profiler/ -> Profiler.php (source)

   1  <?php
   2  
   3  /*
   4   * This file is part of the Symfony package.
   5   *
   6   * (c) Fabien Potencier <fabien@symfony.com>
   7   *
   8   * For the full copyright and license information, please view the LICENSE
   9   * file that was distributed with this source code.
  10   */
  11  
  12  namespace Symfony\Component\HttpKernel\Profiler;
  13  
  14  use Psr\Log\LoggerInterface;
  15  use Symfony\Component\HttpFoundation\Exception\ConflictingHeadersException;
  16  use Symfony\Component\HttpFoundation\Request;
  17  use Symfony\Component\HttpFoundation\Response;
  18  use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  19  use Symfony\Component\HttpKernel\DataCollector\LateDataCollectorInterface;
  20  
  21  /**
  22   * Profiler.
  23   *
  24   * @author Fabien Potencier <fabien@symfony.com>
  25   */
  26  class Profiler
  27  {
  28      private $storage;
  29  
  30      /**
  31       * @var DataCollectorInterface[]
  32       */
  33      private $collectors = array();
  34  
  35      private $logger;
  36  
  37      /**
  38       * @var bool
  39       */
  40      private $enabled = true;
  41  
  42      public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
  43      {
  44          $this->storage = $storage;
  45          $this->logger = $logger;
  46      }
  47  
  48      /**
  49       * Disables the profiler.
  50       */
  51      public function disable()
  52      {
  53          $this->enabled = false;
  54      }
  55  
  56      /**
  57       * Enables the profiler.
  58       */
  59      public function enable()
  60      {
  61          $this->enabled = true;
  62      }
  63  
  64      /**
  65       * Loads the Profile for the given Response.
  66       *
  67       * @return Profile|false A Profile instance
  68       */
  69      public function loadProfileFromResponse(Response $response)
  70      {
  71          if (!$token = $response->headers->get('X-Debug-Token')) {
  72              return false;
  73          }
  74  
  75          return $this->loadProfile($token);
  76      }
  77  
  78      /**
  79       * Loads the Profile for the given token.
  80       *
  81       * @param string $token A token
  82       *
  83       * @return Profile A Profile instance
  84       */
  85      public function loadProfile($token)
  86      {
  87          return $this->storage->read($token);
  88      }
  89  
  90      /**
  91       * Saves a Profile.
  92       *
  93       * @return bool
  94       */
  95      public function saveProfile(Profile $profile)
  96      {
  97          // late collect
  98          foreach ($profile->getCollectors() as $collector) {
  99              if ($collector instanceof LateDataCollectorInterface) {
 100                  $collector->lateCollect();
 101              }
 102          }
 103  
 104          if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
 105              $this->logger->warning('Unable to store the profiler information.', array('configured_storage' => \get_class($this->storage)));
 106          }
 107  
 108          return $ret;
 109      }
 110  
 111      /**
 112       * Purges all data from the storage.
 113       */
 114      public function purge()
 115      {
 116          $this->storage->purge();
 117      }
 118  
 119      /**
 120       * Exports the current profiler data.
 121       *
 122       * @return string The exported data
 123       *
 124       * @deprecated since Symfony 2.8, to be removed in 3.0.
 125       */
 126      public function export(Profile $profile)
 127      {
 128          @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
 129  
 130          return base64_encode(serialize($profile));
 131      }
 132  
 133      /**
 134       * Imports data into the profiler storage.
 135       *
 136       * @param string $data A data string as exported by the export() method
 137       *
 138       * @return Profile|false A Profile instance
 139       *
 140       * @deprecated since Symfony 2.8, to be removed in 3.0.
 141       */
 142      public function import($data)
 143      {
 144          @trigger_error('The '.__METHOD__.' method is deprecated since Symfony 2.8 and will be removed in 3.0.', E_USER_DEPRECATED);
 145  
 146          $profile = unserialize(base64_decode($data));
 147  
 148          if ($this->storage->read($profile->getToken())) {
 149              return false;
 150          }
 151  
 152          $this->saveProfile($profile);
 153  
 154          return $profile;
 155      }
 156  
 157      /**
 158       * Finds profiler tokens for the given criteria.
 159       *
 160       * @param string $ip     The IP
 161       * @param string $url    The URL
 162       * @param string $limit  The maximum number of tokens to return
 163       * @param string $method The request method
 164       * @param string $start  The start date to search from
 165       * @param string $end    The end date to search to
 166       *
 167       * @return array An array of tokens
 168       *
 169       * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
 170       */
 171      public function find($ip, $url, $limit, $method, $start, $end)
 172      {
 173          return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
 174      }
 175  
 176      /**
 177       * Collects data for the given Response.
 178       *
 179       * @return Profile|null A Profile instance or null if the profiler is disabled
 180       */
 181      public function collect(Request $request, Response $response, \Exception $exception = null)
 182      {
 183          if (false === $this->enabled) {
 184              return;
 185          }
 186  
 187          $profile = new Profile(substr(hash('sha256', uniqid(mt_rand(), true)), 0, 6));
 188          $profile->setTime(time());
 189          $profile->setUrl($request->getUri());
 190          $profile->setMethod($request->getMethod());
 191          $profile->setStatusCode($response->getStatusCode());
 192          try {
 193              $profile->setIp($request->getClientIp());
 194          } catch (ConflictingHeadersException $e) {
 195              $profile->setIp('Unknown');
 196          }
 197  
 198          $response->headers->set('X-Debug-Token', $profile->getToken());
 199  
 200          foreach ($this->collectors as $collector) {
 201              $collector->collect($request, $response, $exception);
 202  
 203              // we need to clone for sub-requests
 204              $profile->addCollector(clone $collector);
 205          }
 206  
 207          return $profile;
 208      }
 209  
 210      /**
 211       * Gets the Collectors associated with this profiler.
 212       *
 213       * @return array An array of collectors
 214       */
 215      public function all()
 216      {
 217          return $this->collectors;
 218      }
 219  
 220      /**
 221       * Sets the Collectors associated with this profiler.
 222       *
 223       * @param DataCollectorInterface[] $collectors An array of collectors
 224       */
 225      public function set(array $collectors = array())
 226      {
 227          $this->collectors = array();
 228          foreach ($collectors as $collector) {
 229              $this->add($collector);
 230          }
 231      }
 232  
 233      /**
 234       * Adds a Collector.
 235       */
 236      public function add(DataCollectorInterface $collector)
 237      {
 238          $this->collectors[$collector->getName()] = $collector;
 239      }
 240  
 241      /**
 242       * Returns true if a Collector for the given name exists.
 243       *
 244       * @param string $name A collector name
 245       *
 246       * @return bool
 247       */
 248      public function has($name)
 249      {
 250          return isset($this->collectors[$name]);
 251      }
 252  
 253      /**
 254       * Gets a Collector by name.
 255       *
 256       * @param string $name A collector name
 257       *
 258       * @return DataCollectorInterface A DataCollectorInterface instance
 259       *
 260       * @throws \InvalidArgumentException if the collector does not exist
 261       */
 262      public function get($name)
 263      {
 264          if (!isset($this->collectors[$name])) {
 265              throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
 266          }
 267  
 268          return $this->collectors[$name];
 269      }
 270  
 271      private function getTimestamp($value)
 272      {
 273          if (null === $value || '' == $value) {
 274              return;
 275          }
 276  
 277          try {
 278              $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
 279          } catch (\Exception $e) {
 280              return;
 281          }
 282  
 283          return $value->getTimestamp();
 284      }
 285  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1