modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-31 22:32:39 +04:00
parent b9dab7a67b
commit 07df8b95c4
1 changed files with 49 additions and 20 deletions

View File

@ -3,7 +3,9 @@ namespace nur\sery\os;
use nur\sery\os\file\FileReader; use nur\sery\os\file\FileReader;
use nur\sery\os\file\FileWriter; use nur\sery\os\file\FileWriter;
use nur\sery\os\file\MemoryStream;
use nur\sery\os\file\SharedFile; use nur\sery\os\file\SharedFile;
use nur\sery\os\file\TempStream;
use nur\sery\os\file\TmpfileWriter; use nur\sery\os\file\TmpfileWriter;
/** /**
@ -11,47 +13,74 @@ use nur\sery\os\file\TmpfileWriter;
*/ */
class file { class file {
static function reader($input, ?callable $func=null): FileReader { static function reader($input, ?callable $func=null): FileReader {
$reader = new FileReader($input); $file = new FileReader($input);
if ($func !== null) { if ($func !== null) {
try { try {
$func($reader); $func($file);
} finally { } finally {
$reader->close(); $file->close();
} }
} }
return $file;
} }
static function writer($output, ?callable $func=null): FileWriter { static function writer($output, ?callable $func=null): FileWriter {
$writer = new FileWriter($output); $file = new FileWriter($output);
if ($func !== null) { if ($func !== null) {
try { try {
$func($writer); $func($file);
} finally { } finally {
$writer->close(); $file->close();
}
}
}
static function tmpwriter($destdir=null, ?callable $func=null): TmpfileWriter {
$tmpwriter = new TmpfileWriter($destdir);
if ($func !== null) {
try {
$func($tmpwriter);
} finally {
$tmpwriter->close();
} }
} }
return $file;
} }
static function shared($file, ?callable $func=null): SharedFile { static function shared($file, ?callable $func=null): SharedFile {
$shared = new SharedFile($file); $file = new SharedFile($file);
if ($func !== null) { if ($func !== null) {
try { try {
$func($shared); $func($file);
} finally { } finally {
$shared ->close(); $file ->close();
} }
} }
return $file;
} }
static function tmpwriter($destdir=null, ?callable $func=null): TmpfileWriter {
$file = new TmpfileWriter($destdir);
if ($func !== null) {
try {
$func($file);
} finally {
$file->close();
}
}
return $file;
}
static function memory(?callable $func=null): MemoryStream {
$file = new MemoryStream();
if ($func !== null) {
try {
$func($file);
} finally {
$file->close();
}
}
return $file;
}
static function temp(?callable $func=null): TempStream {
$file = new TempStream();
if ($func !== null) {
try {
$func($file);
} finally {
$file->close();
}
}
return $file;
}
} }