You are here

class ScanDir in Gutenberg 8

Same name and namespace in other branches
  1. 8.2 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.
   */
  public static function scan() {

    // Initialize defaults.
    self::$recursive = FALSE;
    self::$directories = [];
    self::$files = [];
    self::$extFilter = FALSE;

    // Check we have minimum parameters.
    if (!($args = func_get_args())) {
      die("Must provide a path string or array of path strings");
    }
    if (gettype($args[0]) != "string" && gettype($args[0]) != "array") {
      die("Must provide a path string or array of path strings");
    }

    // Check if recursive scan | default action: no sub-directories.
    if (isset($args[2]) && $args[2] == TRUE) {
      self::$recursive = TRUE;
    }

    // Was a filter on file extensions included? | default action: return all
    // file types.
    if (isset($args[1])) {
      if (gettype($args[1]) == "array") {
        self::$extFilter = array_map('strtolower', $args[1]);
      }
      elseif (gettype($args[1]) == "string") {
        self::$extFilter[] = strtolower($args[1]);
      }
    }

    // Grab path(s)
    self::verifyPaths($args[0]);
    return array_map(function ($entry) {
      return substr($entry, 3, strlen($entry) - 1);
    }, self::$files);
  }

  /**
   * Verifies paths.
   */
  private static function verifyPaths($paths) {
    $path_errors = [];
    if (gettype($paths) == "string") {
      $paths = [
        $paths,
      ];
    }
    foreach ($paths as $path) {
      if (is_dir($path)) {
        self::$directories[] = $path;
        $dirContents = self::findContents($path);
      }
      else {
        $path_errors[] = $path;
      }
    }
    if ($path_errors) {
      echo "The following directories do not exists<br />";
      die(var_dump($path_errors));
    }
  }

  /**
   * 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 $value) {
          self::$files[] = $result[] = $value;
        }
      }
    }

    // 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.