Symfony\Component\Finder\Finder::getIterator PHP Method

getIterator() public method

This method implements the IteratorAggregate interface.
public getIterator ( ) : Iterator | Symfony\Component\Finder\SplFileInfo[]
return Iterator | Symfony\Component\Finder\SplFileInfo[] An iterator
    public function getIterator()
    {
        if (0 === count($this->dirs) && 0 === count($this->iterators)) {
            throw new \LogicException('You must call one of in() or append() methods before iterating over a Finder.');
        }

        if (1 === count($this->dirs) && 0 === count($this->iterators)) {
            return $this->searchInDirectory($this->dirs[0]);
        }

        $iterator = new \AppendIterator();
        foreach ($this->dirs as $dir) {
            $iterator->append($this->searchInDirectory($dir));
        }

        foreach ($this->iterators as $it) {
            $iterator->append($it);
        }

        return $iterator;
    }

Usage Example

 /**
  * import
  *
  * @param string                $sourceDir
  * @param DocumentNodeInterface $targetNode
  * @param array                 $excludes
  */
 public function import($sourceDir, DocumentNodeInterface $targetNode, array $excludes = array())
 {
     if (!is_dir($sourceDir)) {
         throw new \InvalidArgumentException(sprintf('The directory %s does not exist', $sourceDir));
     }
     $this->emn->persist($targetNode);
     $currentNode = array(0 => $targetNode);
     $finder = new Finder();
     $finder->in($sourceDir);
     $files = $finder->getIterator();
     foreach ($files as $file) {
         foreach ($excludes as $exclude) {
             if (strpos($file->getRelativePathname(), $exclude) !== false) {
                 continue 2;
             }
         }
         $depth = $files->getDepth();
         if ($file->isDir()) {
             $currentNode[$depth + 1] = $this->importDir($currentNode, $depth, $file, $targetNode);
         } elseif ($file->isFile()) {
             $this->importFile($currentNode, $depth, $file);
         }
     }
     $this->emn->flush();
 }
All Usage Examples Of Symfony\Component\Finder\Finder::getIterator