You are here

class WritableStreamBackupFile in Backup and Migrate 8.4

Class TempFile.

@package BackupMigrate\Core\File

A file object which represents an existing PHP stream that can be written to and read from.

Hierarchy

Expanded class hierarchy of WritableStreamBackupFile

File

lib/backup_migrate_core/src/File/WritableStreamBackupFile.php, line 16

Namespace

BackupMigrate\Core\File
View source
class WritableStreamBackupFile extends ReadableStreamBackupFile implements BackupFileReadableInterface, BackupFileWritableInterface {

  /**
   * @var bool Dirty bit. Has the file been written to since it was opened?
   */
  protected $dirty = FALSE;

  /**
   * Constructor. Create a new file object from .
   */
  function __construct($filepath) {
    parent::__construct($filepath);
  }

  /**
   * Open a file for reading or writing.
   *
   * @param bool $binary Is the file binary
   *
   * @throws \Exception
   */
  function openForWrite($binary = FALSE) {
    if (!$this
      ->isOpen()) {
      $path = $this
        ->realpath();

      // Check if the file can be read/written.
      if (file_exists($path) && !is_writable($path) || !file_exists($path) && !is_writable(dirname($path))) {

        // @TODO: Throw better exception
        throw new BackupMigrateException('Cannot write to file: %path', [
          '%path' => $path,
        ]);
      }

      // Open the file.
      $mode = "w" . ($binary ? "b" : "");
      $this->handle = fopen($path, $mode);
      if (!$this->handle) {
        throw new BackupMigrateException('Cannot open file: %path', [
          '%path' => $path,
        ]);
      }
    }
  }

  /**
   * Write a line to the file.
   *
   * @param string $data A string to write to the file.
   *
   * @throws \Exception
   */
  function write($data) {
    if (!$this
      ->isOpen()) {
      $this
        ->openForWrite();
    }
    if ($this->handle) {
      if (fwrite($this->handle, $data) === FALSE) {
        throw new \Exception('Cannot write to file: ' . $this
          ->realpath());
      }
      else {
        $this->dirty = TRUE;
      }
    }
    else {
      throw new \Exception('File not open for writing.');
    }
  }

  /**
   * Update the file time and size when the file is closed.
   */
  function close() {
    parent::close();

    // If the file has been modified, update the stats from disk.
    if ($this->dirty) {
      $this
        ->_loadFileStats();
      $this->dirty = FALSE;
    }
  }

  /**
   * A shorthand function to open the file, write the given contents and close
   * the file. Used for small amounts of data that can fit in memory.
   *
   * @param $data
   */
  public function writeAll($data) {
    $this
      ->openForWrite();
    $this
      ->write($data);
    $this
      ->close();
  }

}

Members

Namesort descending Modifiers Type Description Overrides
BackupFile::$ext protected property The file extension(s).
BackupFile::$file_info protected property The file info (size, timestamp, etc.).
BackupFile::$metadata protected property The file's metadata.
BackupFile::$name protected property The file name without extension.
BackupFile::$path protected property The file path.
BackupFile::getExt public function Get the full file extension. Overrides BackupFileInterface::getExt
BackupFile::getExtLast public function Get the last file extension. Overrides BackupFileInterface::getExtLast
BackupFile::getExtList public function Get an array of file extensions. Overrides BackupFileInterface::getExtList
BackupFile::getFullName public function Get the full filename with extensions. Overrides BackupFileInterface::getFullName
BackupFile::getMeta public function Get a metadata value. Overrides BackupFileInterface::getMeta
BackupFile::getMetaAll public function Get all metadata. Overrides BackupFileInterface::getMetaAll
BackupFile::getName public function Get the file name without extension. Overrides BackupFileInterface::getName
BackupFile::setExtList public function Overrides BackupFileInterface::setExtList
BackupFile::setFullName public function Set the full filename with extensions. Overrides BackupFileInterface::setFullName
BackupFile::setMeta public function Set a metadata value. Overrides BackupFileInterface::setMeta
BackupFile::setMetaMultiple public function Set a metadata value. Overrides BackupFileInterface::setMetaMultiple
BackupFile::setName public function Set the file name without extension. Overrides BackupFileInterface::setName
ReadableStreamBackupFile::$handle protected property A file handle if it is open.
ReadableStreamBackupFile::isOpen function Is this file open for reading/writing.
ReadableStreamBackupFile::openForRead function Open a file for reading or writing. Overrides BackupFileReadableInterface::openForRead
ReadableStreamBackupFile::readAll public function Read a line from the file. Overrides BackupFileReadableInterface::readAll
ReadableStreamBackupFile::readBytes function Read a line from the file. Overrides BackupFileReadableInterface::readBytes
ReadableStreamBackupFile::readLine public function Read a single line from the file. Overrides BackupFileReadableInterface::readLine
ReadableStreamBackupFile::realpath function Get the realpath of the file. Overrides BackupFileReadableInterface::realpath
ReadableStreamBackupFile::rewind function Rewind the file handle to the start of the file. Overrides BackupFileReadableInterface::rewind
ReadableStreamBackupFile::seekBytes public function Move the file pointer forward a given number of bytes. Overrides BackupFileReadableInterface::seekBytes
ReadableStreamBackupFile::_loadFileStats protected function Get info about the file and load them as metadata.
ReadableStreamBackupFile::__destruct function Destructor.
WritableStreamBackupFile::$dirty protected property
WritableStreamBackupFile::close function Update the file time and size when the file is closed. Overrides ReadableStreamBackupFile::close
WritableStreamBackupFile::openForWrite function Open a file for reading or writing. Overrides BackupFileWritableInterface::openForWrite
WritableStreamBackupFile::write function Write a line to the file. Overrides BackupFileWritableInterface::write
WritableStreamBackupFile::writeAll public function A shorthand function to open the file, write the given contents and close the file. Used for small amounts of data that can fit in memory. Overrides BackupFileWritableInterface::writeAll
WritableStreamBackupFile::__construct function Constructor. Create a new file object from . Overrides ReadableStreamBackupFile::__construct