123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389 |
- <?php
- namespace Symfony\Components\Finder;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * Finder allows to build rules to find files and directories.
- *
- * It is a thin wrapper around several specialized iterator classes.
- *
- * All rules may be invoked several times.
- *
- * All methods return the current Finder object to allow easy chaining:
- *
- * $finder = new Finder();
- * $finder = $finder->files()->name('*.php')->in(__DIR__);
- *
- * @package Symfony
- * @subpackage Components_Finder
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class Finder implements \IteratorAggregate
- {
- protected $mode = 0;
- protected $names = array();
- protected $notNames = array();
- protected $exclude = array();
- protected $filters = array();
- protected $depths = array();
- protected $sizes = array();
- protected $followLinks = false;
- protected $sort = false;
- protected $ignoreVCS = true;
- protected $dirs = array();
- protected $dates = array();
- /**
- * Restricts the matching to directories only.
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- */
- public function directories()
- {
- $this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
- return $this;
- }
- /**
- * Restricts the matching to files only.
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- */
- public function files()
- {
- $this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
- return $this;
- }
- /**
- * Adds tests for the directory depth.
- *
- * Usage:
- *
- * $finder->depth('> 1') // the Finder will start matching at level 1.
- * $finder->depth('< 3') // the Finder will descend at most 3 levels of directories below the starting point.
- *
- * @param int $level The depth level expression
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\DepthRangeFilterIterator
- * @see Symfony\Components\Finder\Comparator\NumberComparator
- */
- public function depth($level)
- {
- $this->depths[] = new Comparator\NumberComparator($level);
- return $this;
- }
- /**
- * Adds tests for file dates (last modified).
- *
- * The date must be something that strtotime() is able to parse:
- *
- * $finder->date('since yesterday');
- * $finder->date('until 2 days ago');
- * $finder->date('> now - 2 hours');
- * $finder->date('>= 2005-10-15');
- *
- * @param string $date A date rage string
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see strtotime
- * @see Symfony\Components\Finder\Iterator\DateRangeFilterIterator
- * @see Symfony\Components\Finder\Comparator\DateComparator
- */
- public function date($date)
- {
- $this->dates[] = new Comparator\DateComparator($date);
- return $this;
- }
- /**
- * Adds rules that files must match.
- *
- * You can use patterns (delimited with / sign), globs or simple strings.
- *
- * $finder->name('*.php')
- * $finder->name('/\.php$/') // same as above
- * $finder->name('test.php')
- *
- * @param string $pattern A pattern (a regexp, a glob, or a string)
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\FilenameFilterIterator
- */
- public function name($pattern)
- {
- $this->names[] = $pattern;
- return $this;
- }
- /**
- * Adds rules that files must not match.
- *
- * @param string $pattern A pattern (a regexp, a glob, or a string)
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\FilenameFilterIterator
- */
- public function notName($pattern)
- {
- $this->notNames[] = $pattern;
- return $this;
- }
- /**
- * Adds tests for file sizes.
- *
- * $finder->size('> 10K');
- * $finder->size('<= 1Ki');
- * $finder->size(4);
- *
- * @param string $size A size range string
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\SizeRangeFilterIterator
- * @see Symfony\Components\Finder\Comparator\NumberComparator
- */
- public function size($size)
- {
- $this->sizes[] = new Comparator\NumberComparator($size);
- return $this;
- }
- /**
- * Excludes directories.
- *
- * @param string $dir A directory to exclude
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\ExcludeDirectoryFilterIterator
- */
- public function exclude($dir)
- {
- $this->exclude[] = $dir;
- return $this;
- }
- /**
- * Forces the finder to ignore version control directories.
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\IgnoreVcsFilterIterator
- */
- public function ignoreVCS($ignoreVCS)
- {
- $this->ignoreVCS = (Boolean) $ignoreVCS;
- return $this;
- }
- /**
- * Sorts files and directories by an anonymous function.
- *
- * The anonymous function receives two \SplFileInfo instances to compare.
- *
- * This can be slow as all the matching files and directories must be retrieved for comparison.
- *
- * @param Closure $closure An anonymous function
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\SortableIterator
- */
- public function sort(\Closure $closure)
- {
- $this->sort = $closure;
- return $this;
- }
- /**
- * Sorts files and directories by name.
- *
- * This can be slow as all the matching files and directories must be retrieved for comparison.
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\SortableIterator
- */
- public function sortByName()
- {
- $this->sort = Iterator\SortableIterator::SORT_BY_NAME;
- return $this;
- }
- /**
- * Sorts files and directories by type (directories before files), then by name.
- *
- * This can be slow as all the matching files and directories must be retrieved for comparison.
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\SortableIterator
- */
- public function sortByType()
- {
- $this->sort = Iterator\SortableIterator::SORT_BY_TYPE;
- return $this;
- }
- /**
- * Filters the iterator with an anonymous function.
- *
- * The anonymous function receives a \SplFileInfo and must return false
- * to remove files.
- *
- * @param Closure $closure An anonymous function
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @see Symfony\Components\Finder\Iterator\CustomFilterIterator
- */
- public function filter(\Closure $closure)
- {
- $this->filters[] = $closure;
- return $this;
- }
- /**
- * Forces the following of symlinks.
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- */
- public function followLinks()
- {
- $this->followLinks = true;
- return $this;
- }
- /**
- * Searches files and directories which match defined rules.
- *
- * @param string|array $dirs A directory path or an array of directories
- *
- * @return Symfony\Components\Finder\Finder The current Finder instance
- *
- * @throws \InvalidArgumentException if one of the directory does not exist
- */
- public function in($dirs)
- {
- if (!is_array($dirs)) {
- $dirs = array($dirs);
- }
- foreach ($dirs as $dir) {
- if (!is_dir($dir)) {
- throw new \InvalidArgumentException(sprintf('The "%s" directory does not exist.', $dir));
- }
- }
- $this->dirs = array_merge($this->dirs, $dirs);
- return $this;
- }
- /**
- * Returns an Iterator for the current Finder configuration.
- *
- * This method implements the IteratorAggregate interface.
- *
- * @return \Iterator An iterator
- *
- * @throws \LogicException if the in() method has not been called
- */
- public function getIterator()
- {
- if (0 === count($this->dirs)) {
- throw new \LogicException('You must call the in() method before iterating over a Finder.');
- }
- if (1 === count($this->dirs)) {
- return $this->searchInDirectory($this->dirs[0]);
- }
- $iterator = new \AppendIterator();
- foreach ($this->dirs as $dir) {
- $iterator->append($this->searchInDirectory($dir));
- }
- return $iterator;
- }
- protected function searchInDirectory($dir)
- {
- $flags = \FilesystemIterator::SKIP_DOTS;
- if ($this->followLinks) {
- $flags |= \FilesystemIterator::FOLLOW_SYMLINKS;
- }
- $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir, $flags), \RecursiveIteratorIterator::SELF_FIRST);
- if ($this->depths) {
- $iterator = new Iterator\DepthRangeFilterIterator($iterator, $this->depths);
- }
- if ($this->mode) {
- $iterator = new Iterator\FileTypeFilterIterator($iterator, $this->mode);
- }
- if ($this->exclude) {
- $iterator = new Iterator\ExcludeDirectoryFilterIterator($iterator, $this->exclude);
- }
- if ($this->ignoreVCS) {
- $iterator = new Iterator\IgnoreVcsFilterIterator($iterator);
- }
- if ($this->names || $this->notNames) {
- $iterator = new Iterator\FilenameFilterIterator($iterator, $this->names, $this->notNames);
- }
- if ($this->sizes) {
- $iterator = new Iterator\SizeRangeFilterIterator($iterator, $this->sizes);
- }
- if ($this->dates) {
- $iterator = new Iterator\DateRangeFilterIterator($iterator, $this->dates);
- }
- if ($this->filters) {
- $iterator = new Iterator\CustomFilterIterator($iterator, $this->filters);
- }
- if ($this->sort) {
- $iterator = new Iterator\SortableIterator($iterator, $this->sort);
- }
- return $iterator;
- }
- }
|