Finder.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856
  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\Finder;
  11. use Symfony\Component\Finder\Comparator\DateComparator;
  12. use Symfony\Component\Finder\Comparator\NumberComparator;
  13. use Symfony\Component\Finder\Exception\DirectoryNotFoundException;
  14. use Symfony\Component\Finder\Iterator\CustomFilterIterator;
  15. use Symfony\Component\Finder\Iterator\DateRangeFilterIterator;
  16. use Symfony\Component\Finder\Iterator\DepthRangeFilterIterator;
  17. use Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator;
  18. use Symfony\Component\Finder\Iterator\FilecontentFilterIterator;
  19. use Symfony\Component\Finder\Iterator\FilenameFilterIterator;
  20. use Symfony\Component\Finder\Iterator\LazyIterator;
  21. use Symfony\Component\Finder\Iterator\SizeRangeFilterIterator;
  22. use Symfony\Component\Finder\Iterator\SortableIterator;
  23. /**
  24. * Finder allows to build rules to find files and directories.
  25. *
  26. * It is a thin wrapper around several specialized iterator classes.
  27. *
  28. * All rules may be invoked several times.
  29. *
  30. * All methods return the current Finder object to allow chaining:
  31. *
  32. * $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
  33. *
  34. * @author Fabien Potencier <fabien@symfony.com>
  35. *
  36. * @implements \IteratorAggregate<string, SplFileInfo>
  37. */
  38. class Finder implements \IteratorAggregate, \Countable
  39. {
  40. public const IGNORE_VCS_FILES = 1;
  41. public const IGNORE_DOT_FILES = 2;
  42. public const IGNORE_VCS_IGNORED_FILES = 4;
  43. private int $mode = 0;
  44. private array $names = [];
  45. private array $notNames = [];
  46. private array $exclude = [];
  47. private array $filters = [];
  48. private array $pruneFilters = [];
  49. private array $depths = [];
  50. private array $sizes = [];
  51. private bool $followLinks = false;
  52. private bool $reverseSorting = false;
  53. private \Closure|int|false $sort = false;
  54. private int $ignore = 0;
  55. private array $dirs = [];
  56. private array $dates = [];
  57. private array $iterators = [];
  58. private array $contains = [];
  59. private array $notContains = [];
  60. private array $paths = [];
  61. private array $notPaths = [];
  62. private bool $ignoreUnreadableDirs = false;
  63. private static array $vcsPatterns = ['.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg'];
  64. public function __construct()
  65. {
  66. $this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
  67. }
  68. /**
  69. * Creates a new Finder.
  70. */
  71. public static function create(): static
  72. {
  73. return new static();
  74. }
  75. /**
  76. * Restricts the matching to directories only.
  77. *
  78. * @return $this
  79. */
  80. public function directories(): static
  81. {
  82. $this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
  83. return $this;
  84. }
  85. /**
  86. * Restricts the matching to files only.
  87. *
  88. * @return $this
  89. */
  90. public function files(): static
  91. {
  92. $this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
  93. return $this;
  94. }
  95. /**
  96. * Adds tests for the directory depth.
  97. *
  98. * Usage:
  99. *
  100. * $finder->depth('> 1') // the Finder will start matching at level 1.
  101. * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
  102. * $finder->depth(['>= 1', '< 3'])
  103. *
  104. * @param string|int|string[]|int[] $levels The depth level expression or an array of depth levels
  105. *
  106. * @return $this
  107. *
  108. * @see DepthRangeFilterIterator
  109. * @see NumberComparator
  110. */
  111. public function depth(string|int|array $levels): static
  112. {
  113. foreach ((array) $levels as $level) {
  114. $this->depths[] = new NumberComparator($level);
  115. }
  116. return $this;
  117. }
  118. /**
  119. * Adds tests for file dates (last modified).
  120. *
  121. * The date must be something that strtotime() is able to parse:
  122. *
  123. * $finder->date('since yesterday');
  124. * $finder->date('until 2 days ago');
  125. * $finder->date('> now - 2 hours');
  126. * $finder->date('>= 2005-10-15');
  127. * $finder->date(['>= 2005-10-15', '<= 2006-05-27']);
  128. *
  129. * @param string|string[] $dates A date range string or an array of date ranges
  130. *
  131. * @return $this
  132. *
  133. * @see strtotime
  134. * @see DateRangeFilterIterator
  135. * @see DateComparator
  136. */
  137. public function date(string|array $dates): static
  138. {
  139. foreach ((array) $dates as $date) {
  140. $this->dates[] = new DateComparator($date);
  141. }
  142. return $this;
  143. }
  144. /**
  145. * Adds rules that files must match.
  146. *
  147. * You can use patterns (delimited with / sign), globs or simple strings.
  148. *
  149. * $finder->name('/\.php$/')
  150. * $finder->name('*.php') // same as above, without dot files
  151. * $finder->name('test.php')
  152. * $finder->name(['test.py', 'test.php'])
  153. *
  154. * @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
  155. *
  156. * @return $this
  157. *
  158. * @see FilenameFilterIterator
  159. */
  160. public function name(string|array $patterns): static
  161. {
  162. $this->names = array_merge($this->names, (array) $patterns);
  163. return $this;
  164. }
  165. /**
  166. * Adds rules that files must not match.
  167. *
  168. * @param string|string[] $patterns A pattern (a regexp, a glob, or a string) or an array of patterns
  169. *
  170. * @return $this
  171. *
  172. * @see FilenameFilterIterator
  173. */
  174. public function notName(string|array $patterns): static
  175. {
  176. $this->notNames = array_merge($this->notNames, (array) $patterns);
  177. return $this;
  178. }
  179. /**
  180. * Adds tests that file contents must match.
  181. *
  182. * Strings or PCRE patterns can be used:
  183. *
  184. * $finder->contains('Lorem ipsum')
  185. * $finder->contains('/Lorem ipsum/i')
  186. * $finder->contains(['dolor', '/ipsum/i'])
  187. *
  188. * @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
  189. *
  190. * @return $this
  191. *
  192. * @see FilecontentFilterIterator
  193. */
  194. public function contains(string|array $patterns): static
  195. {
  196. $this->contains = array_merge($this->contains, (array) $patterns);
  197. return $this;
  198. }
  199. /**
  200. * Adds tests that file contents must not match.
  201. *
  202. * Strings or PCRE patterns can be used:
  203. *
  204. * $finder->notContains('Lorem ipsum')
  205. * $finder->notContains('/Lorem ipsum/i')
  206. * $finder->notContains(['lorem', '/dolor/i'])
  207. *
  208. * @param string|string[] $patterns A pattern (string or regexp) or an array of patterns
  209. *
  210. * @return $this
  211. *
  212. * @see FilecontentFilterIterator
  213. */
  214. public function notContains(string|array $patterns): static
  215. {
  216. $this->notContains = array_merge($this->notContains, (array) $patterns);
  217. return $this;
  218. }
  219. /**
  220. * Adds rules that filenames must match.
  221. *
  222. * You can use patterns (delimited with / sign) or simple strings.
  223. *
  224. * $finder->path('some/special/dir')
  225. * $finder->path('/some\/special\/dir/') // same as above
  226. * $finder->path(['some dir', 'another/dir'])
  227. *
  228. * Use only / as dirname separator.
  229. *
  230. * @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
  231. *
  232. * @return $this
  233. *
  234. * @see FilenameFilterIterator
  235. */
  236. public function path(string|array $patterns): static
  237. {
  238. $this->paths = array_merge($this->paths, (array) $patterns);
  239. return $this;
  240. }
  241. /**
  242. * Adds rules that filenames must not match.
  243. *
  244. * You can use patterns (delimited with / sign) or simple strings.
  245. *
  246. * $finder->notPath('some/special/dir')
  247. * $finder->notPath('/some\/special\/dir/') // same as above
  248. * $finder->notPath(['some/file.txt', 'another/file.log'])
  249. *
  250. * Use only / as dirname separator.
  251. *
  252. * @param string|string[] $patterns A pattern (a regexp or a string) or an array of patterns
  253. *
  254. * @return $this
  255. *
  256. * @see FilenameFilterIterator
  257. */
  258. public function notPath(string|array $patterns): static
  259. {
  260. $this->notPaths = array_merge($this->notPaths, (array) $patterns);
  261. return $this;
  262. }
  263. /**
  264. * Adds tests for file sizes.
  265. *
  266. * $finder->size('> 10K');
  267. * $finder->size('<= 1Ki');
  268. * $finder->size(4);
  269. * $finder->size(['> 10K', '< 20K'])
  270. *
  271. * @param string|int|string[]|int[] $sizes A size range string or an integer or an array of size ranges
  272. *
  273. * @return $this
  274. *
  275. * @see SizeRangeFilterIterator
  276. * @see NumberComparator
  277. */
  278. public function size(string|int|array $sizes): static
  279. {
  280. foreach ((array) $sizes as $size) {
  281. $this->sizes[] = new NumberComparator($size);
  282. }
  283. return $this;
  284. }
  285. /**
  286. * Excludes directories.
  287. *
  288. * Directories passed as argument must be relative to the ones defined with the `in()` method. For example:
  289. *
  290. * $finder->in(__DIR__)->exclude('ruby');
  291. *
  292. * @param string|array $dirs A directory path or an array of directories
  293. *
  294. * @return $this
  295. *
  296. * @see ExcludeDirectoryFilterIterator
  297. */
  298. public function exclude(string|array $dirs): static
  299. {
  300. $this->exclude = array_merge($this->exclude, (array) $dirs);
  301. return $this;
  302. }
  303. /**
  304. * Excludes "hidden" directories and files (starting with a dot).
  305. *
  306. * This option is enabled by default.
  307. *
  308. * @return $this
  309. *
  310. * @see ExcludeDirectoryFilterIterator
  311. */
  312. public function ignoreDotFiles(bool $ignoreDotFiles): static
  313. {
  314. if ($ignoreDotFiles) {
  315. $this->ignore |= static::IGNORE_DOT_FILES;
  316. } else {
  317. $this->ignore &= ~static::IGNORE_DOT_FILES;
  318. }
  319. return $this;
  320. }
  321. /**
  322. * Forces the finder to ignore version control directories.
  323. *
  324. * This option is enabled by default.
  325. *
  326. * @return $this
  327. *
  328. * @see ExcludeDirectoryFilterIterator
  329. */
  330. public function ignoreVCS(bool $ignoreVCS): static
  331. {
  332. if ($ignoreVCS) {
  333. $this->ignore |= static::IGNORE_VCS_FILES;
  334. } else {
  335. $this->ignore &= ~static::IGNORE_VCS_FILES;
  336. }
  337. return $this;
  338. }
  339. /**
  340. * Forces Finder to obey .gitignore and ignore files based on rules listed there.
  341. *
  342. * This option is disabled by default.
  343. *
  344. * @return $this
  345. */
  346. public function ignoreVCSIgnored(bool $ignoreVCSIgnored): static
  347. {
  348. if ($ignoreVCSIgnored) {
  349. $this->ignore |= static::IGNORE_VCS_IGNORED_FILES;
  350. } else {
  351. $this->ignore &= ~static::IGNORE_VCS_IGNORED_FILES;
  352. }
  353. return $this;
  354. }
  355. /**
  356. * Adds VCS patterns.
  357. *
  358. * @see ignoreVCS()
  359. *
  360. * @param string|string[] $pattern VCS patterns to ignore
  361. */
  362. public static function addVCSPattern(string|array $pattern): void
  363. {
  364. foreach ((array) $pattern as $p) {
  365. self::$vcsPatterns[] = $p;
  366. }
  367. self::$vcsPatterns = array_unique(self::$vcsPatterns);
  368. }
  369. /**
  370. * Sorts files and directories by an anonymous function.
  371. *
  372. * The anonymous function receives two \SplFileInfo instances to compare.
  373. *
  374. * This can be slow as all the matching files and directories must be retrieved for comparison.
  375. *
  376. * @return $this
  377. *
  378. * @see SortableIterator
  379. */
  380. public function sort(\Closure $closure): static
  381. {
  382. $this->sort = $closure;
  383. return $this;
  384. }
  385. /**
  386. * Sorts files and directories by extension.
  387. *
  388. * This can be slow as all the matching files and directories must be retrieved for comparison.
  389. *
  390. * @return $this
  391. *
  392. * @see SortableIterator
  393. */
  394. public function sortByExtension(): static
  395. {
  396. $this->sort = SortableIterator::SORT_BY_EXTENSION;
  397. return $this;
  398. }
  399. /**
  400. * Sorts files and directories by name.
  401. *
  402. * This can be slow as all the matching files and directories must be retrieved for comparison.
  403. *
  404. * @return $this
  405. *
  406. * @see SortableIterator
  407. */
  408. public function sortByName(bool $useNaturalSort = false): static
  409. {
  410. $this->sort = $useNaturalSort ? SortableIterator::SORT_BY_NAME_NATURAL : SortableIterator::SORT_BY_NAME;
  411. return $this;
  412. }
  413. /**
  414. * Sorts files and directories by name case insensitive.
  415. *
  416. * This can be slow as all the matching files and directories must be retrieved for comparison.
  417. *
  418. * @return $this
  419. *
  420. * @see SortableIterator
  421. */
  422. public function sortByCaseInsensitiveName(bool $useNaturalSort = false): static
  423. {
  424. $this->sort = $useNaturalSort ? SortableIterator::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE : SortableIterator::SORT_BY_NAME_CASE_INSENSITIVE;
  425. return $this;
  426. }
  427. /**
  428. * Sorts files and directories by size.
  429. *
  430. * This can be slow as all the matching files and directories must be retrieved for comparison.
  431. *
  432. * @return $this
  433. *
  434. * @see SortableIterator
  435. */
  436. public function sortBySize(): static
  437. {
  438. $this->sort = SortableIterator::SORT_BY_SIZE;
  439. return $this;
  440. }
  441. /**
  442. * Sorts files and directories by type (directories before files), then by name.
  443. *
  444. * This can be slow as all the matching files and directories must be retrieved for comparison.
  445. *
  446. * @return $this
  447. *
  448. * @see SortableIterator
  449. */
  450. public function sortByType(): static
  451. {
  452. $this->sort = SortableIterator::SORT_BY_TYPE;
  453. return $this;
  454. }
  455. /**
  456. * Sorts files and directories by the last accessed time.
  457. *
  458. * This is the time that the file was last accessed, read or written to.
  459. *
  460. * This can be slow as all the matching files and directories must be retrieved for comparison.
  461. *
  462. * @return $this
  463. *
  464. * @see SortableIterator
  465. */
  466. public function sortByAccessedTime(): static
  467. {
  468. $this->sort = SortableIterator::SORT_BY_ACCESSED_TIME;
  469. return $this;
  470. }
  471. /**
  472. * Reverses the sorting.
  473. *
  474. * @return $this
  475. */
  476. public function reverseSorting(): static
  477. {
  478. $this->reverseSorting = true;
  479. return $this;
  480. }
  481. /**
  482. * Sorts files and directories by the last inode changed time.
  483. *
  484. * This is the time that the inode information was last modified (permissions, owner, group or other metadata).
  485. *
  486. * On Windows, since inode is not available, changed time is actually the file creation time.
  487. *
  488. * This can be slow as all the matching files and directories must be retrieved for comparison.
  489. *
  490. * @return $this
  491. *
  492. * @see SortableIterator
  493. */
  494. public function sortByChangedTime(): static
  495. {
  496. $this->sort = SortableIterator::SORT_BY_CHANGED_TIME;
  497. return $this;
  498. }
  499. /**
  500. * Sorts files and directories by the last modified time.
  501. *
  502. * This is the last time the actual contents of the file were last modified.
  503. *
  504. * This can be slow as all the matching files and directories must be retrieved for comparison.
  505. *
  506. * @return $this
  507. *
  508. * @see SortableIterator
  509. */
  510. public function sortByModifiedTime(): static
  511. {
  512. $this->sort = SortableIterator::SORT_BY_MODIFIED_TIME;
  513. return $this;
  514. }
  515. /**
  516. * Filters the iterator with an anonymous function.
  517. *
  518. * The anonymous function receives a \SplFileInfo and must return false
  519. * to remove files.
  520. *
  521. * @param \Closure(SplFileInfo): bool $closure
  522. * @param bool $prune Whether to skip traversing directories further
  523. *
  524. * @return $this
  525. *
  526. * @see CustomFilterIterator
  527. */
  528. public function filter(\Closure $closure, bool $prune = false): static
  529. {
  530. $this->filters[] = $closure;
  531. if ($prune) {
  532. $this->pruneFilters[] = $closure;
  533. }
  534. return $this;
  535. }
  536. /**
  537. * Forces the following of symlinks.
  538. *
  539. * @return $this
  540. */
  541. public function followLinks(): static
  542. {
  543. $this->followLinks = true;
  544. return $this;
  545. }
  546. /**
  547. * Tells finder to ignore unreadable directories.
  548. *
  549. * By default, scanning unreadable directories content throws an AccessDeniedException.
  550. *
  551. * @return $this
  552. */
  553. public function ignoreUnreadableDirs(bool $ignore = true): static
  554. {
  555. $this->ignoreUnreadableDirs = $ignore;
  556. return $this;
  557. }
  558. /**
  559. * Searches files and directories which match defined rules.
  560. *
  561. * @param string|string[] $dirs A directory path or an array of directories
  562. *
  563. * @return $this
  564. *
  565. * @throws DirectoryNotFoundException if one of the directories does not exist
  566. */
  567. public function in(string|array $dirs): static
  568. {
  569. $resolvedDirs = [];
  570. foreach ((array) $dirs as $dir) {
  571. if (is_dir($dir)) {
  572. $resolvedDirs[] = [$this->normalizeDir($dir)];
  573. } elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
  574. sort($glob);
  575. $resolvedDirs[] = array_map($this->normalizeDir(...), $glob);
  576. } else {
  577. throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir));
  578. }
  579. }
  580. $this->dirs = array_merge($this->dirs, ...$resolvedDirs);
  581. return $this;
  582. }
  583. /**
  584. * Returns an Iterator for the current Finder configuration.
  585. *
  586. * This method implements the IteratorAggregate interface.
  587. *
  588. * @return \Iterator<string, SplFileInfo>
  589. *
  590. * @throws \LogicException if the in() method has not been called
  591. */
  592. public function getIterator(): \Iterator
  593. {
  594. if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
  595. throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
  596. }
  597. if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
  598. $iterator = $this->searchInDirectory($this->dirs[0]);
  599. if ($this->sort || $this->reverseSorting) {
  600. $iterator = (new SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
  601. }
  602. return $iterator;
  603. }
  604. $iterator = new \AppendIterator();
  605. foreach ($this->dirs as $dir) {
  606. $iterator->append(new \IteratorIterator(new LazyIterator(fn () => $this->searchInDirectory($dir))));
  607. }
  608. foreach ($this->iterators as $it) {
  609. $iterator->append($it);
  610. }
  611. if ($this->sort || $this->reverseSorting) {
  612. $iterator = (new SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
  613. }
  614. return $iterator;
  615. }
  616. /**
  617. * Appends an existing set of files/directories to the finder.
  618. *
  619. * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
  620. *
  621. * @return $this
  622. *
  623. * @throws \InvalidArgumentException when the given argument is not iterable
  624. */
  625. public function append(iterable $iterator): static
  626. {
  627. if ($iterator instanceof \IteratorAggregate) {
  628. $this->iterators[] = $iterator->getIterator();
  629. } elseif ($iterator instanceof \Iterator) {
  630. $this->iterators[] = $iterator;
  631. } elseif (is_iterable($iterator)) {
  632. $it = new \ArrayIterator();
  633. foreach ($iterator as $file) {
  634. $file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
  635. $it[$file->getPathname()] = $file;
  636. }
  637. $this->iterators[] = $it;
  638. } else {
  639. throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
  640. }
  641. return $this;
  642. }
  643. /**
  644. * Check if any results were found.
  645. */
  646. public function hasResults(): bool
  647. {
  648. foreach ($this->getIterator() as $_) {
  649. return true;
  650. }
  651. return false;
  652. }
  653. /**
  654. * Counts all the results collected by the iterators.
  655. */
  656. public function count(): int
  657. {
  658. return iterator_count($this->getIterator());
  659. }
  660. private function searchInDirectory(string $dir): \Iterator
  661. {
  662. $exclude = $this->exclude;
  663. $notPaths = $this->notPaths;
  664. if ($this->pruneFilters) {
  665. $exclude = array_merge($exclude, $this->pruneFilters);
  666. }
  667. if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
  668. $exclude = array_merge($exclude, self::$vcsPatterns);
  669. }
  670. if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
  671. $notPaths[] = '#(^|/)\..+(/|$)#';
  672. }
  673. $minDepth = 0;
  674. $maxDepth = \PHP_INT_MAX;
  675. foreach ($this->depths as $comparator) {
  676. switch ($comparator->getOperator()) {
  677. case '>':
  678. $minDepth = $comparator->getTarget() + 1;
  679. break;
  680. case '>=':
  681. $minDepth = $comparator->getTarget();
  682. break;
  683. case '<':
  684. $maxDepth = $comparator->getTarget() - 1;
  685. break;
  686. case '<=':
  687. $maxDepth = $comparator->getTarget();
  688. break;
  689. default:
  690. $minDepth = $maxDepth = $comparator->getTarget();
  691. }
  692. }
  693. $flags = \RecursiveDirectoryIterator::SKIP_DOTS;
  694. if ($this->followLinks) {
  695. $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
  696. }
  697. $iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
  698. if ($exclude) {
  699. $iterator = new ExcludeDirectoryFilterIterator($iterator, $exclude);
  700. }
  701. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  702. if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
  703. $iterator = new DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
  704. }
  705. if ($this->mode) {
  706. $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
  707. }
  708. if ($this->names || $this->notNames) {
  709. $iterator = new FilenameFilterIterator($iterator, $this->names, $this->notNames);
  710. }
  711. if ($this->contains || $this->notContains) {
  712. $iterator = new FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
  713. }
  714. if ($this->sizes) {
  715. $iterator = new SizeRangeFilterIterator($iterator, $this->sizes);
  716. }
  717. if ($this->dates) {
  718. $iterator = new DateRangeFilterIterator($iterator, $this->dates);
  719. }
  720. if ($this->filters) {
  721. $iterator = new CustomFilterIterator($iterator, $this->filters);
  722. }
  723. if ($this->paths || $notPaths) {
  724. $iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
  725. }
  726. if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
  727. $iterator = new Iterator\VcsIgnoredFilterIterator($iterator, $dir);
  728. }
  729. return $iterator;
  730. }
  731. /**
  732. * Normalizes given directory names by removing trailing slashes.
  733. *
  734. * Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
  735. */
  736. private function normalizeDir(string $dir): string
  737. {
  738. if ('/' === $dir) {
  739. return $dir;
  740. }
  741. $dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
  742. if (preg_match('#^(ssh2\.)?s?ftp://#', $dir)) {
  743. $dir .= '/';
  744. }
  745. return $dir;
  746. }
  747. }