Brak opisu

Finder.php 23KB

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