ajout file::try_ext
This commit is contained in:
parent
341a9ba05a
commit
be6b7371e5
@ -7,6 +7,7 @@ use nulib\file\MemoryStream;
|
||||
use nulib\file\SharedFile;
|
||||
use nulib\file\TempStream;
|
||||
use nulib\file\TmpfileWriter;
|
||||
use nulib\os\path;
|
||||
|
||||
/**
|
||||
* Class file: outils pour gérer les fichiers
|
||||
@ -17,6 +18,30 @@ class file {
|
||||
return $file;
|
||||
}
|
||||
|
||||
/**
|
||||
* si le fichier $file existe, le retourner.
|
||||
* sinon, essayer avec l'une des extensions spécifiées, jusqu'à ce qu'un
|
||||
* fichier soit trouvé.
|
||||
* si aucun fichier n'est trouvé, retourner null
|
||||
*
|
||||
* par exemple, si le fichier "file.yaml" n'existe pas et que le fichier
|
||||
* "file.yml" existe, alors <code>file::try("file.yaml", ".yml")</code>
|
||||
* retourne "file.yml"
|
||||
*
|
||||
* @param string|array $exts
|
||||
* @param string|array $replace_ext
|
||||
*/
|
||||
static function try_ext(string $file, $exts=null, $replace_ext=null): ?string {
|
||||
if (file_exists($file)) return $file;
|
||||
if ($exts !== null) {
|
||||
foreach (cl::with($exts) as $ext) {
|
||||
$tmpfile = path::ensure_ext($file, $ext, $replace_ext);
|
||||
if (file_exists($tmpfile)) return $tmpfile;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
static function reader($input, ?callable $func=null): FileReader {
|
||||
$file = new FileReader(self::fix_dash($input));
|
||||
if ($func !== null) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user