Finder.php 22 KB

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