Finder.php 24 KB

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