PageRenderTime 59ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Component/Finder/Finder.php

https://bitbucket.org/Leimz/leimzwebsite
PHP | 510 lines | 202 code | 62 blank | 246 comment | 23 complexity | 4bc2eb85769bfc059dbbff5282c80f7e MD5 | raw file
Possible License(s): Apache-2.0, LGPL-2.1, LGPL-3.0, BSD-2-Clause, BSD-3-Clause
  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. /**
  12. * Finder allows to build rules to find files and directories.
  13. *
  14. * It is a thin wrapper around several specialized iterator classes.
  15. *
  16. * All rules may be invoked several times.
  17. *
  18. * All methods return the current Finder object to allow easy chaining:
  19. *
  20. * $finder = Finder::create()->files()->name('*.php')->in(__DIR__);
  21. *
  22. * @author Fabien Potencier <fabien@symfony.com>
  23. *
  24. * @api
  25. */
  26. class Finder implements \IteratorAggregate
  27. {
  28. const IGNORE_VCS_FILES = 1;
  29. const IGNORE_DOT_FILES = 2;
  30. private $mode = 0;
  31. private $names = array();
  32. private $notNames = array();
  33. private $exclude = array();
  34. private $filters = array();
  35. private $depths = array();
  36. private $sizes = array();
  37. private $followLinks = false;
  38. private $sort = false;
  39. private $ignore = 0;
  40. private $dirs = array();
  41. private $dates = array();
  42. private $iterators = array();
  43. static private $vcsPatterns = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg');
  44. /**
  45. * Constructor.
  46. */
  47. public function __construct()
  48. {
  49. $this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
  50. }
  51. /**
  52. * Creates a new Finder.
  53. *
  54. * @return Finder A new Finder instance
  55. *
  56. * @api
  57. */
  58. static public function create()
  59. {
  60. return new self();
  61. }
  62. /**
  63. * Restricts the matching to directories only.
  64. *
  65. * @return Finder The current Finder instance
  66. *
  67. * @api
  68. */
  69. public function directories()
  70. {
  71. $this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
  72. return $this;
  73. }
  74. /**
  75. * Restricts the matching to files only.
  76. *
  77. * @return Finder The current Finder instance
  78. *
  79. * @api
  80. */
  81. public function files()
  82. {
  83. $this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
  84. return $this;
  85. }
  86. /**
  87. * Adds tests for the directory depth.
  88. *
  89. * Usage:
  90. *
  91. * $finder->depth('> 1') // the Finder will start matching at level 1.
  92. * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
  93. *
  94. * @param int $level The depth level expression
  95. *
  96. * @return Finder The current Finder instance
  97. *
  98. * @see Symfony\Component\Finder\Iterator\DepthRangeFilterIterator
  99. * @see Symfony\Component\Finder\Comparator\NumberComparator
  100. *
  101. * @api
  102. */
  103. public function depth($level)
  104. {
  105. $this->depths[] = new Comparator\NumberComparator($level);
  106. return $this;
  107. }
  108. /**
  109. * Adds tests for file dates (last modified).
  110. *
  111. * The date must be something that strtotime() is able to parse:
  112. *
  113. * $finder->date('since yesterday');
  114. * $finder->date('until 2 days ago');
  115. * $finder->date('> now - 2 hours');
  116. * $finder->date('>= 2005-10-15');
  117. *
  118. * @param string $date A date rage string
  119. *
  120. * @return Finder The current Finder instance
  121. *
  122. * @see strtotime
  123. * @see Symfony\Component\Finder\Iterator\DateRangeFilterIterator
  124. * @see Symfony\Component\Finder\Comparator\DateComparator
  125. *
  126. * @api
  127. */
  128. public function date($date)
  129. {
  130. $this->dates[] = new Comparator\DateComparator($date);
  131. return $this;
  132. }
  133. /**
  134. * Adds rules that files must match.
  135. *
  136. * You can use patterns (delimited with / sign), globs or simple strings.
  137. *
  138. * $finder->name('*.php')
  139. * $finder->name('/\.php$/') // same as above
  140. * $finder->name('test.php')
  141. *
  142. * @param string $pattern A pattern (a regexp, a glob, or a string)
  143. *
  144. * @return Finder The current Finder instance
  145. *
  146. * @see Symfony\Component\Finder\Iterator\FilenameFilterIterator
  147. *
  148. * @api
  149. */
  150. public function name($pattern)
  151. {
  152. $this->names[] = $pattern;
  153. return $this;
  154. }
  155. /**
  156. * Adds rules that files must not match.
  157. *
  158. * @param string $pattern A pattern (a regexp, a glob, or a string)
  159. *
  160. * @return Finder The current Finder instance
  161. *
  162. * @see Symfony\Component\Finder\Iterator\FilenameFilterIterator
  163. *
  164. * @api
  165. */
  166. public function notName($pattern)
  167. {
  168. $this->notNames[] = $pattern;
  169. return $this;
  170. }
  171. /**
  172. * Adds tests for file sizes.
  173. *
  174. * $finder->size('> 10K');
  175. * $finder->size('<= 1Ki');
  176. * $finder->size(4);
  177. *
  178. * @param string $size A size range string
  179. *
  180. * @return Finder The current Finder instance
  181. *
  182. * @see Symfony\Component\Finder\Iterator\SizeRangeFilterIterator
  183. * @see Symfony\Component\Finder\Comparator\NumberComparator
  184. *
  185. * @api
  186. */
  187. public function size($size)
  188. {
  189. $this->sizes[] = new Comparator\NumberComparator($size);
  190. return $this;
  191. }
  192. /**
  193. * Excludes directories.
  194. *
  195. * @param string $dir A directory to exclude
  196. *
  197. * @return Finder The current Finder instance
  198. *
  199. * @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
  200. *
  201. * @api
  202. */
  203. public function exclude($dir)
  204. {
  205. $this->exclude[] = $dir;
  206. return $this;
  207. }
  208. /**
  209. * Excludes "hidden" directories and files (starting with a dot).
  210. *
  211. * @param Boolean $ignoreDotFiles Whether to exclude "hidden" files or not
  212. *
  213. * @return Finder The current Finder instance
  214. *
  215. * @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
  216. *
  217. * @api
  218. */
  219. public function ignoreDotFiles($ignoreDotFiles)
  220. {
  221. if ($ignoreDotFiles) {
  222. $this->ignore = $this->ignore | static::IGNORE_DOT_FILES;
  223. } else {
  224. $this->ignore = $this->ignore ^ static::IGNORE_DOT_FILES;
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Forces the finder to ignore version control directories.
  230. *
  231. * @param Boolean $ignoreVCS Whether to exclude VCS files or not
  232. *
  233. * @return Finder The current Finder instance
  234. *
  235. * @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
  236. *
  237. * @api
  238. */
  239. public function ignoreVCS($ignoreVCS)
  240. {
  241. if ($ignoreVCS) {
  242. $this->ignore = $this->ignore | static::IGNORE_VCS_FILES;
  243. } else {
  244. $this->ignore = $this->ignore ^ static::IGNORE_VCS_FILES;
  245. }
  246. return $this;
  247. }
  248. static public function addVCSPattern($pattern)
  249. {
  250. static::$vcsPatterns[] = $pattern;
  251. }
  252. /**
  253. * Sorts files and directories by an anonymous function.
  254. *
  255. * The anonymous function receives two \SplFileInfo instances to compare.
  256. *
  257. * This can be slow as all the matching files and directories must be retrieved for comparison.
  258. *
  259. * @param Closure $closure An anonymous function
  260. *
  261. * @return Finder The current Finder instance
  262. *
  263. * @see Symfony\Component\Finder\Iterator\SortableIterator
  264. *
  265. * @api
  266. */
  267. public function sort(\Closure $closure)
  268. {
  269. $this->sort = $closure;
  270. return $this;
  271. }
  272. /**
  273. * Sorts files and directories by name.
  274. *
  275. * This can be slow as all the matching files and directories must be retrieved for comparison.
  276. *
  277. * @return Finder The current Finder instance
  278. *
  279. * @see Symfony\Component\Finder\Iterator\SortableIterator
  280. *
  281. * @api
  282. */
  283. public function sortByName()
  284. {
  285. $this->sort = Iterator\SortableIterator::SORT_BY_NAME;
  286. return $this;
  287. }
  288. /**
  289. * Sorts files and directories by type (directories before files), then by name.
  290. *
  291. * This can be slow as all the matching files and directories must be retrieved for comparison.
  292. *
  293. * @return Finder The current Finder instance
  294. *
  295. * @see Symfony\Component\Finder\Iterator\SortableIterator
  296. *
  297. * @api
  298. */
  299. public function sortByType()
  300. {
  301. $this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
  302. return $this;
  303. }
  304. /**
  305. * Filters the iterator with an anonymous function.
  306. *
  307. * The anonymous function receives a \SplFileInfo and must return false
  308. * to remove files.
  309. *
  310. * @param Closure $closure An anonymous function
  311. *
  312. * @return Finder The current Finder instance
  313. *
  314. * @see Symfony\Component\Finder\Iterator\CustomFilterIterator
  315. *
  316. * @api
  317. */
  318. public function filter(\Closure $closure)
  319. {
  320. $this->filters[] = $closure;
  321. return $this;
  322. }
  323. /**
  324. * Forces the following of symlinks.
  325. *
  326. * @return Finder The current Finder instance
  327. *
  328. * @api
  329. */
  330. public function followLinks()
  331. {
  332. $this->followLinks = true;
  333. return $this;
  334. }
  335. /**
  336. * Searches files and directories which match defined rules.
  337. *
  338. * @param string|array $dirs A directory path or an array of directories
  339. *
  340. * @return Finder The current Finder instance
  341. *
  342. * @throws \InvalidArgumentException if one of the directory does not exist
  343. *
  344. * @api
  345. */
  346. public function in($dirs)
  347. {
  348. $dirs = (array) $dirs;
  349. foreach ($dirs as $dir) {
  350. if (!is_dir($dir)) {
  351. throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
  352. }
  353. }
  354. $this->dirs = array_merge($this->dirs, $dirs);
  355. return $this;
  356. }
  357. /**
  358. * Returns an Iterator for the current Finder configuration.
  359. *
  360. * This method implements the IteratorAggregate interface.
  361. *
  362. * @return \Iterator An iterator
  363. *
  364. * @throws \LogicException if the in() method has not been called
  365. */
  366. public function getIterator()
  367. {
  368. if (0 === count($this->dirs)) {
  369. throw new \LogicException('You must call the in() method before iterating over a Finder.');
  370. }
  371. if (1 === count($this->dirs) && 0 === count($this->iterators)) {
  372. return $this->searchInDirectory($this->dirs[0]);
  373. }
  374. $iterator = new \AppendIterator();
  375. foreach ($this->dirs as $dir) {
  376. $iterator->append($this->searchInDirectory($dir));
  377. }
  378. foreach ($this->iterators as $it) {
  379. $iterator->append($it);
  380. }
  381. return $iterator;
  382. }
  383. /**
  384. * Appends an existing set of files/directories to the finder.
  385. *
  386. * The set can be another Finder, an Iterator, an IteratorAggregate, or even a plain array.
  387. *
  388. * @param mixed $iterator
  389. */
  390. public function append($iterator)
  391. {
  392. if ($iterator instanceof \IteratorAggregate) {
  393. $this->iterators[] = $iterator->getIterator();
  394. } elseif ($iterator instanceof \Iterator) {
  395. $this->iterators[] = $iterator;
  396. } elseif ($iterator instanceof \Traversable || is_array($iterator)) {
  397. $it = new \ArrayIterator();
  398. foreach ($iterator as $file) {
  399. $it->append($file instanceof \SplFileInfo ? $file : new \SplFileInfo($file));
  400. }
  401. $this->iterators[] = $it;
  402. } else {
  403. throw new \InvalidArgumentException('Finder::append() method wrong argument type.');
  404. }
  405. }
  406. private function searchInDirectory($dir)
  407. {
  408. $flags = \RecursiveDirectoryIterator::SKIP_DOTS;
  409. if ($this->followLinks) {
  410. $flags |= \RecursiveDirectoryIterator::FOLLOW_SYMLINKS;
  411. }
  412. $iterator = new \RecursiveIteratorIterator(
  413. new Iterator\RecursiveDirectoryIterator($dir, $flags),
  414. \RecursiveIteratorIterator::SELF_FIRST
  415. );
  416. if ($this->depths) {
  417. $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);
  418. }
  419. if ($this->mode) {
  420. $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
  421. }
  422. if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
  423. $this->exclude = array_merge($this->exclude, static::$vcsPatterns);
  424. }
  425. if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
  426. $this->exclude[] = '\..+';
  427. }
  428. if ($this->exclude) {
  429. $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
  430. }
  431. if ($this->names || $this->notNames) {
  432. $iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
  433. }
  434. if ($this->sizes) {
  435. $iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
  436. }
  437. if ($this->dates) {
  438. $iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
  439. }
  440. if ($this->filters) {
  441. $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
  442. }
  443. if ($this->sort) {
  444. $iterator = new Iterator\SortableIterator($iterator, $this->sort);
  445. }
  446. return $iterator;
  447. }
  448. }