You are here

class DrupalTempFileAdapter in Backup and Migrate 8.4

Class DrupalTempFileAdapter.

@package BackupMigrate\Drupal\File

Hierarchy

Expanded class hierarchy of DrupalTempFileAdapter

File

src/File/DrupalTempFileAdapter.php, line 14

Namespace

BackupMigrate\Drupal\File
View source
class DrupalTempFileAdapter extends TempFileAdapter implements TempFileAdapterInterface {

  /**
   * The Drupal file system for provisioning temp files.
   *
   * @var \Drupal\Core\File\FileSystemInterface
   */
  protected $filesystem;

  /**
   * Construct a manager.
   *
   * @param \Drupal\Core\File\FileSystem $filesystem
   *   A file path or stream URL for the temp directory.
   * @param string $dir
   *   The directory to save to.
   * @param string $prefix
   *   A string prefix to add to each created file.
   */
  public function __construct(FileSystemInterface $filesystem, $dir = 'temporary://', $prefix = 'bam') {

    // Set the prefix and initialize the temp file tracking.
    parent::__construct($dir, $prefix);
    $this->filesystem = $filesystem;
  }

  /**
   * {@inheritdoc}
   */
  public function createTempFile($ext = '') {

    // Add a dot to the file extension.
    $ext = $ext ? '.' . $ext : '';
    $file = $this->filesystem
      ->tempnam($this->dir, $this->prefix);
    if (!$file) {
      throw new \Exception('Could not create a temporary file to write to.');
    }
    $this->tempfiles[] = $file;
    return $file;
  }

  /**
   * {@inheritdoc}
   */
  public function deleteTempFile($filename) {

    // Only delete files that were created by this manager.
    if (in_array($filename, $this->tempfiles)) {
      if (file_exists($filename)) {
        if (!$this->filesystem
          ->unlink($filename)) {
          throw new \Exception('Could not delete a temporary file.');
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DrupalTempFileAdapter::$filesystem protected property The Drupal file system for provisioning temp files.
DrupalTempFileAdapter::createTempFile public function Get a temporary file that can be written to. Overrides TempFileAdapter::createTempFile
DrupalTempFileAdapter::deleteTempFile public function Delete a temporary file. Overrides TempFileAdapter::deleteTempFile
DrupalTempFileAdapter::__construct public function Construct a manager. Overrides TempFileAdapter::__construct
TempFileAdapter::$dir protected property The path to the temp directory.
TempFileAdapter::$prefix protected property A prefix to add to all temp files.
TempFileAdapter::$tempfiles protected property The list of files created by this manager.
TempFileAdapter::deleteAllTempFiles public function Delete all temp files which have been created. Overrides TempFileAdapterInterface::deleteAllTempFiles
TempFileAdapter::__destruct public function Destruct the manager. Delete all the temporary files when this manager is destroyed.