nur-sery/nur_src/shutils.php

34 lines
1.1 KiB
PHP

<?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 {
$all = scandir($dir, $sorting_order);
if ($all === false) return [];
return 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_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_filter(self::ls_all($dir, $pattern, $sorting_order),
function ($file) use ($dir) {
return path::is_file(path::join($dir, $file));
}
);
}
}