function FileSystem::scanDirectory

Same name in other branches
  1. 9 core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::scanDirectory()
  2. 8.9.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::scanDirectory()
  3. 11.x core/lib/Drupal/Core/File/FileSystem.php \Drupal\Core\File\FileSystem::scanDirectory()

File

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

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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.