[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/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 Symfony\Component\HttpFoundation\Request;
  15  use Symfony\Component\HttpFoundation\Response;
  16  use Symfony\Component\HttpKernel\DataCollector\DataCollectorInterface;
  17  use Psr\Log\LoggerInterface;
  18  
  19  /**
  20   * Profiler.
  21   *
  22   * @author Fabien Potencier <fabien@symfony.com>
  23   */
  24  class Profiler
  25  {
  26      /**
  27       * @var ProfilerStorageInterface
  28       */
  29      private $storage;
  30  
  31      /**
  32       * @var DataCollectorInterface[]
  33       */
  34      private $collectors = array();
  35  
  36      /**
  37       * @var LoggerInterface
  38       */
  39      private $logger;
  40  
  41      /**
  42       * @var bool
  43       */
  44      private $enabled = true;
  45  
  46      /**
  47       * Constructor.
  48       *
  49       * @param ProfilerStorageInterface $storage A ProfilerStorageInterface instance
  50       * @param LoggerInterface          $logger  A LoggerInterface instance
  51       */
  52      public function __construct(ProfilerStorageInterface $storage, LoggerInterface $logger = null)
  53      {
  54          $this->storage = $storage;
  55          $this->logger = $logger;
  56      }
  57  
  58      /**
  59       * Disables the profiler.
  60       */
  61      public function disable()
  62      {
  63          $this->enabled = false;
  64      }
  65  
  66      /**
  67       * Enables the profiler.
  68       */
  69      public function enable()
  70      {
  71          $this->enabled = true;
  72      }
  73  
  74      /**
  75       * Loads the Profile for the given Response.
  76       *
  77       * @param Response $response A Response instance
  78       *
  79       * @return Profile A Profile instance
  80       */
  81      public function loadProfileFromResponse(Response $response)
  82      {
  83          if (!$token = $response->headers->get('X-Debug-Token')) {
  84              return false;
  85          }
  86  
  87          return $this->loadProfile($token);
  88      }
  89  
  90      /**
  91       * Loads the Profile for the given token.
  92       *
  93       * @param string $token A token
  94       *
  95       * @return Profile A Profile instance
  96       */
  97      public function loadProfile($token)
  98      {
  99          return $this->storage->read($token);
 100      }
 101  
 102      /**
 103       * Saves a Profile.
 104       *
 105       * @param Profile $profile A Profile instance
 106       *
 107       * @return bool
 108       */
 109      public function saveProfile(Profile $profile)
 110      {
 111          if (!($ret = $this->storage->write($profile)) && null !== $this->logger) {
 112              $this->logger->warning('Unable to store the profiler information.');
 113          }
 114  
 115          return $ret;
 116      }
 117  
 118      /**
 119       * Purges all data from the storage.
 120       */
 121      public function purge()
 122      {
 123          $this->storage->purge();
 124      }
 125  
 126      /**
 127       * Exports the current profiler data.
 128       *
 129       * @param Profile $profile A Profile instance
 130       *
 131       * @return string The exported data
 132       */
 133      public function export(Profile $profile)
 134      {
 135          return base64_encode(serialize($profile));
 136      }
 137  
 138      /**
 139       * Imports data into the profiler storage.
 140       *
 141       * @param string $data A data string as exported by the export() method
 142       *
 143       * @return Profile A Profile instance
 144       */
 145      public function import($data)
 146      {
 147          $profile = unserialize(base64_decode($data));
 148  
 149          if ($this->storage->read($profile->getToken())) {
 150              return false;
 151          }
 152  
 153          $this->saveProfile($profile);
 154  
 155          return $profile;
 156      }
 157  
 158      /**
 159       * Finds profiler tokens for the given criteria.
 160       *
 161       * @param string $ip     The IP
 162       * @param string $url    The URL
 163       * @param string $limit  The maximum number of tokens to return
 164       * @param string $method The request method
 165       * @param string $start  The start date to search from
 166       * @param string $end    The end date to search to
 167       *
 168       * @return array An array of tokens
 169       *
 170       * @see http://php.net/manual/en/datetime.formats.php for the supported date/time formats
 171       */
 172      public function find($ip, $url, $limit, $method, $start, $end)
 173      {
 174          return $this->storage->find($ip, $url, $limit, $method, $this->getTimestamp($start), $this->getTimestamp($end));
 175      }
 176  
 177      /**
 178       * Collects data for the given Response.
 179       *
 180       * @param Request    $request   A Request instance
 181       * @param Response   $response  A Response instance
 182       * @param \Exception $exception An exception instance if the request threw one
 183       *
 184       * @return Profile|null A Profile instance or null if the profiler is disabled
 185       */
 186      public function collect(Request $request, Response $response, \Exception $exception = null)
 187      {
 188          if (false === $this->enabled) {
 189              return;
 190          }
 191  
 192          $profile = new Profile(substr(sha1(uniqid(mt_rand(), true)), 0, 6));
 193          $profile->setTime(time());
 194          $profile->setUrl($request->getUri());
 195          $profile->setIp($request->getClientIp());
 196          $profile->setMethod($request->getMethod());
 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              // forces collectors to become "read/only" (they lose their object dependencies)
 204              $profile->addCollector(unserialize(serialize($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       * @param DataCollectorInterface $collector A DataCollectorInterface instance
 237       */
 238      public function add(DataCollectorInterface $collector)
 239      {
 240          $this->collectors[$collector->getName()] = $collector;
 241      }
 242  
 243      /**
 244       * Returns true if a Collector for the given name exists.
 245       *
 246       * @param string $name A collector name
 247       *
 248       * @return bool
 249       */
 250      public function has($name)
 251      {
 252          return isset($this->collectors[$name]);
 253      }
 254  
 255      /**
 256       * Gets a Collector by name.
 257       *
 258       * @param string $name A collector name
 259       *
 260       * @return DataCollectorInterface A DataCollectorInterface instance
 261       *
 262       * @throws \InvalidArgumentException if the collector does not exist
 263       */
 264      public function get($name)
 265      {
 266          if (!isset($this->collectors[$name])) {
 267              throw new \InvalidArgumentException(sprintf('Collector "%s" does not exist.', $name));
 268          }
 269  
 270          return $this->collectors[$name];
 271      }
 272  
 273      private function getTimestamp($value)
 274      {
 275          if (null === $value || '' == $value) {
 276              return;
 277          }
 278  
 279          try {
 280              $value = new \DateTime(is_numeric($value) ? '@'.$value : $value);
 281          } catch (\Exception $e) {
 282              return;
 283          }
 284  
 285          return $value->getTimestamp();
 286      }
 287  }


Generated: Thu Jan 11 00:25:41 2018 Cross-referenced by PHPXref 0.7.1