[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

/vendor/symfony/config/Resource/ -> GlobResource.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\Config\Resource;
  13  
  14  use Symfony\Component\Finder\Finder;
  15  use Symfony\Component\Finder\Glob;
  16  
  17  /**
  18   * GlobResource represents a set of resources stored on the filesystem.
  19   *
  20   * Only existence/removal is tracked (not mtimes.)
  21   *
  22   * @author Nicolas Grekas <p@tchwork.com>
  23   */
  24  class GlobResource implements \IteratorAggregate, SelfCheckingResourceInterface, \Serializable
  25  {
  26      private $prefix;
  27      private $pattern;
  28      private $recursive;
  29      private $hash;
  30  
  31      /**
  32       * @param string $prefix    A directory prefix
  33       * @param string $pattern   A glob pattern
  34       * @param bool   $recursive Whether directories should be scanned recursively or not
  35       *
  36       * @throws \InvalidArgumentException
  37       */
  38      public function __construct($prefix, $pattern, $recursive)
  39      {
  40          $this->prefix = realpath($prefix) ?: (file_exists($prefix) ? $prefix : false);
  41          $this->pattern = $pattern;
  42          $this->recursive = $recursive;
  43  
  44          if (false === $this->prefix) {
  45              throw new \InvalidArgumentException(sprintf('The path "%s" does not exist.', $prefix));
  46          }
  47      }
  48  
  49      public function getPrefix()
  50      {
  51          return $this->prefix;
  52      }
  53  
  54      /**
  55       * {@inheritdoc}
  56       */
  57      public function __toString()
  58      {
  59          return 'glob.'.$this->prefix.$this->pattern.(int) $this->recursive;
  60      }
  61  
  62      /**
  63       * {@inheritdoc}
  64       */
  65      public function isFresh($timestamp)
  66      {
  67          $hash = $this->computeHash();
  68  
  69          if (null === $this->hash) {
  70              $this->hash = $hash;
  71          }
  72  
  73          return $this->hash === $hash;
  74      }
  75  
  76      /**
  77       * @internal
  78       */
  79      public function serialize()
  80      {
  81          if (null === $this->hash) {
  82              $this->hash = $this->computeHash();
  83          }
  84  
  85          return serialize([$this->prefix, $this->pattern, $this->recursive, $this->hash]);
  86      }
  87  
  88      /**
  89       * @internal
  90       */
  91      public function unserialize($serialized)
  92      {
  93          list($this->prefix, $this->pattern, $this->recursive, $this->hash) = unserialize($serialized);
  94      }
  95  
  96      public function getIterator()
  97      {
  98          if (!file_exists($this->prefix) || (!$this->recursive && '' === $this->pattern)) {
  99              return;
 100          }
 101  
 102          if (0 !== strpos($this->prefix, 'phar://') && false === strpos($this->pattern, '/**/') && (\defined('GLOB_BRACE') || false === strpos($this->pattern, '{'))) {
 103              $paths = glob($this->prefix.$this->pattern, \GLOB_NOSORT | (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0));
 104              sort($paths);
 105              foreach ($paths as $path) {
 106                  if ($this->recursive && is_dir($path)) {
 107                      $files = iterator_to_array(new \RecursiveIteratorIterator(
 108                          new \RecursiveCallbackFilterIterator(
 109                              new \RecursiveDirectoryIterator($path, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
 110                              function (\SplFileInfo $file) { return '.' !== $file->getBasename()[0]; }
 111                          ),
 112                          \RecursiveIteratorIterator::LEAVES_ONLY
 113                      ));
 114                      uasort($files, function (\SplFileInfo $a, \SplFileInfo $b) {
 115                          return (string) $a > (string) $b ? 1 : -1;
 116                      });
 117  
 118                      foreach ($files as $path => $info) {
 119                          if ($info->isFile()) {
 120                              yield $path => $info;
 121                          }
 122                      }
 123                  } elseif (is_file($path)) {
 124                      yield $path => new \SplFileInfo($path);
 125                  }
 126              }
 127  
 128              return;
 129          }
 130  
 131          if (!class_exists(Finder::class)) {
 132              throw new \LogicException(sprintf('Extended glob pattern "%s" cannot be used as the Finder component is not installed.', $this->pattern));
 133          }
 134  
 135          $finder = new Finder();
 136          $regex = Glob::toRegex($this->pattern);
 137          if ($this->recursive) {
 138              $regex = substr_replace($regex, '(/|$)', -2, 1);
 139          }
 140  
 141          $prefixLen = \strlen($this->prefix);
 142          foreach ($finder->followLinks()->sortByName()->in($this->prefix) as $path => $info) {
 143              if (preg_match($regex, substr('\\' === \DIRECTORY_SEPARATOR ? str_replace('\\', '/', $path) : $path, $prefixLen)) && $info->isFile()) {
 144                  yield $path => $info;
 145              }
 146          }
 147      }
 148  
 149      private function computeHash()
 150      {
 151          $hash = hash_init('md5');
 152  
 153          foreach ($this->getIterator() as $path => $info) {
 154              hash_update($hash, $path."\n");
 155          }
 156  
 157          return hash_final($hash);
 158      }
 159  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1