You are here

class DirectoryOperations in AT Tools 8

Same name and namespace in other branches
  1. 8.3 at_theme_generator/src/File/DirectoryOperations.php \Drupal\at_theme_generator\File\DirectoryOperations
  2. 8.2 at_theme_generator/src/File/DirectoryOperations.php \Drupal\at_theme_generator\File\DirectoryOperations

Hierarchy

Expanded class hierarchy of DirectoryOperations

2 files declare their use of DirectoryOperations
at_theme_generator.install in at_theme_generator/at_theme_generator.install
AT Theme Generator install file.
GenerateForm.php in at_theme_generator/src/Form/GenerateForm.php
Generator form.

File

at_theme_generator/src/File/DirectoryOperations.php, line 10
Contains \Drupal\at_theme_generator\File\DirectoryOperations

Namespace

Drupal\at_theme_generator\File
View source
class DirectoryOperations {

  /**
   * Build, prepare and return the path for generated files.
   *
   * @param array $file_path
   *   Numeric array of path parts (directories).
   * @return string
   *   Path to the prepared directory/s.
   */
  public function directoryPrepare($file_path) {
    $directory_path = implode('/', $file_path);
    if (!file_exists($directory_path)) {
      file_prepare_directory($directory_path, FILE_CREATE_DIRECTORY);
    }
    return $directory_path;
  }

  /**
   * Copy a directory recursively.
   *
   * @param $source
   *   The source directory.
   * @param $target
   *   The target directory.
   * @param $ignore
   *   Regex to filter out unwanted files and directories.
   */
  public function directoryRecursiveCopy($source, $target, $ignore = '/^(\\.(\\.)?|CVS|\\.sass-cache|\\.svn|\\.git|\\.DS_Store)$/') {
    $dir = opendir($source);
    @mkdir($target);
    while ($file = readdir($dir)) {
      if (!preg_match($ignore, $file)) {
        if (is_dir($source . '/' . $file)) {
          self::directoryRecursiveCopy($source . '/' . $file, $target . '/' . $file, $ignore);
        }
        else {
          copy($source . '/' . $file, $target . '/' . $file);
        }
      }
    }
    closedir($dir);
  }

  /**
   * Delete a folder and all files recursively.
   *
   * @param $directory
   * @return bool Returns TRUE on success, FALSE on failure
   * Returns TRUE on success, FALSE on failure
   */
  public function directoryRemove($directory) {
    if (!file_exists($directory)) {
      return false;
    }
    if (is_file($directory)) {
      return unlink($directory);
    }
    $dir = dir($directory);
    while (false !== ($entry = $dir
      ->read())) {
      if ($entry == '.' || $entry == '..') {
        continue;
      }
      self::directoryRemove("{$directory}/{$entry}");
    }
    $dir
      ->close();
    return rmdir($directory);
  }

  /**
   * Scan directories.
   *
   * @param $path
   * @return array Files below the path.
   * Files below the path.
   */
  public function directoryScan($path) {
    $scan_directories = array();
    if (file_exists($path)) {
      $scan_directories = preg_grep('/^([^.])/', scandir($path));
    }
    return $scan_directories;
  }

  /**
   * Scan directories recursively.
   *
   * @param $path
   * @return array Directories & files below the path.
   * Directories & files below the path.
   */
  public function directoryScanRecursive($path) {
    $scan_directories_recursive = array();
    $path_directory = scandir($path);
    foreach ($path_directory as $key => $value) {
      if (!in_array($value, array(
        ".",
        "..",
      ))) {
        if (is_dir($path . '/' . $value)) {
          $scan_directories_recursive[$value] = self::directoryScanRecursive($path . '/' . $value);
        }
        else {
          $scan_directories_recursive[] = $value;
        }
      }
    }
    return $scan_directories_recursive;
  }

  /**
   * Recursively glob files below the path
   * of a specified type.
   *
   * @param $path
   * @param array $types
   * @return array globbed files
   */
  public function directoryGlob($path, array $types) {
    $files = array();
    $scan_directories = self::directoryScan($path);
    if (isset($scan_directories)) {
      foreach ($scan_directories as $directory) {
        $glob_path = $types . $directory;
        if (is_dir($glob_path)) {
          if (isset($types)) {
            foreach ($types as $type) {
              $files[$directory][$type] = array_filter(glob($glob_path . "/*.{$type}"), 'is_file');
            }
          }
        }
      }
    }
    return $files;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DirectoryOperations::directoryGlob public function Recursively glob files below the path of a specified type.
DirectoryOperations::directoryPrepare public function Build, prepare and return the path for generated files.
DirectoryOperations::directoryRecursiveCopy public function Copy a directory recursively.
DirectoryOperations::directoryRemove public function Delete a folder and all files recursively.
DirectoryOperations::directoryScan public function Scan directories.
DirectoryOperations::directoryScanRecursive public function Scan directories recursively.