[ Index ]

PHP Cross Reference of phpBB-3.1.12-deutsch

title

Body

[close]

/vendor/symfony/http-kernel/Symfony/Component/HttpKernel/Profiler/ -> FileProfilerStorage.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   * Storage for profiler using files.
  16   *
  17   * @author Alexandre Salomé <alexandre.salome@gmail.com>
  18   */
  19  class FileProfilerStorage implements ProfilerStorageInterface
  20  {
  21      /**
  22       * Folder where profiler data are stored.
  23       *
  24       * @var string
  25       */
  26      private $folder;
  27  
  28      /**
  29       * Constructs the file storage using a "dsn-like" path.
  30       *
  31       * Example : "file:/path/to/the/storage/folder"
  32       *
  33       * @param string $dsn The DSN
  34       *
  35       * @throws \RuntimeException
  36       */
  37      public function __construct($dsn)
  38      {
  39          if (0 !== strpos($dsn, 'file:')) {
  40              throw new \RuntimeException(sprintf('Please check your configuration. You are trying to use FileStorage with an invalid dsn "%s". The expected format is "file:/path/to/the/storage/folder".', $dsn));
  41          }
  42          $this->folder = substr($dsn, 5);
  43  
  44          if (!is_dir($this->folder) && false === @mkdir($this->folder, 0777, true) && !is_dir($this->folder)) {
  45              throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $this->folder));
  46          }
  47      }
  48  
  49      /**
  50       * {@inheritdoc}
  51       */
  52      public function find($ip, $url, $limit, $method, $start = null, $end = null)
  53      {
  54          $file = $this->getIndexFilename();
  55  
  56          if (!file_exists($file)) {
  57              return array();
  58          }
  59  
  60          $file = fopen($file, 'r');
  61          fseek($file, 0, SEEK_END);
  62  
  63          $result = array();
  64          while (count($result) < $limit && $line = $this->readLineFromFile($file)) {
  65              list($csvToken, $csvIp, $csvMethod, $csvUrl, $csvTime, $csvParent) = str_getcsv($line);
  66  
  67              $csvTime = (int) $csvTime;
  68  
  69              if ($ip && false === strpos($csvIp, $ip) || $url && false === strpos($csvUrl, $url) || $method && false === strpos($csvMethod, $method)) {
  70                  continue;
  71              }
  72  
  73              if (!empty($start) && $csvTime < $start) {
  74                  continue;
  75              }
  76  
  77              if (!empty($end) && $csvTime > $end) {
  78                  continue;
  79              }
  80  
  81              $result[$csvToken] = array(
  82                  'token' => $csvToken,
  83                  'ip' => $csvIp,
  84                  'method' => $csvMethod,
  85                  'url' => $csvUrl,
  86                  'time' => $csvTime,
  87                  'parent' => $csvParent,
  88              );
  89          }
  90  
  91          fclose($file);
  92  
  93          return array_values($result);
  94      }
  95  
  96      /**
  97       * {@inheritdoc}
  98       */
  99      public function purge()
 100      {
 101          $flags = \FilesystemIterator::SKIP_DOTS;
 102          $iterator = new \RecursiveDirectoryIterator($this->folder, $flags);
 103          $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::CHILD_FIRST);
 104  
 105          foreach ($iterator as $file) {
 106              if (is_file($file)) {
 107                  unlink($file);
 108              } else {
 109                  rmdir($file);
 110              }
 111          }
 112      }
 113  
 114      /**
 115       * {@inheritdoc}
 116       */
 117      public function read($token)
 118      {
 119          if (!$token || !file_exists($file = $this->getFilename($token))) {
 120              return;
 121          }
 122  
 123          return $this->createProfileFromData($token, unserialize(file_get_contents($file)));
 124      }
 125  
 126      /**
 127       * {@inheritdoc}
 128       *
 129       * @throws \RuntimeException
 130       */
 131      public function write(Profile $profile)
 132      {
 133          $file = $this->getFilename($profile->getToken());
 134  
 135          $profileIndexed = is_file($file);
 136          if (!$profileIndexed) {
 137              // Create directory
 138              $dir = dirname($file);
 139              if (!is_dir($dir) && false === @mkdir($dir, 0777, true) && !is_dir($dir)) {
 140                  throw new \RuntimeException(sprintf('Unable to create the storage directory (%s).', $dir));
 141              }
 142          }
 143  
 144          // Store profile
 145          $data = array(
 146              'token' => $profile->getToken(),
 147              'parent' => $profile->getParentToken(),
 148              'children' => array_map(function ($p) { return $p->getToken(); }, $profile->getChildren()),
 149              'data' => $profile->getCollectors(),
 150              'ip' => $profile->getIp(),
 151              'method' => $profile->getMethod(),
 152              'url' => $profile->getUrl(),
 153              'time' => $profile->getTime(),
 154          );
 155  
 156          if (false === file_put_contents($file, serialize($data))) {
 157              return false;
 158          }
 159  
 160          if (!$profileIndexed) {
 161              // Add to index
 162              if (false === $file = fopen($this->getIndexFilename(), 'a')) {
 163                  return false;
 164              }
 165  
 166              fputcsv($file, array(
 167                  $profile->getToken(),
 168                  $profile->getIp(),
 169                  $profile->getMethod(),
 170                  $profile->getUrl(),
 171                  $profile->getTime(),
 172                  $profile->getParentToken(),
 173              ));
 174              fclose($file);
 175          }
 176  
 177          return true;
 178      }
 179  
 180      /**
 181       * Gets filename to store data, associated to the token.
 182       *
 183       * @param string $token
 184       *
 185       * @return string The profile filename
 186       */
 187      protected function getFilename($token)
 188      {
 189          // Uses 4 last characters, because first are mostly the same.
 190          $folderA = substr($token, -2, 2);
 191          $folderB = substr($token, -4, 2);
 192  
 193          return $this->folder.'/'.$folderA.'/'.$folderB.'/'.$token;
 194      }
 195  
 196      /**
 197       * Gets the index filename.
 198       *
 199       * @return string The index filename
 200       */
 201      protected function getIndexFilename()
 202      {
 203          return $this->folder.'/index.csv';
 204      }
 205  
 206      /**
 207       * Reads a line in the file, backward.
 208       *
 209       * This function automatically skips the empty lines and do not include the line return in result value.
 210       *
 211       * @param resource $file The file resource, with the pointer placed at the end of the line to read
 212       *
 213       * @return mixed A string representing the line or null if beginning of file is reached
 214       */
 215      protected function readLineFromFile($file)
 216      {
 217          $line = '';
 218          $position = ftell($file);
 219  
 220          if (0 === $position) {
 221              return;
 222          }
 223  
 224          while (true) {
 225              $chunkSize = min($position, 1024);
 226              $position -= $chunkSize;
 227              fseek($file, $position);
 228  
 229              if (0 === $chunkSize) {
 230                  // bof reached
 231                  break;
 232              }
 233  
 234              $buffer = fread($file, $chunkSize);
 235  
 236              if (false === ($upTo = strrpos($buffer, "\n"))) {
 237                  $line = $buffer.$line;
 238                  continue;
 239              }
 240  
 241              $position += $upTo;
 242              $line = substr($buffer, $upTo + 1).$line;
 243              fseek($file, max(0, $position), SEEK_SET);
 244  
 245              if ('' !== $line) {
 246                  break;
 247              }
 248          }
 249  
 250          return '' === $line ? null : $line;
 251      }
 252  
 253      protected function createProfileFromData($token, $data, $parent = null)
 254      {
 255          $profile = new Profile($token);
 256          $profile->setIp($data['ip']);
 257          $profile->setMethod($data['method']);
 258          $profile->setUrl($data['url']);
 259          $profile->setTime($data['time']);
 260          $profile->setCollectors($data['data']);
 261  
 262          if (!$parent && $data['parent']) {
 263              $parent = $this->read($data['parent']);
 264          }
 265  
 266          if ($parent) {
 267              $profile->setParent($parent);
 268          }
 269  
 270          foreach ($data['children'] as $token) {
 271              if (!$token || !file_exists($file = $this->getFilename($token))) {
 272                  continue;
 273              }
 274  
 275              $profile->addChild($this->createProfileFromData($token, unserialize(file_get_contents($file)), $profile));
 276          }
 277  
 278          return $profile;
 279      }
 280  }


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