modifs.mineures sans commentaires

This commit is contained in:
Jephté Clain 2023-12-31 19:31:16 +04:00
parent bc115a3e6c
commit 6441d82e0d
2 changed files with 73 additions and 21 deletions

57
src/os/file.php Normal file
View File

@ -0,0 +1,57 @@
<?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();
}
}
}
}

View File

@ -18,8 +18,9 @@ class path {
* - retourner la valeur null inchangée
* - retourner '.' pour un chemin vide
*/
static final function normalize(?string $path): ?string {
if ($path === null) return null;
static final function normalize($path): ?string {
if ($path === null || $path === false) return null;
$path = strval($path);
if ($path !== "") {
if (substr($path, 0, 2) == "~/") {
$path = sh::homedir().substr($path, 1);
@ -53,15 +54,9 @@ class path {
return $path !== ""? $path: ".";
}
/** Comme normalize() mais retourner inchangée la valeur false */
static final function with($path) {
if ($path === null || $path === false) return $path;
else return self::normalize(strval($path));
}
/** obtenir le chemin absolu normalisé correspondant à $path */
static final function abspath($path, ?string $cwd=null) {
if ($path === null || $path === false) return $path;
static final function abspath($path, ?string $cwd=null): ?string {
if ($path === null || $path === false) return null;
$path = strval($path);
if (substr($path, 0, 1) !== "/") {
if ($cwd === null) $cwd = getcwd();
@ -125,12 +120,10 @@ class path {
* @throws IOException si le chemin n'existe pas ou si une autre erreur se
* produit
*/
static final function realpath($path) {
if ($path === null || $path === false) return $path;
static final function realpath($path): ?string {
if ($path === null || $path === false) return null;
$path = strval($path);
$realpath = realpath($path);
if ($realpath === false) throw IOException::last_error();
return $realpath;
return IOException::ensure_value(realpath($path));
}
/**
@ -141,13 +134,14 @@ class path {
* dirname("filename") === ".";
*/
static final function dirname($path): ?string {
if ($path === null || $path === false) return $path;
if ($path === null || $path === false) return null;
else return dirname(strval($path));
}
/** obtenir le nom du fichier sans son chemin */
static final function filename($path): ?string {
if ($path === null || $path === false) return $path;
if ($path === null || $path === false) return null;
$path = strval($path);
$index = strrpos($path, "/");
if ($index !== false) $path = substr($path, $index + 1);
return $path;
@ -155,7 +149,7 @@ class path {
/** obtenir le nom de base du fichier (sans son chemin et sans l'extension) */
static final function basename($path): ?string {
if ($path === null || $path === false) return $path;
if ($path === null || $path === false) return null;
$basename = self::filename($path);
$index = strrpos($basename, ".");
if ($index !== false) $basename = substr($basename, 0, $index);
@ -164,7 +158,7 @@ class path {
/** obtenir l'extension du fichier. l'extension est retournée avec le '.' */
static final function ext($path): ?string {
if ($path === null || $path === false) return $path;
if ($path === null || $path === false) return null;
$ext = self::filename($path);
$index = strrpos($ext, ".");
if ($index === false) $ext = "";
@ -174,8 +168,9 @@ class path {
/** découper le chemin entre sa partie "répertoire" et "fichier" */
static final function split($path): array {
if ($path === null || $path === false) return [$path, $path];
if ($path === null || $path === false) return [null, null];
elseif ($path === "") return ["", ""];
$path = strval($path);
$index = strrpos($path, "/");
if ($index !== false) {
if ($index == 0) $dir = "/";