You are here

protected function FileDirectorySource::_getFilesFromDirectory in Backup and Migrate 8.4

@internal param string $dir

Parameters

$base_path: The name of the directory to list. This must always end in '/'.

string $subdir:

Return value

array

1 call to FileDirectorySource::_getFilesFromDirectory()
FileDirectorySource::getFilesToBackup in lib/backup_migrate_core/src/Source/FileDirectorySource.php
Get a list if files to be backed up from the given directory.

File

lib/backup_migrate_core/src/Source/FileDirectorySource.php, line 173

Class

FileDirectorySource
Class FileDirectorySource.

Namespace

BackupMigrate\Core\Source

Code

protected function _getFilesFromDirectory($base_path, $subdir = '') {
  $out = $errors = [];

  // Open the directory.
  if (!($handle = opendir($base_path . $subdir))) {
    $errors[] = $base_path . $subdir;
  }
  else {
    while (($file = readdir($handle)) !== FALSE) {

      // If not a dot file and the file name isn't excluded.
      if ($file != '.' && $file != '..') {

        // Get the full path of the file.
        $path = $base_path . $subdir . $file;

        // Allow filters to modify or exclude this path.
        $path = $this
          ->plugins()
          ->call('beforeFileBackup', $path, [
          'source' => $this,
          'base_path' => $base_path,
        ]);
        if ($path) {
          if (is_dir($path)) {
            list($sub_files, $sub_errors) = $this
              ->_getFilesFromDirectory($base_path, $subdir . $file . '/');

            // Add the directory if it is empty.
            if (empty($sub_files)) {
              $out[$subdir . $file] = $path;
            }

            // Add the sub-files to the output.
            $out = array_merge($out, $sub_files);
            $errors = array_merge($errors, $sub_errors);
          }
          else {
            if (is_readable($path)) {
              $out[$subdir . $file] = $path;
            }
            else {
              $errors[] = $path;
            }
          }
        }
      }
    }
    closedir($handle);
  }
  return [
    $out,
    $errors,
  ];
}