Drupal investigation

Finder.php 24KB

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