You are here

class DrupalDirectoryDestination in Backup and Migrate 5.0.x

@package Drupal\backup_migrate\Drupal\Destination

Hierarchy

Expanded class hierarchy of DrupalDirectoryDestination

1 file declares its use of DrupalDirectoryDestination
DrupalConfigHelper.php in src/Drupal/Config/DrupalConfigHelper.php

File

src/Drupal/Destination/DrupalDirectoryDestination.php, line 17

Namespace

Drupal\backup_migrate\Drupal\Destination
View source
class DrupalDirectoryDestination extends DirectoryDestination {

  /**
   * Do the actual file save.
   *
   * This function is called to save the data file AND the metadata sidecar
   * file.
   *
   * @param \Drupal\backup_migrate\Core\File\BackupFileReadableInterface $file
   *
   * @throws \Drupal\backup_migrate\Core\Exception\BackupMigrateException
   */
  public function saveTheFile(BackupFileReadableInterface $file) {

    // Check if the directory exists.
    $this
      ->checkDirectory();
    try {
      \Drupal::service('file_system')
        ->move($file
        ->realpath(), $this
        ->idToPath($file
        ->getFullName()), FileSystemInterface::EXISTS_REPLACE);
    } catch (FileException $e) {
      return FALSE;
    }
  }

  /**
   * Check that the directory can be used for backup.
   *
   * @throws \Drupal\backup_migrate\Core\Exception\BackupMigrateException
   */
  protected function checkDirectory() {

    // @todo Figure out if the file is or might be accessible via the web.
    $dir = $this
      ->confGet('directory');
    $is_private = strpos($dir, 'private://') === 0;

    // Attempt to create/prepare the directory if it is in the private
    // directory.
    if ($is_private) {
      if (!PrivateStream::basePath()) {
        throw new BackupMigrateException("The backup file could not be saved to '%dir' because your private files system path has not been set.", [
          '%dir' => $dir,
        ]);
      }
      if (!\Drupal::service('file_system')
        ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY && FileSystemInterface::MODIFY_PERMISSIONS)) {
        throw new BackupMigrateException("The backup file could not be saved to '%dir' because the directory could not be created or cannot be written to. Please make sure your private files directory is writable by the web server.", [
          '%dir' => $dir,
        ]);
      }
    }
    else {

      // If the file is local to the server.
      $real = \Drupal::service('file_system')
        ->realpath($dir);
      if ($real) {

        // If the file is within the docroot.
        $in_root = strpos($real, DRUPAL_ROOT) === 0;
        if ($in_root) {
          throw new BackupMigrateException("The backup file could not be saved to '%dir' because that directory may be publicly accessible via the web. Please save your backups to the private file directory or a directory outside of the web root.", [
            '%dir' => $dir,
          ]);
        }
      }
    }

    // Do the regular exists/writable checks.
    parent::checkDirectory();

