Example #1
0
 /**
  * Scan directory looking for files matching patterns.
  *
  * @method scan
  * @param  string [$path=null]
  */
 public function scan($path = null)
 {
     // If first call (no path provided).
     if ($path === null) {
         // Reset files tree.
         $this->files = [];
         // Set root path as first path to scan.
         $path = $this->path;
     }
     $includedFiles = [];
     $excludedFiles = [];
     $excludedDirectories = [];
     $files = [];
     // For each file/directory
     foreach (scandir($path, SCANDIR_SORT_NONE) as $file) {
         // Skip curent and parent directory.
         if (in_array($file, array('.', '..'))) {
             continue;
         }
         // Create {File} instance.
         $file = new File($this->path, $path, $file);
         // If matches exclude files pattern.
         if ($file->nameMatch($this->config->get('excludeFiles')) or $file->pathMatch($this->config->get('excludePaths'))) {
             // Add file to excluded files.
             if ($file->isDirectory()) {
                 $excludedDirectories[$file->getRelativePath()] = $file;
             } else {
                 $excludedFiles[$file->getRelativePath()] = $file;
             }
             // Go to next item.
             continue;
         }
         // If directory.
         if ($file->isDirectory()) {
             // Merge sub-files matching patterns with current tree.
             $files += $this->scan($file->getAbsolutePath());
             // Go to next item.
             continue;
         }
         // If file type and matches one pattern.
         if ($file->nameMatch($this->config->get('includeFiles'))) {
             // Add file to found files.
             $includedFiles[$file->getRelativePath()] = $file;
         }
     }
     ksort($excludedFiles, SORT_NATURAL | SORT_FLAG_CASE);
     ksort($excludedDirectories, SORT_NATURAL | SORT_FLAG_CASE);
     ksort($includedFiles, SORT_NATURAL | SORT_FLAG_CASE);
     return $this->files = array_merge_recursive(compact('excludedFiles', 'excludedDirectories', 'includedFiles'), $files);
 }