酒店预订平台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

822 lines
23 KiB

  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. public function getIterator()
  556. {
  557. if (0 === \count($this->dirs) && 0 === \count($this->iterators)) {
  558. throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
  559. }
  560. if (1 === \count($this->dirs) && 0 === \count($this->iterators)) {
  561. $iterator = $this->searchInDirectory($this->dirs[0]);
  562. if ($this->sort || $this->reverseSorting) {
  563. $iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
  564. }
  565. return $iterator;
  566. }
  567. $iterator = new \AppendIterator();
  568. foreach ($this->dirs as $dir) {
  569. $iterator->append(new \IteratorIterator(new LazyIterator(function () use ($dir) {
  570. return $this->searchInDirectory($dir);
  571. })));
  572. }
  573. foreach ($this->iterators as $it) {
  574. $iterator->append($it);
  575. }
  576. if ($this->sort || $this->reverseSorting) {
  577. $iterator = (new Iterator\SortableIterator($iterator, $this->sort, $this->reverseSorting))->getIterator();
  578. }
  579. return $iterator;
  580. }
  581. /**
  582. * Appends an existing set of files/directories to the finder.
  583. *
  584. * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
  585. *
  586. * @param iterable $iterator
  587. *
  588. * @return $this
  589. *
  590. * @throws \InvalidArgumentException when the given argument is not iterable
  591. */
  592. public function append($iterator)
  593. {
  594. if ($iterator instanceof \IteratorAggregate) {
  595. $this->iterators[] = $iterator->getIterator();
  596. } elseif ($iterator instanceof \Iterator) {
  597. $this->iterators[] = $iterator;
  598. } elseif ($iterator instanceof \Traversable || \is_array($iterator)) {
  599. $it = new \ArrayIterator();
  600. foreach ($iterator as $file) {
  601. $file = $file instanceof \SplFileInfo ? $file : new \SplFileInfo($file);
  602. $it[$file->getPathname()] = $file;
  603. }
  604. $this->iterators[] = $it;
  605. } else {
  606. throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
  607. }
  608. return $this;
  609. }
  610. /**
  611. * Check if any results were found.
  612. *
  613. * @return bool
  614. */
  615. public function hasResults()
  616. {
  617. foreach ($this->getIterator() as $_) {
  618. return true;
  619. }
  620. return false;
  621. }
  622. /**
  623. * Counts all the results collected by the iterators.
  624. *
  625. * @return int
  626. */
  627. public function count()
  628. {
  629. return iterator_count($this->getIterator());
  630. }
  631. private function searchInDirectory(string $dir): \Iterator
  632. {
  633. $exclude = $this->exclude;
  634. $notPaths = $this->notPaths;
  635. if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
  636. $exclude = array_merge($exclude, self::$vcsPatterns);
  637. }
  638. if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
  639. $notPaths[] = '#(^|/)\..+(/|$)#';
  640. }
  641. if (static::IGNORE_VCS_IGNORED_FILES === (static::IGNORE_VCS_IGNORED_FILES & $this->ignore)) {
  642. $gitignoreFilePath = sprintf('%s/.gitignore', $dir);
  643. if (!is_readable($gitignoreFilePath)) {
  644. throw new \RuntimeException(sprintf('The "ignoreVCSIgnored" option cannot be used by the Finder as the "%s" file is not readable.', $gitignoreFilePath));
  645. }
  646. $notPaths = array_merge($notPaths, [Gitignore::toRegex(file_get_contents($gitignoreFilePath))]);
  647. }
  648. $minDepth = 0;
  649. $maxDepth = \PHP_INT_MAX;
  650. foreach ($this->depths as $comparator) {
  651. switch ($comparator->getOperator()) {
  652. case '>':
  653. $minDepth = $comparator->getTarget() + 1;
  654. break;
  655. case '>=':
  656. $minDepth = $comparator->getTarget();
  657. break;
  658. case '<':
  659. $maxDepth = $comparator->getTarget() - 1;
  660. break;
  661. case '<=':
  662. $maxDepth = $comparator->getTarget();
  663. break;
  664. default:
  665. $minDepth = $maxDepth = $comparator->getTarget();
  666. }
  667. }
  668. $flags = \RecursiveDirectoryIterator::SKIP_DOTS;
  669. if ($this->followLinks) {
  670. $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
  671. }
  672. $iterator = new Iterator\RecursiveDirectoryIterator($dir, $flags, $this->ignoreUnreadableDirs);
  673. if ($exclude) {
  674. $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $exclude);
  675. }
  676. $iterator = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
  677. if ($minDepth > 0 || $maxDepth < \PHP_INT_MAX) {
  678. $iterator = new Iterator\DepthRangeFilterIterator($iterator, $minDepth, $maxDepth);
  679. }
  680. if ($this->mode) {
  681. $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
  682. }
  683. if ($this->names || $this->notNames) {
  684. $iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
  685. }
  686. if ($this->contains || $this->notContains) {
  687. $iterator = new Iterator\FilecontentFilterIterator($iterator, $this->contains, $this->notContains);
  688. }
  689. if ($this->sizes) {
  690. $iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
  691. }
  692. if ($this->dates) {
  693. $iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
  694. }
  695. if ($this->filters) {
  696. $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
  697. }
  698. if ($this->paths || $notPaths) {
  699. $iterator = new Iterator\PathFilterIterator($iterator, $this->paths, $notPaths);
  700. }
  701. return $iterator;
  702. }
  703. /**
  704. * Normalizes given directory names by removing trailing slashes.
  705. *
  706. * Excluding: (s)ftp:// or ssh2.(s)ftp:// wrapper
  707. */
  708. private function normalizeDir(string $dir): string
  709. {
  710. if ('/' === $dir) {
  711. return $dir;
  712. }
  713. $dir = rtrim($dir, '/'.\DIRECTORY_SEPARATOR);
  714. if (preg_match('#^(ssh2\.)?s?ftp://#', $dir)) {
  715. $dir .= '/';
  716. }
  717. return $dir;
  718. }
  719. }