Finder.php 24 KB

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