modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2024-01-01 00:24:47 +04:00
parent 81f696beba
commit b0a677c6b6
7 changed files with 69 additions and 26 deletions

View File

@ -11,10 +11,10 @@ interface IWriter extends _IFile {
function fwrite(string $data, int $length=0): int;
/** @throws IOException */
function fflush(): void;
function fflush(): self;
/** @throws IOException */
function ftruncate(int $size): void;
function ftruncate(int $size): self;
/** afficher les lignes */
function writeLines(?iterable $lines): self;
@ -30,7 +30,7 @@ interface IWriter extends _IFile {
* verrouiller en mode exclusif puis retourner un objet permettant d'écrire
* dans le fichier
*/
function getWriter(bool $lockedByCanWrite=false): self;
function getWriter(bool $lockedByCanWrite=false): IWriter;
/** écrire le contenu spécifié dans le fichier */
function putContents(string $contents, bool $close=true, bool $lockedByCanWrite=false): void;

View File

@ -1,13 +1,23 @@
<?php
namespace nur\sery\os\file;
use nur\sery\os\IOException;
/**
* Class MemoryStream: un flux qui peut être lu ou écrit, et qui reste
* uniquement en mémoire.
*/
class MemoryStream extends Stream {
protected static function memory_fd() {
return fopen("php://memory", "w+b");
}
function __construct(bool $throwOnError=true) {
$fd = fopen("php://memory", "w+b");
parent::__construct($fd, true, $throwOnError);
parent::__construct(self::memory_fd(), true, $throwOnError);
}
function getResource() {
if ($this->fd === null) $this->fd = self::memory_fd();
return parent::getResource();
}
}

View File

@ -4,7 +4,7 @@ namespace nur\sery\os\file;
use nulib\ValueException;
class SharedFile extends FileWriter {
const ALLOW_LOCKING = true;
const USE_LOCKING = true;
const DEFAULT_MODE = "c+b";

View File

@ -14,7 +14,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
use TStreamFilter;
/** @var bool les opérations de verrouillages sont-elle activées? */
const ALLOW_LOCKING = false;
const USE_LOCKING = false;
/** @var resource */
protected $fd;
@ -26,7 +26,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
protected $throwOnError;
/** @var bool */
protected $allowLocking;
protected $useLocking;
/** @var int|null */
protected $serial;
@ -34,13 +34,13 @@ class Stream extends AbstractIterator implements IReader, IWriter {
/** @var array */
protected $stat;
function __construct($fd, bool $close=true, bool $throwOnError=true, ?bool $allowLocking=null) {
function __construct($fd, bool $close=true, bool $throwOnError=true, ?bool $useLocking=null) {
if ($fd === null) throw ValueException::null("resource");
$this->fd = $fd;
$this->close = $close;
$this->throwOnError = $throwOnError;
if ($allowLocking === null) $allowLocking = static::ALLOW_LOCKING;
$this->allowLocking = $allowLocking;
if ($useLocking === null) $useLocking = static::USE_LOCKING;
$this->useLocking = $useLocking;
}
#############################################################################
@ -83,6 +83,10 @@ class Stream extends AbstractIterator implements IReader, IWriter {
return $this->stat;
}
function getSize(): int {
return $this->fstat()["size"];
}
/** @throws IOException */
function ftell(): int {
$fd = $this->getResource();
@ -99,6 +103,11 @@ class Stream extends AbstractIterator implements IReader, IWriter {
return $this->ftell();
}
function seek(int $offset, int $whence=SEEK_SET): self {
$this->fseek($offset, $whence);
return $this;
}
/** fermer le fichier si c'est nécessaire */
function close(bool $close=true, ?int $ifSerial=null): void {
AbstractIterator::rewind();
@ -171,7 +180,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
* true
*/
function canRead(): bool {
if ($this->allowLocking) return $this->lock(LOCK_SH + LOCK_NB);
if ($this->useLocking) return $this->lock(LOCK_SH + LOCK_NB);
else return true;
}
@ -180,7 +189,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
* fichier.
*/
function getReader(bool $lockedByCanRead=false): IReader {
if ($this->allowLocking && !$lockedByCanRead) $this->lock(LOCK_SH);
if ($this->useLocking && !$lockedByCanRead) $this->lock(LOCK_SH);
return new class($this->fd, ++$this->serial, $this) extends Stream {
function __construct($fd, int $serial, Stream $parent) {
$this->parent = $parent;
@ -202,12 +211,12 @@ class Stream extends AbstractIterator implements IReader, IWriter {
/** retourner le contenu du fichier sous forme de chaine */
function getContents(bool $close=true, bool $lockedByCanRead=false): string {
$allowLocking = $this->allowLocking;
if ($allowLocking && !$lockedByCanRead) $this->lock(LOCK_SH);
$useLocking = $this->useLocking;
if ($useLocking && !$lockedByCanRead) $this->lock(LOCK_SH);
try {
return IOException::ensure_value(stream_get_contents($this->fd), $this->throwOnError);
} finally {
if ($allowLocking) $this->unlock($close);
if ($useLocking) $this->unlock($close);
elseif ($close) $this->close();
}
}
@ -244,15 +253,17 @@ class Stream extends AbstractIterator implements IReader, IWriter {
}
/** @throws IOException */
function fflush(): void {
function fflush(): self {
$fd = $this->getResource();
IOException::ensure_value(fflush($fd), $this->throwOnError);
return $this;
}
/** @throws IOException */
function ftruncate(int $size): void {
function ftruncate(int $size): self {
$fd = $this->getResource();
IOException::ensure_value(ftruncate($fd, $size), $this->throwOnError);
return $this;
}
function writeLines(?iterable $lines): IWriter {
@ -271,7 +282,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
* true
*/
function canWrite(): bool {
if ($this->allowLocking) return $this->lock(LOCK_EX + LOCK_NB);
if ($this->useLocking) return $this->lock(LOCK_EX + LOCK_NB);
else return true;
}
@ -280,7 +291,7 @@ class Stream extends AbstractIterator implements IReader, IWriter {
* dans le fichier
*/
function getWriter(bool $lockedByCanWrite=false): IWriter {
if ($this->allowLocking && !$lockedByCanWrite) $this->lock(LOCK_EX);
if ($this->useLocking && !$lockedByCanWrite) $this->lock(LOCK_EX);
return new class($this->fd, ++$this->serial, $this) extends Stream {
function __construct($fd, int $serial, Stream $parent) {
$this->parent = $parent;
@ -302,12 +313,12 @@ class Stream extends AbstractIterator implements IReader, IWriter {
}
function putContents(string $contents, bool $close=true, bool $lockedByCanWrite=false): void {
$allowLocking = $this->allowLocking;
if ($allowLocking && !$lockedByCanWrite) $this->lock(LOCK_EX);
$useLocking = $this->useLocking;
if ($useLocking && !$lockedByCanWrite) $this->lock(LOCK_EX);
try {
$this->fwrite($contents);
} finally {
if ($allowLocking) $this->unlock($close);
if ($useLocking) $this->unlock($close);
elseif ($close) $this->close();
}
}

View File

@ -11,7 +11,19 @@ class TempStream extends Stream {
function __construct(?int $maxMemory=null, bool $throwOnError=true) {
if ($maxMemory === null) $maxMemory = static::MAX_MEMORY;
$fd = fopen("php://temp/maxmemory:$maxMemory", "w+b");
parent::__construct($fd, true, $throwOnError);
$this->maxMemory = $maxMemory;
parent::__construct($this->tempFd(), true, $throwOnError);
}
/** @var int */
protected $maxMemory;
protected function tempFd() {
return fopen("php://temp/maxmemory:$this->maxMemory", "w+b");
}
function getResource() {
if ($this->fd === null) $this->fd = $this->tempFd();
return parent::getResource();
}
}

View File

@ -30,6 +30,9 @@ interface _IFile extends Iterator, ICloseable {
/** obtenir des informations sur le fichier */
function fstat(): array;
/** retourner la taille du fichier */
function getSize(): int;
/**
* retourner la position actuelle de lecture/écriture
*
@ -45,6 +48,9 @@ interface _IFile extends Iterator, ICloseable {
*/
function fseek(int $offset, int $whence=SEEK_SET): int;
/** comme {@link fseek()} mais retourne self */
function seek(int $offset, int $whence=SEEK_SET): self;
/** fermer le fichier si c'est nécessaire */
function close(bool $close=true): void;
}

View File

@ -300,12 +300,16 @@ class func {
return true;
}
/** @var Schema */
private static $call_all_params_schema;
/**
* retourner la liste des méthodes de $class_or_object qui correspondent au
* filtre $options. le filtre doit respecter le schéme {@link CALL_ALL_PARAMS_SCHEMA}
*/
static function get_all($class_or_object, $params=null): array {
Schema::nv($paramsv, $params, null, $schema, ref_func::CALL_ALL_PARAMS_SCHEMA);
Schema::nv($paramsv, $params, null
, self::$call_all_params_schema, ref_func::CALL_ALL_PARAMS_SCHEMA);
if (is_callable($class_or_object, true) && is_array($class_or_object)) {
# callable sous forme de tableau
$class_or_object = $class_or_object[0];