You are here

protected function BaseValidatorForm::scanDirectory in Advanced CSS/JS Aggregation 8.4

Same name and namespace in other branches
  1. 8.2 advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::scanDirectory()
  2. 8.3 advagg_validator/src/Form/BaseValidatorForm.php \Drupal\advagg_validator\Form\BaseValidatorForm::scanDirectory()

Finds all files that match a given mask in a given directory.

Directories and files beginning with a period are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned.

Parameters

string $dir: The base directory or URI to scan, without trailing slash.

string $mask: The preg_match() regular expression of the files to find.

array $options: An associative array of additional options, with the following elements:

  • 'nomask': The preg_match() regular expression of the files to ignore. Defaults to '/(\.\.?|CVS)$/'.
  • 'nomask': The preg_match() regular expression of the dirs to ignore. Defaults to '/(\.git)/'.
  • 'callback': The callback function to call for each match. There is no default callback.
  • 'recurse': When TRUE, the directory scan will recurse the entire tree starting at the provided directory. Defaults to TRUE.
  • 'key': The key to be used for the returned associative array of files. Possible values are 'uri', for the file's URI; 'filename', for the basename of the file; and 'name' for the name of the file without the extension. Defaults to 'uri'.
  • 'min_depth': Minimum depth of directories to return files from. Defaults to 0.

int $depth: Current depth of recursion. This parameter is only used internally and should not be passed in.

Return value

array An associative array (keyed on the chosen key) of objects with 'uri', 'filename', and 'name' members corresponding to the matching files.

1 call to BaseValidatorForm::scanDirectory()
BaseValidatorForm::scanAllDirs in advagg_validator/src/Form/BaseValidatorForm.php
Recursively scan the drupal webroot for files matching the given extension.

File

advagg_validator/src/Form/BaseValidatorForm.php, line 286

Class

BaseValidatorForm
Base form for all advagg validator options.

Namespace

Drupal\advagg_validator\Form

Code

protected function scanDirectory($dir, $mask, array $options = [], $depth = 0) {

  // Merge in defaults.
  $options += [
    'nomask' => '/(\\.\\.?|CVS)$/',
    'nodirmask' => '/(\\.git)/',
    'callback' => 0,
    'recurse' => TRUE,
    'key' => 'uri',
    'min_depth' => 0,
  ];
  $options['key'] = in_array($options['key'], [
    'uri',
    'filename',
    'name',
  ]) ? $options['key'] : 'uri';
  $files = [];
  if (is_dir($dir)) {
    $handle = opendir($dir);
    if ($handle) {
      while (FALSE !== ($filename = readdir($handle))) {

        // Skip if filename matches the nomask or is '.'.
        if (preg_match($options['nomask'], $filename) || $filename[0] === '.') {
          continue;
        }
        $uri = "{$dir}/{$filename}";
        $uri = $this->streamWrapperManager
          ->normalizeUri($uri);
        if (is_dir($uri) && $options['recurse'] && !preg_match($options['nodirmask'], $uri)) {

          // Give priority to files in this folder by merging them in after
          // any subdirectory files.
          $files = array_merge($this
            ->scanDirectory($uri, $mask, $options, $depth + 1), $files);
        }
        elseif ($depth >= $options['min_depth'] && preg_match($mask, $filename)) {

          // Always use this match over anything already set in $files with
          // the same $$options['key'].
          $file = new \stdClass();
          $file->uri = $uri;
          $file->filename = $filename;
          $file->name = pathinfo($filename, PATHINFO_FILENAME);
          $key = $options['key'];
          $files[$file->{$key}] = $file;
          if ($options['callback']) {
            $options['callback']($uri);
          }
        }
      }
      closedir($handle);
    }
  }
  return $files;
}