nur-sery/src/os/file.php

58 lines
1.2 KiB
PHP
Raw Normal View History

2023-12-31 19:31:16 +04:00
<?php
namespace nur\sery\os;
use nur\sery\os\file\FileReader;
use nur\sery\os\file\FileWriter;
use nur\sery\os\file\SharedFile;
use nur\sery\os\file\TmpfileWriter;
/**
* Class file: outils pour gérer les fichiers
*/
class file {
static function reader($input, ?callable $func=null): FileReader {
$reader = new FileReader($input);
if ($func !== null) {
try {
$func($reader);
} finally {
$reader->close();
}
}
}
static function writer($output, ?callable $func=null): FileWriter {
$writer = new FileWriter($output);
if ($func !== null) {
try {
$func($writer);
} finally {
$writer->close();
}
}
}
static function tmpwriter($destdir=null, ?callable $func=null): TmpfileWriter {
$tmpwriter = new TmpfileWriter($destdir);
if ($func !== null) {
try {
$func($tmpwriter);
} finally {
$tmpwriter->close();
}
}
}
static function shared($file, ?callable $func=null): SharedFile {
$shared = new SharedFile($file);
if ($func !== null) {
try {
$func($shared);
} finally {
$shared ->close();
}
}
}
}