    // @todo Warn if the realpath cannot be resolved (because we cannot
    // determine if the file is publicly accessible).
  }

  /**
   * {@inheritdoc}
   */
  public function queryFiles(array $filters = [], $sort = 'datestamp', $sort_direction = SORT_DESC, $count = 100, $start = 0) {

    // Get the full list of files.
    $out = $this
      ->listFiles($count + $start);
    foreach ($out as $key => $file) {
      $out[$key] = $this
        ->loadFileMetadata($file);
    }

    // Filter the output.
    if ($filters) {
      $out = array_filter($out, function ($file) use ($filters) {
        foreach ($filters as $key => $value) {
          if ($file
            ->getMeta($key) !== $value) {
            return FALSE;
          }
        }
        return TRUE;
      });
    }

    // Sort the files.
    if ($sort && $sort_direction) {
      uasort($out, function ($a, $b) use ($sort, $sort_direction) {
        if ($sort_direction == SORT_DESC) {
          if ($sort == 'name') {
            return $a
              ->getFullName() <=> $b
              ->getFullName();
          }

          // @todo fix this in core
          return $a
            ->getMeta($sort) <=> $b
            ->getMeta($sort);
        }
        else {
          if ($sort == 'name') {
            return $b
              ->getFullName() <=> $a
              ->getFullName();
          }

          // @todo fix this in core
          return $b
            ->getMeta($sort) <=> $a
            ->getMeta($sort);
        }
      });
    }

    // Slice the return array.
    if ($count || $start) {
      $out = array_slice($out, $start, $count);
    }
    return $out;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ConfigurableTrait::$config protected property The object's configuration object.
ConfigurableTrait::$init protected property The initial configuration.
ConfigurableTrait::confGet public function Get a specific value from the configuration.
ConfigurableTrait::config public function Get the configuration object for this item.
ConfigurableTrait::configDefaults public function Get the default values for the plugin. 10
ConfigurableTrait::configErrors public function Get any validation errors in the config.
ConfigurableTrait::setConfig public function Set the configuration for all plugins. 1
ConfigurableTrait::__construct public function 2
DestinationBase::isRemote public function
DestinationBase::loadFileMetadata public function Load the metadata for the given file however it may be stored. Overrides ReadableDestinationInterface::loadFileMetadata
DestinationBase::supportedOps public function Get a list of supported operations and their weight. Overrides PluginBase::supportedOps
DirectoryDestination::checkWritable public function Overrides DestinationBase::checkWritable
DirectoryDestination::configSchema public function Get a definition for user-configurable settings. Overrides ConfigurableTrait::configSchema
DirectoryDestination::countFiles public function Overrides ListableDestinationInterface::countFiles
DirectoryDestination::deleteTheFile public function Do the actual delete for a file. Overrides DestinationBase::deleteTheFile
DirectoryDestination::fileExists public function Does the file with the given id (filename) exist in this destination. Overrides ReadableDestinationInterface::fileExists
DirectoryDestination::getAllFileNames protected function Get the entire file list from this destination.
DirectoryDestination::getFile public function Get a file object representing the file with the given ID from the dest. Overrides ReadableDestinationInterface::getFile
DirectoryDestination::idToPath protected function Return a file path for the given file id.
DirectoryDestination::listFiles public function Return a list of files from the destination. Overrides ListableDestinationInterface::listFiles
DirectoryDestination::loadFileForReading public function Load the file with the given ID from the destination. Overrides ReadableDestinationInterface::loadFileForReading
DirectoryDestination::saveFile public function Save a file to the destination. Overrides DestinationBase::saveFile
DrupalDirectoryDestination::checkDirectory protected function Check that the directory can be used for backup. Overrides DirectoryDestination::checkDirectory
DrupalDirectoryDestination::queryFiles public function Run a basic query with sort on the list of files. Overrides DirectoryDestination::queryFiles
DrupalDirectoryDestination::saveTheFile public function Do the actual file save. Overrides DirectoryDestination::saveTheFile
FileProcessorTrait::$tempfilemanager protected property
FileProcessorTrait::alterMime public function Provide the file mime for the given file extension if known.
FileProcessorTrait::getTempFileManager public function Get the temp file manager.
FileProcessorTrait::setTempFileManager public function Inject the temp file manager.
PluginBase::opWeight public function What is the weight of the given operation for this plugin. Overrides PluginInterface::opWeight
PluginBase::supportsOp public function Does this plugin implement the given operation. Overrides PluginInterface::supportsOp
SidecarMetadataDestinationTrait::arrayToIni protected function
SidecarMetadataDestinationTrait::deleteFile public function
SidecarMetadataDestinationTrait::iniToArray protected function Parse an INI file's contents.
SidecarMetadataDestinationTrait::loadFileMetadataArray protected function
SidecarMetadataDestinationTrait::saveTheFileMetadata protected function
TranslatableTrait::$translator protected property
TranslatableTrait::setTranslator public function
TranslatableTrait::t public function Translate the given string if there is a translator service available.