/** * Scan given directory for files, returning splFileObjects matching the include/exclude patterns * * @param string $path Path to work on * @param boolean $recursive Scan recursively or not * * @throws Exception * @return \Iterator */ public function getIterator($path, $recursive = true) { if (!file_exists($path)) { throw new Exception("Path '{$path}' does not exist.", Exception::NotFound); } if ($recursive) { $worker = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path, $this->flags)); } else { $worker = new \DirectoryIterator($path); } $filter = new IncludeExcludeFilterIterator(new FilesOnlyFilterIterator($worker)); $filter->setInclude(count($this->include) ? $this->include : array('*')); $filter->setExclude($this->exclude); return $filter; }
/** * Magic invoker method to use object in foreach-alike constructs as iterator, * returning splFileObjects matching the include/exclude patterns of given path * * @param string $path Path to work on * * @return Iterator */ public function __invoke($path) { if (!file_exists($path)) { throw new DirectoryScannerException("Path '{$path}' does not exist.", ScannerException::NotFound); } $filter = new IncludeExcludeFilterIterator(new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path))); $filter->setInclude(count($this->include) ? $this->include : array('*')); $filter->setExclude($this->exclude); return $filter; }