PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/symfony/2.0.0pr4/src/vendor/symfony/src/Symfony/Component/Finder/Finder.php

http://github.com/pmjones/php-framework-benchmarks
PHP | 387 lines | 150 code | 52 blank | 185 comment | 15 complexity | d7818773201be2c1aa3d324ed877d603 MD5 | raw file
Possible License(s): LGPL-3.0, Apache-2.0, BSD-3-Clause, ISC, AGPL-3.0, LGPL-2.1
  1. <?php
  2. namespace Symfony\Component\Finder;
  3. /*
  4. * This file is part of the Symfony framework.
  5. *
  6. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  7. *
  8. * This source file is subject to the MIT license that is bundled
  9. * with this source code in the file LICENSE.
  10. */
  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 = new Finder();
  21. * $finder = $finder->files()->name('*.php')->in(__DIR__);
  22. *
  23. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24. */
  25. class Finder implements \IteratorAggregate
  26. {
  27. protected $mode = 0;
  28. protected $names = array();
  29. protected $notNames = array();
  30. protected $exclude = array();
  31. protected $filters = array();
  32. protected $depths = array();
  33. protected $sizes = array();
  34. protected $followLinks = false;
  35. protected $sort = false;
  36. protected $ignoreVCS = true;
  37. protected $dirs = array();
  38. protected $dates = array();
  39. /**
  40. * Restricts the matching to directories only.
  41. *
  42. * @return Finder The current Finder instance
  43. */
  44. public function directories()
  45. {
  46. $this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
  47. return $this;
  48. }
  49. /**
  50. * Restricts the matching to files only.
  51. *
  52. * @return Finder The current Finder instance
  53. */
  54. public function files()
  55. {
  56. $this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
  57. return $this;
  58. }
  59. /**
  60. * Adds tests for the directory depth.
  61. *
  62. * Usage:
  63. *
  64. * $finder->depth('> 1') // the Finder will start matching at level 1.
  65. * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
  66. *
  67. * @param int $level The depth level expression
  68. *
  69. * @return Finder The current Finder instance
  70. *
  71. * @see Symfony\Component\Finder\Iterator\DepthRangeFilterIterator
  72. * @see Symfony\Component\Finder\Comparator\NumberComparator
  73. */
  74. public function depth($level)
  75. {
  76. $this->depths[] = new Comparator\NumberComparator($level);
  77. return $this;
  78. }
  79. /**
  80. * Adds tests for file dates (last modified).
  81. *
  82. * The date must be something that strtotime() is able to parse:
  83. *
  84. * $finder->date('since yesterday');
  85. * $finder->date('until 2 days ago');
  86. * $finder->date('> now - 2 hours');
  87. * $finder->date('>= 2005-10-15');
  88. *
  89. * @param string $date A date rage string
  90. *
  91. * @return Finder The current Finder instance
  92. *
  93. * @see strtotime
  94. * @see Symfony\Component\Finder\Iterator\DateRangeFilterIterator
  95. * @see Symfony\Component\Finder\Comparator\DateComparator
  96. */
  97. public function date($date)
  98. {
  99. $this->dates[] = new Comparator\DateComparator($date);
  100. return $this;
  101. }
  102. /**
  103. * Adds rules that files must match.
  104. *
  105. * You can use patterns (delimited with / sign), globs or simple strings.
  106. *
  107. * $finder->name('*.php')
  108. * $finder->name('/\.php$/') // same as above
  109. * $finder->name('test.php')
  110. *
  111. * @param string $pattern A pattern (a regexp, a glob, or a string)
  112. *
  113. * @return Finder The current Finder instance
  114. *
  115. * @see Symfony\Component\Finder\Iterator\FilenameFilterIterator
  116. */
  117. public function name($pattern)
  118. {
  119. $this->names[] = $pattern;
  120. return $this;
  121. }
  122. /**
  123. * Adds rules that files must not match.
  124. *
  125. * @param string $pattern A pattern (a regexp, a glob, or a string)
  126. *
  127. * @return Finder The current Finder instance
  128. *
  129. * @see Symfony\Component\Finder\Iterator\FilenameFilterIterator
  130. */
  131. public function notName($pattern)
  132. {
  133. $this->notNames[] = $pattern;
  134. return $this;
  135. }
  136. /**
  137. * Adds tests for file sizes.
  138. *
  139. * $finder->size('> 10K');
  140. * $finder->size('<= 1Ki');
  141. * $finder->size(4);
  142. *
  143. * @param string $size A size range string
  144. *
  145. * @return Finder The current Finder instance
  146. *
  147. * @see Symfony\Component\Finder\Iterator\SizeRangeFilterIterator
  148. * @see Symfony\Component\Finder\Comparator\NumberComparator
  149. */
  150. public function size($size)
  151. {
  152. $this->sizes[] = new Comparator\NumberComparator($size);
  153. return $this;
  154. }
  155. /**
  156. * Excludes directories.
  157. *
  158. * @param string $dir A directory to exclude
  159. *
  160. * @return Finder The current Finder instance
  161. *
  162. * @see Symfony\Component\Finder\Iterator\ExcludeDirectoryFilterIterator
  163. */
  164. public function exclude($dir)
  165. {
  166. $this->exclude[] = $dir;
  167. return $this;
  168. }
  169. /**
  170. * Forces the finder to ignore version control directories.
  171. *
  172. * @return Finder The current Finder instance
  173. *
  174. * @see Symfony\Component\Finder\Iterator\IgnoreVcsFilterIterator
  175. */
  176. public function ignoreVCS($ignoreVCS)
  177. {
  178. $this->ignoreVCS = (Boolean) $ignoreVCS;
  179. return $this;
  180. }
  181. /**
  182. * Sorts files and directories by an anonymous function.
  183. *
  184. * The anonymous function receives two \SplFileInfo instances to compare.
  185. *
  186. * This can be slow as all the matching files and directories must be retrieved for comparison.
  187. *
  188. * @param Closure $closure An anonymous function
  189. *
  190. * @return Finder The current Finder instance
  191. *
  192. * @see Symfony\Component\Finder\Iterator\SortableIterator
  193. */
  194. public function sort(\Closure $closure)
  195. {
  196. $this->sort = $closure;
  197. return $this;
  198. }
  199. /**
  200. * Sorts files and directories by name.
  201. *
  202. * This can be slow as all the matching files and directories must be retrieved for comparison.
  203. *
  204. * @return Finder The current Finder instance
  205. *
  206. * @see Symfony\Component\Finder\Iterator\SortableIterator
  207. */
  208. public function sortByName()
  209. {
  210. $this->sort = Iterator\SortableIterator::SORT_BY_NAME;
  211. return $this;
  212. }
  213. /**
  214. * Sorts files and directories by type (directories before files), then by name.
  215. *
  216. * This can be slow as all the matching files and directories must be retrieved for comparison.
  217. *
  218. * @return Finder The current Finder instance
  219. *
  220. * @see Symfony\Component\Finder\Iterator\SortableIterator
  221. */
  222. public function sortByType()
  223. {
  224. $this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
  225. return $this;
  226. }
  227. /**
  228. * Filters the iterator with an anonymous function.
  229. *
  230. * The anonymous function receives a \SplFileInfo and must return false
  231. * to remove files.
  232. *
  233. * @param Closure $closure An anonymous function
  234. *
  235. * @return Finder The current Finder instance
  236. *
  237. * @see Symfony\Component\Finder\Iterator\CustomFilterIterator
  238. */
  239. public function filter(\Closure $closure)
  240. {
  241. $this->filters[] = $closure;
  242. return $this;
  243. }
  244. /**
  245. * Forces the following of symlinks.
  246. *
  247. * @return Finder The current Finder instance
  248. */
  249. public function followLinks()
  250. {
  251. $this->followLinks = true;
  252. return $this;
  253. }
  254. /**
  255. * Searches files and directories which match defined rules.
  256. *
  257. * @param string|array $dirs A directory path or an array of directories
  258. *
  259. * @return Finder The current Finder instance
  260. *
  261. * @throws \InvalidArgumentException if one of the directory does not exist
  262. */
  263. public function in($dirs)
  264. {
  265. if (!is_array($dirs)) {
  266. $dirs = array($dirs);
  267. }
  268. foreach ($dirs as $dir) {
  269. if (!is_dir($dir)) {
  270. throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
  271. }
  272. }
  273. $this->dirs = array_merge($this->dirs, $dirs);
  274. return $this;
  275. }
  276. /**
  277. * Returns an Iterator for the current Finder configuration.
  278. *
  279. * This method implements the IteratorAggregate interface.
  280. *
  281. * @return \Iterator An iterator
  282. *
  283. * @throws \LogicException if the in() method has not been called
  284. */
  285. public function getIterator()
  286. {
  287. if (0 === count($this->dirs)) {
  288. throw new \LogicException('You must call the in() method before iterating over a Finder.');
  289. }
  290. if (1 === count($this->dirs)) {
  291. return $this->searchInDirectory($this->dirs[0]);
  292. }
  293. $iterator = new \AppendIterator();
  294. foreach ($this->dirs as $dir) {
  295. $iterator->append($this->searchInDirectory($dir));
  296. }
  297. return $iterator;
  298. }
  299. protected function searchInDirectory($dir)
  300. {
  301. $flags = \FilesystemIterator::SKIP_DOTS;
  302. if ($this->followLinks) {
  303. $flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
  304. }
  305. $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
  306. if ($this->depths) {
  307. $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);
  308. }
  309. if ($this->mode) {
  310. $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
  311. }
  312. if ($this->exclude) {
  313. $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
  314. }
  315. if ($this->ignoreVCS) {
  316. $iterator = new Iterator\IgnoreVcsFilterIterator($iterator);
  317. }
  318. if ($this->names || $this->notNames) {
  319. $iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
  320. }
  321. if ($this->sizes) {
  322. $iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
  323. }
  324. if ($this->dates) {
  325. $iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
  326. }
  327. if ($this->filters) {
  328. $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
  329. }
  330. if ($this->sort) {
  331. $iterator = new Iterator\SortableIterator($iterator, $this->sort);
  332. }
  333. return $iterator;
  334. }
  335. }