You are here

public static function ScanDir::scan in Gutenberg 8.2

Same name and namespace in other branches
  1. 8 src/ScanDir.php \Drupal\gutenberg\ScanDir::scan()

Scans directories.

Parameters

string|array $root_dirs: The root directory.

string|array $extension_filters: The extension filter(s).

bool $recursive: Whether to scan recursively.

Return value

array The scan results.

1 call to ScanDir::scan()
gutenberg-dependencies.php in scripts/gutenberg-dependencies.php
Gets Gutenberg dependencies.

File

src/ScanDir.php, line 51

Class

ScanDir
Class ScanDir.

Namespace

Drupal\gutenberg

Code

public static function scan($root_dirs, $extension_filters = NULL, $recursive = FALSE) {

  // Initialize defaults.
  self::$recursive = FALSE;
  self::$directories = [];
  self::$files = [];
  self::$extFilter = FALSE;
  if (!is_string($root_dirs) && !is_array($root_dirs)) {
    throw new \LogicException('Must provide a path string or array of path strings');
  }

  // Check if recursive scan | default action: no sub-directories.
  self::$recursive = $recursive;

  // Was a filter on file extensions included? | default action: return all
  // file types.
  if (isset($extension_filters)) {
    if (is_array($extension_filters)) {
      self::$extFilter = array_map('strtolower', $extension_filters);
    }
    elseif (is_string($extension_filters)) {
      self::$extFilter[] = strtolower($extension_filters);
    }
  }

  // Grab path(s)
  if (is_string($root_dirs)) {
    $root_dirs = [
      $root_dirs,
    ];
  }
  self::verifyPaths($root_dirs);
  return array_map(function ($entry) use ($root_dirs) {
    $asset = $entry;
    foreach ($root_dirs as $root_dir) {

      // Strip out the root directory prefix.
      $root_dir .= DIRECTORY_SEPARATOR;
      $root_dir_length = strlen($root_dir);
      if (substr($entry, 0, $root_dir_length) === $root_dir) {
        $asset = substr($entry, $root_dir_length);
        break;
      }
    }
    if ('\\' === \DIRECTORY_SEPARATOR) {

      // Replace the directory separator with a forward slash when on Windows.
      $asset = str_replace(DIRECTORY_SEPARATOR, '/', $asset);
    }
    return $asset;
  }, self::$files);
}