You are here

class ReadableStreamBackupFile in Backup and Migrate 8.4

Class ReadableStreamBackupFile.

@package BackupMigrate\Core\File

An implementation of the BackupFileReadableInterface which uses a readable php stream such as a local file.

Hierarchy

Expanded class hierarchy of ReadableStreamBackupFile

5 files declare their use of ReadableStreamBackupFile
DirectoryDestination.php in lib/backup_migrate_core/src/Destination/DirectoryDestination.php
DrupalBrowserUploadDestination.php in src/Destination/DrupalBrowserUploadDestination.php
DrupalDirectoryDestination.php in src/Destination/DrupalDirectoryDestination.php
HTTPClientInterface.php in lib/backup_migrate_core/src/Service/HTTPClientInterface.php
PhpCurlHttpClient.php in lib/backup_migrate_core/src/Service/PhpCurlHttpClient.php

File

lib/backup_migrate_core/src/File/ReadableStreamBackupFile.php, line 13

Namespace

BackupMigrate\Core\File
View source
class ReadableStreamBackupFile extends BackupFile implements BackupFileReadableInterface {

  /**
   * A file handle if it is open.
   *
   * @var resource
   */
  protected $handle;

  /**
   * Constructor.
   *
   * @param string $filepath string The path to a file (which must already exist). Can be a stream URI.
   *
   * @throws \Exception
   */
  function __construct($filepath) {

    // Check that the file exists and is readable.
    if (!file_exists($filepath)) {
      throw new \Exception("The file '{$filepath}' does not exists");
    }
    if (!is_readable($filepath)) {
      throw new \Exception("The file '{$filepath}' cannot be read");
    }
    $this->path = $filepath;

    // Get the basename and extensions.
    $this
      ->setFullName(basename($filepath));

    // Get the basic file stats since this is probably a read-only file option and these won't change.
    $this
      ->_loadFileStats();
  }

  /**
   * Destructor.
   */
  function __destruct() {

    // Close the handle if we've opened it.
    $this
      ->close();
  }

  /**
   * Get the realpath of the file.
   *
   * @return string The path or stream URI to the file or NULL if the file does not exist.
   */
  function realpath() {
    if (file_exists($this->path)) {
      return $this->path;
    }
    return NULL;
  }

  /**
   * Open a file for reading or writing.
   *
   * @param bool $binary If true open as a binary file
   *
   * @return resource
   *
   * @throws \Exception
   */
  function openForRead($binary = FALSE) {
    if (!$this
      ->isOpen()) {
      $path = $this
        ->realpath();
      if (!is_readable($path)) {

        // @TODO: Throw better exception
        throw new \Exception('Cannot read file.');
      }

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

    // If the file is already open, rewind it.
    $this
      ->rewind();
    return $this->handle;
  }

  /**
   * Close a file when we're done reading/writing.
   */
  function close() {
    if ($this
      ->isOpen()) {
      fclose($this->handle);
      $this->handle = NULL;
    }
  }

  /**
   * Is this file open for reading/writing.
   *
   * Return bool True if the file is open, false if not.
   */
  function isOpen() {
    return !empty($this->handle) && get_resource_type($this->handle) == 'stream';
  }

  /**
   * Read a line from the file.
   *
   * @param int $size The number of bites to read or 0 to read the whole file
   *
   * @return string The data read from the file or NULL if the file can't be read or is at the end of the file.
   */
  function readBytes($size = 1024, $binary = FALSE) {
    if (!$this
      ->isOpen()) {
      $this
        ->openForRead($binary);
    }
    if ($this->handle && !feof($this->handle)) {
      return fread($this->handle, $size);
    }
    return NULL;
  }

  /**
   * Read a single line from the file.
   *
   * @return string The data read from the file or NULL if the file can't be read or is at the end of the file.
   */
  public function readLine() {
    if (!$this
      ->isOpen()) {
      $this
        ->openForRead();
    }
    return fgets($this->handle);
  }

  /**
   * Read a line from the file.
   *
   * @return string The data read from the file or NULL if the file can't be read.
   */
  public function readAll() {
    if (!$this
      ->isOpen()) {
      $this
        ->openForRead();
    }
    $this
      ->rewind();
    return stream_get_contents($this->handle);
  }

  /**
   * Move the file pointer forward a given number of bytes.
   *
   * @param int $bytes
   *
   * @return int
   *  The number of bytes moved or -1 if the operation failed.
   */
  public function seekBytes($bytes) {
    if ($this
      ->isOpen()) {
      return fseek($this->handle, $bytes);
    }
    return -1;
  }

  /**
   * Rewind the file handle to the start of the file.
   */
  function rewind() {
    if ($this
      ->isOpen()) {
      rewind($this->handle);
    }
  }

  /**
   * Get info about the file and load them as metadata.
   */
  protected function _loadFileStats() {
    clearstatcache();
    $this
      ->setMeta('filesize', filesize($this
      ->realpath()));
    $this
      ->setMeta('datestamp', filectime($this
      ->realpath()));
  }

}

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::close function Close a file when we're done reading/writing. Overrides BackupFileReadableInterface::close 1
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::__construct function Constructor. 1
ReadableStreamBackupFile::__destruct function Destructor.