diff --git a/src/os/file.php b/src/os/file.php new file mode 100644 index 0000000..e42749b --- /dev/null +++ b/src/os/file.php @@ -0,0 +1,57 @@ +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(); + } + } + } + +} diff --git a/src/os/path.php b/src/os/path.php index a15b47a..0a11003 100644 --- a/src/os/path.php +++ b/src/os/path.php @@ -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,14 +120,12 @@ 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)); } - + /** * obtenir le chemin parent de $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 = "/";