You are here

class TempFileManager in Backup and Migrate 8.4

Class TempFileManager.

@package BackupMigrate\Core\Services

Hierarchy

Expanded class hierarchy of TempFileManager

1 string reference to 'TempFileManager'
backup_migrate_backup_migrate_service_object_alter in ./backup_migrate.module
Implements hook_backup_migrate_service_object_alter().

File

lib/backup_migrate_core/src/File/TempFileManager.php, line 10

Namespace

BackupMigrate\Core\File
View source
class TempFileManager implements TempFileManagerInterface {

  /**
   * @var \BackupMigrate\Core\File\TempFileAdapterInterface
   */
  protected $adapter;

  /**
   * Build the manager with the given adapter. This manager needs the adapter
   * to create the actual temp files.
   *
   * @param \BackupMigrate\Core\File\TempFileAdapterInterface $adapter
   */
  public function __construct(TempFileAdapterInterface $adapter) {
    $this->adapter = $adapter;
  }

  /**
   * Create a brand new temp file with the given extension (if specified). The
   * new file should be writable.
   *
   * @param string $ext The file extension for this file (optional)
   *
   * @return BackupFileWritableInterface
   */
  public function create($ext = '') {
    $file = new WritableStreamBackupFile($this->adapter
      ->createTempFile($ext));
    $file
      ->setExtList(explode('.', $ext));
    return $file;
  }

  /**
   * Return a new file based on the passed in file with the given file extension.
   * This should maintain the metadata of the file passed in with the new file
   * extension added after the old one.
   * For example: xxx.mysql would become xxx.mysql.gz.
   *
   * @param \BackupMigrate\Core\File\BackupFileInterface $file
   *        The file to add the extension to.
   * @param $ext
   *        The new file extension.
   *
   * @return \BackupMigrate\Core\File\BackupFileWritableInterface
   *        A new writable backup file with the new extension and all of the metadata
   *        from the previous file.
   */
  public function pushExt(BackupFileInterface $file, $ext) {

    // Push the new extension on to the new file.
    $parts = $file
      ->getExtList();
    array_push($parts, $ext);
    $new_ext = implode('.', $parts);

    // Copy the file metadata to a new TempFile.
    $out = new WritableStreamBackupFile($this->adapter
      ->createTempFile($new_ext));

    // Copy the file metadata to a new TempFile.
    $out
      ->setMetaMultiple($file
      ->getMetaAll());
    $out
      ->setName($file
      ->getName());
    $out
      ->setExtList($parts);
    return $out;
  }

  /**
   * Return a new file based on the one passed in but with the last part of the
   * file extension removed.
   * For example: xxx.mysql.gz would become xxx.mysql.
   *
   * @param \BackupMigrate\Core\File\BackupFileInterface $file
   *
   * @return \BackupMigrate\Core\File\BackupFileWritableInterface
   *        A new writable backup file with the last extension removed and
   *        all of the metadata from the previous file.
   */
  public function popExt(BackupFileInterface $file) {

    // Pop the last extension from the last of the file.
    $parts = $file
      ->getExtList();
    array_pop($parts);
    $new_ext = implode('.', $parts);

    // Create a new temp file with the new extension.
    $out = new WritableStreamBackupFile($this->adapter
      ->createTempFile($new_ext));

    // Copy the file metadata to a new TempFile.
    $out
      ->setMetaMultiple($file
      ->getMetaAll());
    $out
      ->setName($file
      ->getName());
    $out
      ->setExtList($parts);
    return $out;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
TempFileManager::$adapter protected property
TempFileManager::create public function Create a brand new temp file with the given extension (if specified). The new file should be writable. Overrides TempFileManagerInterface::create
TempFileManager::popExt public function Return a new file based on the one passed in but with the last part of the file extension removed. For example: xxx.mysql.gz would become xxx.mysql. Overrides TempFileManagerInterface::popExt
TempFileManager::pushExt public function Return a new file based on the passed in file with the given file extension. This should maintain the metadata of the file passed in with the new file extension added after the old one. For example: xxx.mysql would become xxx.mysql.gz. Overrides TempFileManagerInterface::pushExt
TempFileManager::__construct public function Build the manager with the given adapter. This manager needs the adapter to create the actual temp files.