[ Index ]

PHP Cross Reference of phpBB-3.2.8-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Profiler/ -> BaseMemcacheProfilerStorage.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  @trigger_error('The '.__NAMESPACE__.'\BaseMemcacheProfilerStorage class is deprecated since Symfony 2.8 and will be removed in 3.0. Use FileProfilerStorage instead.', E_USER_DEPRECATED);
  15  
  16  /**
  17   * Base Memcache storage for profiling information in a Memcache.
  18   *
  19   * @author Andrej Hudec <pulzarraider@gmail.com>
  20   *
  21   * @deprecated Deprecated since Symfony 2.8, to be removed in Symfony 3.0.
  22   *             Use {@link FileProfilerStorage} instead.
  23   */
  24  abstract class BaseMemcacheProfilerStorage implements ProfilerStorageInterface
  25  {
  26      const TOKEN_PREFIX = 'sf_profiler_';
  27  
  28      protected $dsn;
  29      protected $lifetime;
  30  
  31      /**
  32       * @param string $dsn      A data source name
  33       * @param string $username
  34       * @param string $password
  35       * @param int    $lifetime The lifetime to use for the purge
  36       */
  37      public function __construct($dsn, $username = '', $password = '', $lifetime = 86400)
  38      {
  39          $this->dsn = $dsn;
  40          $this->lifetime = (int) $lifetime;
  41      }
  42  
  43      /**
  44       * {@inheritdoc}
  45       */
  46      public function find($ip, $url, $limit, $method, $start = null, $end = null)
  47      {
  48          $indexName = $this->getIndexName();
  49  
  50          $indexContent = $this->getValue($indexName);
  51          if (!$indexContent) {
  52              return array();
  53          }
  54  
  55          $profileList = explode("\n", $indexContent);
  56          $result = array();
  57  
  58          foreach ($profileList as $item) {
  59              if (0 === $limit) {
  60                  break;
  61              }
  62  
  63              if ('' == $item) {
  64                  continue;
  65              }
  66  
  67              $values = explode("\t", $item, 7);
  68              list($itemToken, $itemIp, $itemMethod, $itemUrl, $itemTime, $itemParent) = $values;
  69              $statusCode = isset($values[6]) ? $values[6] : null;
  70  
  71              $itemTime = (int) $itemTime;
  72  
  73              if ($ip && false === strpos($itemIp, $ip) || $url && false === strpos($itemUrl, $url) || $method && false === strpos($itemMethod, $method)) {
  74                  continue;
  75              }
  76  
  77              if (!empty($start) && $itemTime < $start) {
  78                  continue;
  79              }
  80  
  81              if (!empty($end) && $itemTime > $end) {
  82                  continue;
  83              }
  84  
  85              $result[$itemToken] = array(
  86                  'token' => $itemToken,
  87                  'ip' => $itemIp,
  88                  'method' => $itemMethod,
  89                  'url' => $itemUrl,
  90                  'time' => $itemTime,
  91                  'parent' => $itemParent,
  92                  'status_code' => $statusCode,
  93              );
  94              --$limit;
  95          }
  96  
  97          usort($result, function ($a, $b) {
  98              if ($a['time'] === $b['time']) {
  99                  return 0;
 100              }
 101  
 102              return $a['time'] > $b['time'] ? -1 : 1;
 103          });
 104  
 105          return $result;
 106      }
 107  
 108      /**
 109       * {@inheritdoc}
 110       */
 111      public function purge()
 112      {
 113          // delete only items from index
 114          $indexName = $this->getIndexName();
 115  
 116          $indexContent = $this->getValue($indexName);
 117  
 118          if (!$indexContent) {
 119              return false;
 120          }
 121  
 122          $profileList = explode("\n", $indexContent);
 123  
 124          foreach ($profileList as $item) {
 125              if ('' == $item) {
 126                  continue;
 127              }
 128  
 129              if (false !== $pos = strpos($item, "\t")) {
 130                  $this->delete($this->getItemName(substr($item, 0, $pos)));
 131              }
 132          }
 133  
 134          return $this->delete($indexName);
 135      }
 136  
 137      /**
 138       * {@inheritdoc}
 139       */
 140      public function read($token)
 141      {
 142          if (empty($token)) {
 143              return false;
 144          }
 145  
 146          $profile = $this->getValue($this->getItemName($token));
 147  
 148          if (false !== $profile) {
 149              $profile = $this->createProfileFromData($token, $profile);
 150          }
 151  
 152          return $profile;
 153      }
 154  
 155      /**
 156       * {@inheritdoc}
 157       */
 158      public function write(Profile $profile)
 159      {
 160          $data = array(
 161              'token' => $profile->getToken(),
 162              'parent' => $profile->getParentToken(),
 163              'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
 164              'data' => $profile->getCollectors(),
 165              'ip' => $profile->getIp(),
 166              'method' => $profile->getMethod(),
 167              'url' => $profile->getUrl(),
 168              'time' => $profile->getTime(),
 169          );
 170  
 171          $profileIndexed = false !== $this->getValue($this->getItemName($profile->getToken()));
 172  
 173          if ($this->setValue($this->getItemName($profile->getToken()), $data, $this->lifetime)) {
 174              if (!$profileIndexed) {
 175                  // Add to index
 176                  $indexName = $this->getIndexName();
 177  
 178                  $indexRow = implode("\t", array(
 179                      $profile->getToken(),
 180                      $profile->getIp(),
 181                      $profile->getMethod(),
 182                      $profile->getUrl(),
 183                      $profile->getTime(),
 184                      $profile->getParentToken(),
 185                      $profile->getStatusCode(),
 186                  ))."\n";
 187  
 188                  return $this->appendValue($indexName, $indexRow, $this->lifetime);
 189              }
 190  
 191              return true;
 192          }
 193  
 194          return false;
 195      }
 196  
 197      /**
 198       * Retrieve item from the memcache server.
 199       *
 200       * @param string $key
 201       *
 202       * @return mixed
 203       */
 204      abstract protected function getValue($key);
 205  
 206      /**
 207       * Store an item on the memcache server under the specified key.
 208       *
 209       * @param string $key
 210       * @param mixed  $value
 211       * @param int    $expiration
 212       *
 213       * @return bool
 214       */
 215      abstract protected function setValue($key, $value, $expiration = 0);
 216  
 217      /**
 218       * Delete item from the memcache server.
 219       *
 220       * @param string $key
 221       *
 222       * @return bool
 223       */
 224      abstract protected function delete($key);
 225  
 226      /**
 227       * Append data to an existing item on the memcache server.
 228       *
 229       * @param string $key
 230       * @param string $value
 231       * @param int    $expiration
 232       *
 233       * @return bool
 234       */
 235      abstract protected function appendValue($key, $value, $expiration = 0);
 236  
 237      private function createProfileFromData($token, $data, $parent = null)
 238      {
 239          $profile = new Profile($token);
 240          $profile->setIp($data['ip']);
 241          $profile->setMethod($data['method']);
 242          $profile->setUrl($data['url']);
 243          $profile->setTime($data['time']);
 244          $profile->setCollectors($data['data']);
 245  
 246          if (!$parent && $data['parent']) {
 247              $parent = $this->read($data['parent']);
 248          }
 249  
 250          if ($parent) {
 251              $profile->setParent($parent);
 252          }
 253  
 254          foreach ($data['children'] as $token) {
 255              if (!$token) {
 256                  continue;
 257              }
 258  
 259              if (!$childProfileData = $this->getValue($this->getItemName($token))) {
 260                  continue;
 261              }
 262  
 263              $profile->addChild($this->createProfileFromData($token, $childProfileData, $profile));
 264          }
 265  
 266          return $profile;
 267      }
 268  
 269      /**
 270       * Get item name.
 271       *
 272       * @param string $token
 273       *
 274       * @return string
 275       */
 276      private function getItemName($token)
 277      {
 278          $name = self::TOKEN_PREFIX.$token;
 279  
 280          if ($this->isItemNameValid($name)) {
 281              return $name;
 282          }
 283  
 284          return false;
 285      }
 286  
 287      /**
 288       * Get name of index.
 289       *
 290       * @return string
 291       */
 292      private function getIndexName()
 293      {
 294          $name = self::TOKEN_PREFIX.'index';
 295  
 296          if ($this->isItemNameValid($name)) {
 297              return $name;
 298          }
 299  
 300          return false;
 301      }
 302  
 303      private function isItemNameValid($name)
 304      {
 305          $length = \strlen($name);
 306  
 307          if ($length > 250) {
 308              throw new \RuntimeException(sprintf('The memcache item key "%s" is too long (%s bytes). Allowed maximum size is 250 bytes.', $name, $length));
 309          }
 310  
 311          return true;
 312      }
 313  }


Generated: Tue Apr 7 19:42:26 2020 Cross-referenced by PHPXref 0.7.1