You are here

class ScanDir in Gutenberg 8.2

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

Class ScanDir.

Hierarchy

Expanded class hierarchy of ScanDir

1 file declares its use of ScanDir
gutenberg-dependencies.php in scripts/gutenberg-dependencies.php
Gets Gutenberg dependencies.

File

src/ScanDir.php, line 8

Namespace

Drupal\gutenberg
View source
class ScanDir {

  /**
   * The scanned directories.
   *
   * @var array
   */
  private static $directories;

  /**
   * The list of files.
   *
   * @var array
   */
  private static $files;

  /**
   * Whether a filter was included on file extensions.
   *
   * @var bool|array
   */
  private static $extFilter;

  /**
   * Whether recursive mode is enabled.
   *
   * @var bool
   */
  private static $recursive;

  /**
   * Scans directories.
   *
   * @param string|array $root_dirs
   *   The root directory.
   * @param string|array $extension_filters
   *   The extension filter(s).
   * @param bool $recursive
   *   Whether to scan recursively.
   *
   * @return array
   *   The scan results.
   */
  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);
  }

  /**
   * Verifies paths.
   *
   * @param array $paths
   *   Paths to scan.
   */
  private static function verifyPaths(array $paths) {
    $path_errors = [];
    foreach ($paths as $path) {
      if (is_dir($path)) {
        self::$directories[] = $path;
        self::findContents($path);
      }
      else {
        $path_errors[] = $path;
      }
    }
    if ($path_errors) {
      throw new \RuntimeException("The following directories do not exists\n" . var_export($path_errors, TRUE));
    }
  }

  /**
   * This is how we scan directories.
   */
  private static function findContents($dir) {
    $result = [];
    $root = scandir($dir);
    foreach ($root as $value) {
      if ($value === '.' || $value === '..') {
        continue;
      }
      if (is_file($dir . DIRECTORY_SEPARATOR . $value)) {
        if (!self::$extFilter || in_array(strtolower(pathinfo($dir . DIRECTORY_SEPARATOR . $value, PATHINFO_EXTENSION)), self::$extFilter)) {
          self::$files[] = $result[] = $dir . DIRECTORY_SEPARATOR . $value;
        }
        continue;
      }
      if (self::$recursive) {
        foreach (self::findContents($dir . DIRECTORY_SEPARATOR . $value) as $entry) {
          self::$files[] = $result[] = $entry;
        }
      }
    }

    // Return required for recursive search.
    return $result;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ScanDir::$directories private static property The scanned directories.
ScanDir::$extFilter private static property Whether a filter was included on file extensions.
ScanDir::$files private static property The list of files.
ScanDir::$recursive private static property Whether recursive mode is enabled.
ScanDir::findContents private static function This is how we scan directories.
ScanDir::scan public static function Scans directories.
ScanDir::verifyPaths private static function Verifies paths.