You are here

public function FileSystem::scanDirectory in Drupal 9

Same name and namespace in other branches
  1. 8 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::scanDirectory()

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

Directories and files beginning with a dot are excluded; this prevents hidden files and directories (such as SVN working directories) from being scanned. Use the umask option to skip configuration directories to eliminate the possibility of accidentally exposing configuration information. Also, you can use the base directory, recurse, and min_depth options to improve performance by limiting how much of the filesystem has to be traversed.

Parameters

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

string $mask: The preg_match() regular expression for files to be included.

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

  • 'nomask': The preg_match() regular expression for files to be excluded. Defaults to the 'file_scan_ignore_directories' setting.
  • '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.

Return value

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

Throws

\Drupal\Core\File\Exception\NotRegularDirectoryException If the directory does not exist.

Overrides FileSystemInterface::scanDirectory

File

core/lib/Drupal/Core/File/FileSystem.php, line 655

Class

FileSystem
Provides helpers to operate on files and stream wrappers.

Namespace

Drupal\Core\File

Code

public function scanDirectory($dir, $mask, array $options = []) {

  // Merge in defaults.
  $options += [
    'callback' => 0,
    'recurse' => TRUE,
    'key' => 'uri',
    'min_depth' => 0,
  ];
  $dir = $this->streamWrapperManager
    ->normalizeUri($dir);
  if (!is_dir($dir)) {
    throw new NotRegularDirectoryException("{$dir} is not a directory.");
  }

  // Allow directories specified in settings.php to be ignored. You can use
  // this to not check for files in common special-purpose directories. For
  // example, node_modules and bower_components. Ignoring irrelevant
  // directories is a performance boost.
  if (!isset($options['nomask'])) {
    $ignore_directories = $this->settings
      ->get('file_scan_ignore_directories', []);
    array_walk($ignore_directories, function (&$value) {
      $value = preg_quote($value, '/');
    });
    $options['nomask'] = '/^' . implode('|', $ignore_directories) . '$/';
  }
  $options['key'] = in_array($options['key'], [
    'uri',
    'filename',
    'name',
  ]) ? $options['key'] : 'uri';
  return $this
    ->doScanDirectory($dir, $mask, $options);
}