outils pour lister les fichiers

This commit is contained in:
Jephté Clain 2024-11-14 18:02:27 +04:00
parent ab744eb1a5
commit b5b1b83d46
1 changed files with 48 additions and 0 deletions

View File

@ -1,6 +1,7 @@
<?php <?php
namespace nur\sery\os; namespace nur\sery\os;
use nur\path;
use nur\sery\cl; use nur\sery\cl;
use nur\sery\ExitError; use nur\sery\ExitError;
use nur\sery\StateException; use nur\sery\StateException;
@ -313,4 +314,51 @@ class sh {
if (!is_link($f) || !is_link($g)) return false; if (!is_link($f) || !is_link($g)) return false;
return @readlink($f) === @readlink($g); return @readlink($f) === @readlink($g);
} }
#############################################################################
static function ls_all(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
$all = scandir($dir, $sorting_order);
if ($all === false) return [];
return array_values(array_filter($all,
function ($file) use ($pattern) {
if ($file === "." || $file === "..") return false;
return $pattern === null || fnmatch($pattern, $file);
}
));
}
static function ls_dirs(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
return array_values(array_filter(self::ls_all($dir, $pattern, $sorting_order),
function ($file) use ($dir) {
return path::is_dir(path::join($dir, $file));
}
));
}
static function ls_files(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
return array_values(array_filter(self::ls_all($dir, $pattern, $sorting_order),
function ($file) use ($dir) {
return path::is_file(path::join($dir, $file));
}
));
}
static function ls_pall(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
return array_map(function(string $name) use ($dir) {
return path::join($dir, $name);
}, self::ls_all($dir, $pattern, $sorting_order));
}
static function ls_pdirs(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
return array_map(function(string $name) use ($dir) {
return path::join($dir, $name);
}, self::ls_dirs($dir, $pattern, $sorting_order));
}
static function ls_pfiles(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
return array_map(function(string $name) use ($dir) {
return path::join($dir, $name);
}, self::ls_files($dir, $pattern, $sorting_order));
}
} }