[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

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


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