2023-12-03 22:10:18 +04:00
|
|
|
<?php
|
|
|
|
namespace nur;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class shutils: des outils donnant le même confort que le shell
|
|
|
|
*/
|
|
|
|
class shutils {
|
|
|
|
static function ls_all(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
|
2024-03-08 08:28:49 +04:00
|
|
|
$all = scandir($dir, $sorting_order);
|
|
|
|
if ($all === false) return [];
|
2024-04-04 15:36:42 +04:00
|
|
|
return array_values(array_filter($all,
|
2023-12-03 22:10:18 +04:00
|
|
|
function ($file) use ($pattern) {
|
|
|
|
if ($file === "." || $file === "..") return false;
|
|
|
|
return $pattern === null || fnmatch($pattern, $file);
|
2024-04-04 15:36:42 +04:00
|
|
|
}
|
|
|
|
));
|
2023-12-03 22:10:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static function ls_dirs(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
|
2024-04-04 15:36:42 +04:00
|
|
|
return array_values(array_filter(self::ls_all($dir, $pattern, $sorting_order),
|
2023-12-03 22:10:18 +04:00
|
|
|
function ($file) use ($dir) {
|
|
|
|
return path::is_dir(path::join($dir, $file));
|
|
|
|
}
|
2024-04-04 15:36:42 +04:00
|
|
|
));
|
2023-12-03 22:10:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static function ls_files(string $dir, ?string $pattern=null, int $sorting_order=SCANDIR_SORT_ASCENDING): array {
|
2024-04-04 15:36:42 +04:00
|
|
|
return array_values(array_filter(self::ls_all($dir, $pattern, $sorting_order),
|
2023-12-03 22:10:18 +04:00
|
|
|
function ($file) use ($dir) {
|
|
|
|
return path::is_file(path::join($dir, $file));
|
|
|
|
}
|
2024-04-04 15:36:42 +04:00
|
|
|
));
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
2023-12-03 22:10:18 +04:00
|
|
|
}
|
|
|
|
}
|