comparison vendor/symfony/finder/Finder.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children 12f9dff5fda9
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
29 * 29 *
30 * All rules may be invoked several times. 30 * All rules may be invoked several times.
31 * 31 *
32 * All methods return the current Finder object to allow easy chaining: 32 * All methods return the current Finder object to allow easy chaining:
33 * 33 *
34 * $finder = Finder::create()->files()->name('*.php')->in(__DIR__); 34 * $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
35 * 35 *
36 * @author Fabien Potencier <fabien@symfony.com> 36 * @author Fabien Potencier <fabien@symfony.com>
37 */ 37 */
38 class Finder implements \IteratorAggregate, \Countable 38 class Finder implements \IteratorAggregate, \Countable
39 { 39 {
40 const IGNORE_VCS_FILES = 1; 40 const IGNORE_VCS_FILES = 1;
41 const IGNORE_DOT_FILES = 2; 41 const IGNORE_DOT_FILES = 2;
42 42
43 private $mode = 0; 43 private $mode = 0;
44 private $names = array(); 44 private $names = [];
45 private $notNames = array(); 45 private $notNames = [];
46 private $exclude = array(); 46 private $exclude = [];
47 private $filters = array(); 47 private $filters = [];
48 private $depths = array(); 48 private $depths = [];
49 private $sizes = array(); 49 private $sizes = [];
50 private $followLinks = false; 50 private $followLinks = false;
51 private $sort = false; 51 private $sort = false;
52 private $ignore = 0; 52 private $ignore = 0;
53 private $dirs = array(); 53 private $dirs = [];
54 private $dates = array(); 54 private $dates = [];
55 private $iterators = array(); 55 private $iterators = [];
56 private $contains = array(); 56 private $contains = [];
57 private $notContains = array(); 57 private $notContains = [];
58 private $paths = array(); 58 private $paths = [];
59 private $notPaths = array(); 59 private $notPaths = [];
60 private $ignoreUnreadableDirs = false; 60 private $ignoreUnreadableDirs = false;
61 61
62 private static $vcsPatterns = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'); 62 private static $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
63 63
64 public function __construct() 64 public function __construct()
65 { 65 {
66 $this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES; 66 $this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
67 } 67 }
103 /** 103 /**
104 * Adds tests for the directory depth. 104 * Adds tests for the directory depth.
105 * 105 *
106 * Usage: 106 * Usage:
107 * 107 *
108 * $finder->depth('> 1') // the Finder will start matching at level 1. 108 * $finder->depth('> 1') // the Finder will start matching at level 1.
109 * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point. 109 * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
110 * 110 *
111 * @param string|int $level The depth level expression 111 * @param string|int $level The depth level expression
112 * 112 *
113 * @return $this 113 * @return $this
114 * 114 *
125 /** 125 /**
126 * Adds tests for file dates (last modified). 126 * Adds tests for file dates (last modified).
127 * 127 *
128 * The date must be something that strtotime() is able to parse: 128 * The date must be something that strtotime() is able to parse:
129 * 129 *
130 * $finder->date('since yesterday'); 130 * $finder->date('since yesterday');
131 * $finder->date('until 2 days ago'); 131 * $finder->date('until 2 days ago');
132 * $finder->date('> now - 2 hours'); 132 * $finder->date('> now - 2 hours');
133 * $finder->date('>= 2005-10-15'); 133 * $finder->date('>= 2005-10-15');
134 * 134 *
135 * @param string $date A date range string 135 * @param string $date A date range string
136 * 136 *
137 * @return $this 137 * @return $this
138 * 138 *
150 /** 150 /**
151 * Adds rules that files must match. 151 * Adds rules that files must match.
152 * 152 *
153 * You can use patterns (delimited with / sign), globs or simple strings. 153 * You can use patterns (delimited with / sign), globs or simple strings.
154 * 154 *
155 * $finder->name('*.php') 155 * $finder->name('*.php')
156 * $finder->name('/\.php$/') // same as above 156 * $finder->name('/\.php$/') // same as above
157 * $finder->name('test.php') 157 * $finder->name('test.php')
158 * 158 *
159 * @param string $pattern A pattern (a regexp, a glob, or a string) 159 * @param string $pattern A pattern (a regexp, a glob, or a string)
160 * 160 *
161 * @return $this 161 * @return $this
162 * 162 *
188 /** 188 /**
189 * Adds tests that file contents must match. 189 * Adds tests that file contents must match.
190 * 190 *
191 * Strings or PCRE patterns can be used: 191 * Strings or PCRE patterns can be used:
192 * 192 *
193 * $finder->contains('Lorem ipsum') 193 * $finder->contains('Lorem ipsum')
194 * $finder->contains('/Lorem ipsum/i') 194 * $finder->contains('/Lorem ipsum/i')
195 * 195 *
196 * @param string $pattern A pattern (string or regexp) 196 * @param string $pattern A pattern (string or regexp)
197 * 197 *
198 * @return $this 198 * @return $this
199 * 199 *
209 /** 209 /**
210 * Adds tests that file contents must not match. 210 * Adds tests that file contents must not match.
211 * 211 *
212 * Strings or PCRE patterns can be used: 212 * Strings or PCRE patterns can be used:
213 * 213 *
214 * $finder->notContains('Lorem ipsum') 214 * $finder->notContains('Lorem ipsum')
215 * $finder->notContains('/Lorem ipsum/i') 215 * $finder->notContains('/Lorem ipsum/i')
216 * 216 *
217 * @param string $pattern A pattern (string or regexp) 217 * @param string $pattern A pattern (string or regexp)
218 * 218 *
219 * @return $this 219 * @return $this
220 * 220 *
230 /** 230 /**
231 * Adds rules that filenames must match. 231 * Adds rules that filenames must match.
232 * 232 *
233 * You can use patterns (delimited with / sign) or simple strings. 233 * You can use patterns (delimited with / sign) or simple strings.
234 * 234 *
235 * $finder->path('some/special/dir') 235 * $finder->path('some/special/dir')
236 * $finder->path('/some\/special\/dir/') // same as above 236 * $finder->path('/some\/special\/dir/') // same as above
237 * 237 *
238 * Use only / as dirname separator. 238 * Use only / as dirname separator.
239 * 239 *
240 * @param string $pattern A pattern (a regexp or a string) 240 * @param string $pattern A pattern (a regexp or a string)
241 * 241 *
253 /** 253 /**
254 * Adds rules that filenames must not match. 254 * Adds rules that filenames must not match.
255 * 255 *
256 * You can use patterns (delimited with / sign) or simple strings. 256 * You can use patterns (delimited with / sign) or simple strings.
257 * 257 *
258 * $finder->notPath('some/special/dir') 258 * $finder->notPath('some/special/dir')
259 * $finder->notPath('/some\/special\/dir/') // same as above 259 * $finder->notPath('/some\/special\/dir/') // same as above
260 * 260 *
261 * Use only / as dirname separator. 261 * Use only / as dirname separator.
262 * 262 *
263 * @param string $pattern A pattern (a regexp or a string) 263 * @param string $pattern A pattern (a regexp or a string)
264 * 264 *
274 } 274 }
275 275
276 /** 276 /**
277 * Adds tests for file sizes. 277 * Adds tests for file sizes.
278 * 278 *
279 * $finder->size('> 10K'); 279 * $finder->size('> 10K');
280 * $finder->size('<= 1Ki'); 280 * $finder->size('<= 1Ki');
281 * $finder->size(4); 281 * $finder->size(4);
282 * 282 *
283 * @param string|int $size A size range string or an integer 283 * @param string|int $size A size range string or an integer
284 * 284 *
285 * @return $this 285 * @return $this
286 * 286 *
534 * 534 *
535 * @throws \InvalidArgumentException if one of the directories does not exist 535 * @throws \InvalidArgumentException if one of the directories does not exist
536 */ 536 */
537 public function in($dirs) 537 public function in($dirs)
538 { 538 {
539 $resolvedDirs = array(); 539 $resolvedDirs = [];
540 540
541 foreach ((array) $dirs as $dir) { 541 foreach ((array) $dirs as $dir) {
542 if (is_dir($dir)) { 542 if (is_dir($dir)) {
543 $resolvedDirs[] = $this->normalizeDir($dir); 543 $resolvedDirs[] = $this->normalizeDir($dir);
544 } elseif ($glob = glob($dir, (defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) { 544 } elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
545 $resolvedDirs = array_merge($resolvedDirs, array_map(array($this, 'normalizeDir'), $glob)); 545 $resolvedDirs = array_merge($resolvedDirs, array_map([$this, 'normalizeDir'], $glob));
546 } else { 546 } else {
547 throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir)); 547 throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
548 } 548 }
549 } 549 }
550 550
562 * 562 *
563 * @throws \LogicException if the in() method has not been called 563 * @throws \LogicException if the in() method has not been called
564 */ 564 */
565 public function getIterator() 565 public function getIterator()
566 { 566 {
567 if (0 === count($this->dirs) && 0 === count($this->iterators)) { 567 if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
568 throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.'); 568 throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
569 } 569 }
570 570
571 if (1 === count($this->dirs) && 0 === count($this->iterators)) { 571 if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
572 return $this->searchInDirectory($this->dirs[0]); 572 return $this->searchInDirectory($this->dirs[0]);
573 } 573 }
574 574
575 $iterator = new \AppendIterator(); 575 $iterator = new \AppendIterator();
576 foreach ($this->dirs as $dir) { 576 foreach ($this->dirs as $dir) {
587 /** 587 /**
588 * Appends an existing set of files/directories to the finder. 588 * Appends an existing set of files/directories to the finder.
589 * 589 *
590 * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array. 590 * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
591 * 591 *
592 * @param mixed $iterator 592 * @param iterable $iterator
593 * 593 *
594 * @return $this 594 * @return $this
595 * 595 *
596 * @throws \InvalidArgumentException when the given argument is not iterable 596 * @throws \InvalidArgumentException when the given argument is not iterable
597 */ 597 */
599 { 599 {
600 if ($iterator instanceof \IteratorAggregate) { 600 if ($iterator instanceof \IteratorAggregate) {
601 $this->iterators[] = $iterator->getIterator(); 601 $this->iterators[] = $iterator->getIterator();
602 } elseif ($iterator instanceof \Iterator) { 602 } elseif ($iterator instanceof \Iterator) {
603 $this->iterators[] = $iterator; 603 $this->iterators[] = $iterator;
604 } elseif ($iterator instanceof \Traversable || is_array($iterator)) { 604 } elseif ($iterator instanceof \Traversable || \is_array($iterator)) {
605 $it = new \ArrayIterator(); 605 $it = new \ArrayIterator();
606 foreach ($iterator as $file) { 606 foreach ($iterator as $file) {
607 $it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file)); 607 $it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file));
608 } 608 }
609 $this->iterators[] = $it; 609 $this->iterators[] = $it;
637 { 637 {
638 return iterator_count($this->getIterator()); 638 return iterator_count($this->getIterator());
639 } 639 }
640 640
641 /** 641 /**
642 * @param $dir 642 * @param string $dir
643 * 643 *
644 * @return \Iterator 644 * @return \Iterator
645 */ 645 */
646 private function searchInDirectory($dir) 646 private function searchInDirectory($dir)
647 { 647 {
730 } 730 }
731 731
732 /** 732 /**
733 * Normalizes given directory names by removing trailing slashes. 733 * Normalizes given directory names by removing trailing slashes.
734 * 734 *
735 * Excluding: (s)ftp:// wrapper
736 *
735 * @param string $dir 737 * @param string $dir
736 * 738 *
737 * @return string 739 * @return string
738 */ 740 */
739 private function normalizeDir($dir) 741 private function normalizeDir($dir)
740 { 742 {
741 return rtrim($dir, '/'.\DIRECTORY_SEPARATOR); 743 $dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
744
745 if (preg_match('#^s?ftp://#', $dir)) {
746 $dir .= '/';
747 }
748
749 return $dir;
742 } 750 }
743 } 751 }