12 namespace Symfony\Component\Finder;
45 class Finder implements \IteratorAggregate, \Countable
70 private static $vcsPatterns = array(
'.svn',
'_svn',
'CVS',
'_darcs',
'.arch-params',
'.monotone',
'.bzr',
'.git',
'.hg');
77 $this->ignore = static::IGNORE_VCS_FILES | static::IGNORE_DOT_FILES;
109 $this->adapters[$adapter->
getName()] = array(
110 'adapter' => $adapter,
111 'priority' => $priority,
141 if (!isset($this->adapters[$name])) {
142 throw new \InvalidArgumentException(sprintf(
'Adapter "%s" does not exist.', $name));
146 $this->adapters[$name][
'selected'] =
true;
158 $this->adapters = array();
170 return array_values(array_map(
function (array $adapter) {
171 return $adapter[
'adapter'];
184 $this->mode = Iterator\FileTypeFilterIterator::ONLY_DIRECTORIES;
198 $this->mode = Iterator\FileTypeFilterIterator::ONLY_FILES;
273 $this->names[] = $pattern;
291 $this->notNames[] = $pattern;
356 $this->paths[] = $pattern;
379 $this->notPaths[] = $pattern;
438 if ($ignoreDotFiles) {
439 $this->ignore |= static::IGNORE_DOT_FILES;
461 $this->ignore |= static::IGNORE_VCS_FILES;
478 foreach ((array) $pattern as $p) {
479 self::$vcsPatterns[] = $p;
482 self::$vcsPatterns = array_unique(self::$vcsPatterns);
500 public function sort(\Closure $closure)
502 $this->
sort = $closure;
520 $this->
sort = Iterator\SortableIterator::SORT_BY_NAME;
538 $this->
sort = Iterator\SortableIterator::SORT_BY_TYPE;
558 $this->
sort = Iterator\SortableIterator::SORT_BY_ACCESSED_TIME;
580 $this->
sort = Iterator\SortableIterator::SORT_BY_CHANGED_TIME;
600 $this->
sort = Iterator\SortableIterator::SORT_BY_MODIFIED_TIME;
619 public function filter(\Closure $closure)
621 $this->filters[] = $closure;
669 $resolvedDirs = array();
671 foreach ((array)
$dirs as $dir) {
673 $resolvedDirs[] = $dir;
674 }
elseif ($glob = glob($dir, (defined(
'GLOB_BRACE') ? GLOB_BRACE : 0) | GLOB_ONLYDIR)) {
675 $resolvedDirs = array_merge($resolvedDirs, $glob);
677 throw new \InvalidArgumentException(sprintf(
'The "%s" directory does not exist.', $dir));
681 $this->dirs = array_merge($this->dirs, $resolvedDirs);
697 if (0 ===
count($this->dirs) && 0 ===
count($this->iterators)) {
698 throw new \LogicException(
'You must call one of in() or append() methods before iterating over a Finder.');
701 if (1 ===
count($this->dirs) && 0 ===
count($this->iterators)) {
705 $iterator = new \AppendIterator();
706 foreach ($this->dirs as $dir) {
710 foreach ($this->iterators as $it) {
711 $iterator->append($it);
730 if ($iterator instanceof \IteratorAggregate) {
731 $this->iterators[] = $iterator->getIterator();
732 }
elseif ($iterator instanceof \Iterator) {
733 $this->iterators[] = $iterator;
734 }
elseif ($iterator instanceof \Traversable || is_array($iterator)) {
735 $it = new \ArrayIterator();
736 foreach ($iterator as $file) {
739 $this->iterators[] = $it;
741 throw new \InvalidArgumentException(
'Finder::append() method wrong argument type.');
762 uasort($this->adapters,
function (array $a, array $b) {
763 if ($a[
'selected'] || $b[
'selected']) {
764 return $a[
'selected'] ? -1 : 1;
767 return $a[
'priority'] > $b[
'priority'] ? -1 : 1;
782 if (static::IGNORE_VCS_FILES === (static::IGNORE_VCS_FILES & $this->ignore)) {
786 if (static::IGNORE_DOT_FILES === (static::IGNORE_DOT_FILES & $this->ignore)) {
787 $this->notPaths[] =
'#(^|/)\..+(/|$)#';
790 foreach ($this->adapters as $adapter) {
791 if ($adapter[
'adapter']->isSupported()) {
794 ->buildAdapter($adapter[
'adapter'])
795 ->searchInDirectory($dir);
801 throw new \RuntimeException(
'No supported adapter found.');
813 ->setDepths($this->depths)
814 ->setMode($this->mode)
816 ->setNames($this->names)
817 ->setNotNames($this->notNames)
820 ->setSizes($this->sizes)
821 ->setDates($this->dates)
822 ->setFilters($this->filters)
823 ->setSort($this->
sort)
824 ->setPath($this->paths)
825 ->setNotPath($this->notPaths)
834 $this->adapters = array_map(
function (array $properties) {
835 $properties[
'selected'] =
false;