function drush_scan_directory

8.0.x filesystem.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0)
6.x filesystem.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = , $recurse_max_depth = TRUE, $key = 'filename', $min_depth = , $include_dot_files = FALSE, $depth = )
7.x filesystem.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0)
3.x command.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0)
4.x command.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0)
5.x filesystem.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0)
master filesystem.inc drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0)

Finds all files that match a given mask in a given directory. Directories and files beginning with a period are excluded; this prevents hidden files and directories (such as SVN working directories and GIT repositories) from being scanned.

Parameters

$dir: The base directory for the scan, without trailing slash.

$mask: The regular expression of the files to find.

$nomask: An array of files/directories to ignore.

$callback: The callback function to call for each match.

$recurse_max_depth: When TRUE, the directory scan will recurse the entire tree starting at the provided directory. When FALSE, only files in the provided directory are returned. Integer values limit the depth of the traversal, with zero being treated identically to FALSE, and 1 limiting the traversal to the provided directory and its immediate children only, and so on.

$key: The key to be used for the returned array of files. Possible values are "filename", for the path starting with $dir, "basename", for the basename of the file, and "name" for the name of the file without an extension.

$min_depth: Minimum depth of directories to return files from.

$include_dot_files: If TRUE, files that begin with a '.' will be returned if they match the provided mask. If FALSE, files that begin with a '.' will not be returned, even if they match the provided mask.

$depth: Current depth of recursion. This parameter is only used internally and should not be passed.

Return value

An associative array (keyed on the provided key) of objects with "path", "basename", and "name" members corresponding to the matching files.

10 calls to drush_scan_directory()
drush_core_find_project_path in commands/core/core.drush.inc
drush_core_php_script in commands/core/core.drush.inc
Command callback. Runs "naked" php scripts.
pm_drush_pm_adjust_download_destination in commands/pm/pm.drush.inc
Built-in adjust-download-destination hook. This particular version of the hook will move modules that contain only drush commands to /usr/share/drush/commands if it exists, or $HOME/.drush if the site-wide location does not exist.
_drush_find_commandfiles in includes/command.inc
_drush_find_local_sites_at_root in includes/sitealias.inc
Return a list of all of the local sites at the specified drupal root.

... See full list

File

includes/command.inc, line 727
The drush command engine.

Code

function drush_scan_directory($dir, $mask, $nomask = array('.', '..', 'CVS'), $callback = 0, $recurse_max_depth = TRUE, $key = 'filename', $min_depth = 0, $include_dot_files = FALSE, $depth = 0) {
  $key = (in_array($key, array('filename', 'basename', 'name')) ? $key : 'filename');
  $files = array();

  if (is_dir($dir) && $handle = opendir($dir)) {
    while (FALSE !== ($file = readdir($handle))) {
      if (!in_array($file, $nomask) && (($include_dot_files && (!preg_match("/\.\+/", $file))) || ($file[0] != '.'))) {
        if (is_dir("$dir/$file") && (($recurse_max_depth === TRUE) || ($depth < $recurse_max_depth))) {
          // Give priority to files in this folder by merging them in after any subdirectory files.
          $files = array_merge(drush_scan_directory("$dir/$file", $mask, $nomask, $callback, $recurse_max_depth, $key, $min_depth, $include_dot_files, $depth + 1), $files);
        }
        elseif ($depth >= $min_depth && preg_match($mask, $file)) {
          // Always use this match over anything already set in $files with the same $$key.
          $filename = "$dir/$file";
          $basename = basename($file);
          $name = substr($basename, 0, strrpos($basename, '.'));
          $files[$$key] = new stdClass();
          $files[$$key]->filename = $filename;
          $files[$$key]->basename = $basename;
          $files[$$key]->name = $name;
          if ($callback) {
            $callback($filename);
          }
        }
      }
    }

    closedir($handle);
  }

  return $files;
}