GlobResource.php 6.1 KB

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