nur-sery/src/file.php

87 lines
1.7 KiB
PHP
Raw Normal View History

2023-12-31 19:31:16 +04:00
<?php
2024-04-25 17:46:14 +04:00
namespace nur\sery;
2023-12-31 19:31:16 +04:00
2024-05-22 20:44:20 +04:00
use nur\sery\file\FileReader;
use nur\sery\file\FileWriter;
use nur\sery\file\MemoryStream;
use nur\sery\file\SharedFile;
use nur\sery\file\TempStream;
use nur\sery\file\TmpfileWriter;
2023-12-31 19:31:16 +04:00
/**
* 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
}