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;
|
2023-12-31 22:32:39 +04:00
|
|
|
use nur\sery\os\file\MemoryStream;
|
2023-12-31 19:31:16 +04:00
|
|
|
use nur\sery\os\file\SharedFile;
|
2023-12-31 22:32:39 +04:00
|
|
|
use nur\sery\os\file\TempStream;
|
2023-12-31 19:31:16 +04:00
|
|
|
use nur\sery\os\file\TmpfileWriter;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class file: outils pour gérer les fichiers
|
|
|
|
*/
|
|
|
|
class file {
|
|
|
|
static function reader($input, ?callable $func=null): FileReader {
|
2023-12-31 22:32:39 +04:00
|
|
|
$file = new FileReader($input);
|
2023-12-31 19:31:16 +04:00
|
|
|
if ($func !== null) {
|
|
|
|
try {
|
2023-12-31 22:32:39 +04:00
|
|
|
$func($file);
|
2023-12-31 19:31:16 +04:00
|
|
|
} finally {
|
2023-12-31 22:32:39 +04:00
|
|
|
$file->close();
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
}
|
2023-12-31 22:32:39 +04:00
|
|
|
return $file;
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
|
2024-01-01 18:14:49 +04:00
|
|
|
static function writer($output, ?string $mode=null, ?callable $func=null): FileWriter {
|
|
|
|
$file = new FileWriter($output, $mode);
|
2023-12-31 19:31:16 +04:00
|
|
|
if ($func !== null) {
|
|
|
|
try {
|
2023-12-31 22:32:39 +04:00
|
|
|
$func($file);
|
2023-12-31 19:31:16 +04:00
|
|
|
} finally {
|
2023-12-31 22:32:39 +04:00
|
|
|
$file->close();
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
}
|
2023-12-31 22:32:39 +04:00
|
|
|
return $file;
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
2023-12-31 22:32:39 +04:00
|
|
|
|
|
|
|
static function shared($file, ?callable $func=null): SharedFile {
|
|
|
|
$file = new SharedFile($file);
|
|
|
|
if ($func !== null) {
|
|
|
|
try {
|
|
|
|
$func($file);
|
|
|
|
} finally {
|
|
|
|
$file ->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
|
|
|
|
2023-12-31 19:31:16 +04:00
|
|
|
static function tmpwriter($destdir=null, ?callable $func=null): TmpfileWriter {
|
2023-12-31 22:32:39 +04:00
|
|
|
$file = new TmpfileWriter($destdir);
|
2023-12-31 19:31:16 +04:00
|
|
|
if ($func !== null) {
|
|
|
|
try {
|
2023-12-31 22:32:39 +04:00
|
|
|
$func($file);
|
2023-12-31 19:31:16 +04:00
|
|
|
} finally {
|
2023-12-31 22:32:39 +04:00
|
|
|
$file->close();
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
}
|
2023-12-31 22:32:39 +04:00
|
|
|
return $file;
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
|
2023-12-31 22:32:39 +04:00
|
|
|
static function memory(?callable $func=null): MemoryStream {
|
|
|
|
$file = new MemoryStream();
|
2023-12-31 19:31:16 +04:00
|
|
|
if ($func !== null) {
|
|
|
|
try {
|
2023-12-31 22:32:39 +04:00
|
|
|
$func($file);
|
2023-12-31 19:31:16 +04:00
|
|
|
} finally {
|
2023-12-31 22:32:39 +04:00
|
|
|
$file->close();
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
}
|
2023-12-31 22:32:39 +04:00
|
|
|
return $file;
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|
|
|
|
|
2023-12-31 22:32:39 +04:00
|
|
|
static function temp(?callable $func=null): TempStream {
|
|
|
|
$file = new TempStream();
|
|
|
|
if ($func !== null) {
|
|
|
|
try {
|
|
|
|
$func($file);
|
|
|
|
} finally {
|
|
|
|
$file->close();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $file;
|
|
|
|
}
|
2023-12-31 19:31:16 +04:00
|
|
|
}
|