Finder.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859
  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 Comparator\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 Comparator\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 Comparator\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. * @return void
  363. */
  364. public static function addVCSPattern(string|array $pattern)
  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 = Iterator\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 ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL : Iterator\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 ? Iterator\SortableIterator::SORT_BY_NAME_NATURAL_CASE_INSENSITIVE : Iterator\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 = Iterator\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 = Iterator\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 = Iterator\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 = Iterator\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 = Iterator\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. $prune = 1 < \func_num_args() ? func_get_arg(1) : false;
  533. $this->filters[] = $closure;
  534. if ($prune) {
  535. $this->pruneFilters[] = $closure;
  536. }
  537. return $this;
  538. }
  539. /**
  540. * Forces the following of symlinks.
  541. *
  542. * @return $this
  543. */
  544. public function followLinks(): static
  545. {
  546. $this->followLinks = true;
  547. return $this;
  548. }
  549. /**
  550. * Tells finder to ignore unreadable directories.
  551. *
  552. * By default, scanning unreadable directories content throws an AccessDeniedException.
  553. *
  554. * @return $this
  555. */
  556. public function ignoreUnreadableDirs(bool $ignore = true): static
  557. {
  558. $this->ignoreUnreadableDirs = $ignore;
  559. return $this;
  560. }
  561. /**
  562. * Searches files and directories which match defined rules.
  563. *
  564. * @param string|string[] $dirs A directory path or an array of directories
  565. *
  566. * @return $this
  567. *
  568. * @throws DirectoryNotFoundException if one of the directories does not exist
  569. */
  570. public function in(string|array $dirs): static
  571. {
  572. $resolvedDirs = [];
  573. foreach ((array) $dirs as $dir) {
  574. if (is_dir($dir)) {
  575. $resolvedDirs[] = [$this->normalizeDir($dir)];
  576. } elseif ($glob = glob($dir, (\defined('GLOB_BRACE') ? \GLOB_BRACE : 0) | \GLOB_ONLYDIR | \GLOB_NOSORT)) {
  577. sort($glob);
  578. $resolvedDirs[] = array_map($this->normalizeDir(...), $glob);
  579. } else {
  580. throw new DirectoryNotFoundException(sprintf('The "%s" directory does not exist.', $dir));
  581. }
  582. }
  583. $this->dirs = array_merge($this->dirs, ...$resolvedDirs);
  584. return $this;
  585. }
  586. /**
  587. * Returns an Iterator for the current Finder configuration.
  588. *
  589. * This method implements the IteratorAggregate interface.
  590. *
  591. * @return \Iterator<string, SplFileInfo>
  592. *
  593. * @throws \LogicException if the in() method has not been called
  594. */
  595. public function getIterator(): \Iterator
  596. {
  597. if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
  598. throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
  599. }
  600. if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
  601. $iterator = $this->searchInDirectory($this->dirs[0]);
  602. if ($this->sort || $this->reverseSorting) {
  603. $iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
  604. }
  605. return $iterator;
  606. }
  607. $iterator = new \AppendIterator();
  608. foreach ($this->dirs as $dir) {
  609. $iterator->append(new \IteratorIterator(new LazyIterator(fn () => $this->searchInDirectory($dir))));
  610. }
  611. foreach ($this->iterators as $it) {
  612. $iterator->append($it);
  613. }
  614. if ($this->sort || $this->reverseSorting) {
  615. $iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
  616. }
  617. return $iterator;
  618. }
  619. /**
  620. * Appends an existing set of files/directories to the finder.
  621. *
  622. * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
  623. *
  624. * @return $this
  625. *
  626. * @throws \InvalidArgumentException when the given argument is not iterable
  627. */
  628. public function append(iterable $iterator): static
  629. {
  630. if ($iterator instanceof \IteratorAggregate) {
  631. $this->iterators[] = $iterator->getIterator();
  632. } elseif ($iterator instanceof \Iterator) {
  633. $this->iterators[] = $iterator;
  634. } elseif (is_iterable($iterator)) {
  635. $it = new \ArrayIterator();
  636. foreach ($iterator as $file) {
  637. $file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
  638. $it[$file->getPathname()] = $file;
  639. }
  640. $this->iterators[] = $it;
  641. } else {
  642. throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
  643. }
  644. return $this;
  645. }
  646. /**
  647. * Check if any results were found.
  648. */
  649. public function hasResults(): bool
  650. {
  651. foreach ($this->getIterator() as $_) {
  652. return true;
  653. }
  654. return false;
  655. }
  656. /**
  657. * Counts all the results collected by the iterators.
  658. */
  659. public function count(): int
  660. {
  661. return iterator_count($this->getIterator());
  662. }
  663. private function searchInDirectory(string $dir): \Iterator
  664. {
  665. $exclude = $this->exclude;
  666. $notPaths = $this->notPaths;
  667. if ($this->pruneFilters) {
  668. $exclude = array_merge($exclude, $this->pruneFilters);
  669. }
  670. if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
  671. $exclude = array_merge($exclude, self::$vcsPatterns);
  672. }
  673. if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
  674. $notPaths[] = '#(^|/)\..+(/|$)#';
  675. }
  676. $minDepth = 0;
  677. $maxDepth = \PHP_INT_MAX;
  678. foreach ($this->depths as $comparator) {
  679. switch ($comparator->getOperator()) {
  680. case '>':
  681. $minDepth = $comparator->getTarget() + 1;
  682. break;
  683. case '>=':
  684. $minDepth = $comparator->getTarget();
  685. break;
  686. case '<':
  687. $maxDepth = $comparator->getTarget() - 1;
  688. break;
  689. case '<=':
  690. $maxDepth = $comparator->getTarget();
  691. break;
  692. default:
  693. $minDepth = $maxDepth = $comparator->getTarget();
  694. }
  695. }
  696. $flags = \RecursiveDirectoryIterator::SKIP_DOTS;
  697. if ($this->followLinks) {
  698. $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
  699. }
  700. $iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
  701. if ($exclude) {
  702. $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $exclude);
  703. }
  704. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  705. if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
  706. $iterator = new Iterator\DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
  707. }
  708. if ($this->mode) {
  709. $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
  710. }
  711. if ($this->names || $this->notNames) {
  712. $iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
  713. }
  714. if ($this->contains || $this->notContains) {
  715. $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
  716. }
  717. if ($this->sizes) {
  718. $iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
  719. }
  720. if ($this->dates) {
  721. $iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
  722. }
  723. if ($this->filters) {
  724. $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
  725. }
  726. if ($this->paths || $notPaths) {
  727. $iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
  728. }
  729. if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
  730. $iterator = new Iterator\VcsIgnoredFilterIterator($iterator, $dir);
  731. }
  732. return $iterator;
  733. }
  734. /**
  735. * Normalizes given directory names by removing trailing slashes.
  736. *
  737. * Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
  738. */
  739. private function normalizeDir(string $dir): string
  740. {
  741. if ('/' === $dir) {
  742. return $dir;
  743. }
  744. $dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
  745. if (preg_match('#^(ssh2\.)?s?ftp://#', $dir)) {
  746. $dir .= '/';
  747. }
  748. return $dir;
  749. }
  750. }