Finder.php 24 KB

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