nulib/php/src_file/base/FileWriter.php

32 lines
795 B
PHP
Raw Normal View History

2024-04-25 17:46:18 +04:00
<?php
namespace nulib\file\base;
use nulib\os\IOException;
use nulib\os\sh;
/**
* Class FileWriter: un fichier accédé en lecture/écriture
*/
class FileWriter extends _File {
const DEFAULT_MODE = "a+b";
function __construct($output, ?string $mode=null, bool $throwOnError=true, ?bool $allowLocking=null) {
if ($output === null) {
$fd = STDOUT;
$close = false;
} elseif (is_resource($output)) {
$fd = $output;
$close = false;
} else {
$file = $output;
if ($mode === null) $mode = static::DEFAULT_MODE;
IOException::ensure_valid(sh::mkdirof($file));
$this->file = $file;
$this->mode = $mode;
$fd = $this->open();
$close = true;
}
parent::__construct($fd, $close, $throwOnError, $allowLocking);
}
}