nur-sery/src/file/TempStream.php

29 lines
813 B
PHP

<?php
namespace nur\sery\file;
/**
* Class TempStream: un flux qui peut être lu ou écrit, et qui reste en mémoire,
* jusqu'à ce que la taille des données atteint {@link self::MAX_MEMORY} et à
* ce moment-là un fichier temporaire est automatiquement créé.
*/
class TempStream extends Stream {
const MAX_MEMORY = 2 * 1024 * 1024;
function __construct(?int $maxMemory=null, ?bool $throwOnError=null) {
$this->maxMemory = $maxMemory ?? static::MAX_MEMORY;
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();
}
}