class WritableStreamBackupFile in Backup and Migrate 5.0.x
A file object which represents an existing PHP stream with read/write.
@package Drupal\backup_migrate\Core\File
Hierarchy
- class \Drupal\backup_migrate\Core\File\BackupFile implements BackupFileInterface
- class \Drupal\backup_migrate\Core\File\ReadableStreamBackupFile implements BackupFileReadableInterface
- class \Drupal\backup_migrate\Core\File\WritableStreamBackupFile implements BackupFileReadableInterface, BackupFileWritableInterface
- class \Drupal\backup_migrate\Core\File\ReadableStreamBackupFile implements BackupFileReadableInterface
Expanded class hierarchy of WritableStreamBackupFile
File
- src/
Core/ File/ WritableStreamBackupFile.php, line 12
Namespace
Drupal\backup_migrate\Core\FileView source
class WritableStreamBackupFile extends ReadableStreamBackupFile implements BackupFileReadableInterface, BackupFileWritableInterface {
/**
* Dirty bit - has the file been written to since it was opened?
*
* @var bool
*/
protected $dirty = FALSE;
/**
* Open a file for reading or writing.
*
* @param bool $binary
* Is the file binary.
*
* @throws \Exception
*/
public 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
*/
public 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.
*/
public function close() {
parent::close();
// If the file has been modified, update the stats from disk.
if ($this->dirty) {
$this
->loadFileStats();
$this->dirty = FALSE;
}
}
/**
* Open the file, writes the given contents and closes it.
*
* 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
BackupFile:: |
protected | property | The file extension(s). | |
BackupFile:: |
protected | property | The file info (size, timestamp, etc.). | |
BackupFile:: |
protected | property | The file's metadata. | |
BackupFile:: |
protected | property | The file name without extension. | |
BackupFile:: |
protected | property | The file path. | |
BackupFile:: |
public | function |
Get the full file extension. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Get the last file extension. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Get an array of file extensions. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Get the full filename with extensions. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Get a metadata value. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Get all metadata. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Get the file name without extension. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Set the full filename with extensions. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Set a metadata value. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Set a metadata value. Overrides BackupFileInterface:: |
|
BackupFile:: |
public | function |
Set the file name without extension. Overrides BackupFileInterface:: |
|
ReadableStreamBackupFile:: |
protected | property | A file handle if it is open. | |
ReadableStreamBackupFile:: |
public | function | Is this file open for reading/writing. | |
ReadableStreamBackupFile:: |
protected | function | Get info about the file and load them as metadata. | |
ReadableStreamBackupFile:: |
public | function |
Open a file for reading or writing. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function |
Read a line from the file. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function |
Read a line from the file. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function |
Read a single line from the file. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function |
Get the realpath of the file. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function |
Rewind the file handle to the start of the file. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function |
Move the file pointer forward a given number of bytes. Overrides BackupFileReadableInterface:: |
|
ReadableStreamBackupFile:: |
public | function | Constructor. | |
ReadableStreamBackupFile:: |
public | function | Destructor. | |
WritableStreamBackupFile:: |
protected | property | Dirty bit - has the file been written to since it was opened? | |
WritableStreamBackupFile:: |
public | function |
Update the file time and size when the file is closed. Overrides ReadableStreamBackupFile:: |
|
WritableStreamBackupFile:: |
public | function |
Open a file for reading or writing. Overrides BackupFileWritableInterface:: |
|
WritableStreamBackupFile:: |
public | function |
Write a line to the file. Overrides BackupFileWritableInterface:: |
|
WritableStreamBackupFile:: |
public | function |
Open the file, writes the given contents and closes it. Overrides BackupFileWritableInterface:: |