Finder.php 23 KB

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