WritableStreamBackupFile.php in Backup and Migrate 5.0.x
File
src/Core/File/WritableStreamBackupFile.php
View source
<?php
namespace Drupal\backup_migrate\Core\File;
use Drupal\backup_migrate\Core\Exception\BackupMigrateException;
class WritableStreamBackupFile extends ReadableStreamBackupFile implements BackupFileReadableInterface, BackupFileWritableInterface {
protected $dirty = FALSE;
public function openForWrite($binary = FALSE) {
if (!$this
->isOpen()) {
$path = $this
->realpath();
if (file_exists($path) && !is_writable($path) || !file_exists($path) && !is_writable(dirname($path))) {
throw new BackupMigrateException('Cannot write to file: %path', [
'%path' => $path,
]);
}
$mode = "w" . ($binary ? "b" : "");
$this->handle = fopen($path, $mode);
if (!$this->handle) {
throw new BackupMigrateException('Cannot open file: %path', [
'%path' => $path,
]);
}
}
}
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.');
}
}
public function close() {
parent::close();
if ($this->dirty) {
$this
->loadFileStats();
$this->dirty = FALSE;
}
}
public function writeAll($data) {
$this
->openForWrite();
$this
->write($data);
$this
->close();
}
}