You are here

public function FileDirectorySource::importFromFile in Backup and Migrate 5.0.x

Import to this source from the given backup file.

This is the main restore function for this source.

Parameters

\Drupal\backup_migrate\Core\File\BackupFileReadableInterface $file: The file to read the backup from. It will not be opened for reading.

Overrides SourceInterface::importFromFile

1 method overrides FileDirectorySource::importFromFile()
DrupalSiteArchiveSource::importFromFile in src/Drupal/Source/DrupalSiteArchiveSource.php
Import to this source from the given backup file.

File

src/Core/Source/FileDirectorySource.php, line 80

Class

FileDirectorySource
@package Drupal\backup_migrate\Core\Source

Namespace

Drupal\backup_migrate\Core\Source

Code

public function importFromFile(BackupFileReadableInterface $file) {
  if ($directory = $this
    ->confGet('directory')) {

    // Make sure the directory ends in exactly 1 slash:
    if (substr($directory, -1) !== '/') {
      $directory = $directory . '/';
    }
    if (!file_exists($directory)) {
      throw new BackupMigrateException('The directory %dir does not exist to restore to.', [
        '%dir' => $directory,
      ]);
    }
    if (!is_writable($directory)) {
      throw new BackupMigrateException('The directory %dir cannot be written to because of the operating system file permissions.', [
        '%dir' => $directory,
      ]);
    }
    if (!($reader = $this
      ->getArchiveReader())) {
      throw new BackupMigrateException('A file directory source requires an archive reader object.');
    }

    // Check that the file endings match.
    if ($reader
      ->getFileExt() !== $file
      ->getExtLast()) {
      throw new BackupMigrateException('This source expects a .%ext file.', [
        '%ext' => $reader
          ->getFileExt(),
      ]);
    }
    $reader
      ->setArchive($file);
    $reader
      ->extractTo($directory);
    $reader
      ->closeArchive();
    return TRUE;
  }
  return FALSE;
}