fix: 修改文件扫描,增加深度参数

This commit is contained in:
邹景立 2024-09-09 23:39:39 +08:00
parent 328bdc1905
commit ee6783804e
4 changed files with 66 additions and 58 deletions

View File

@ -108,9 +108,9 @@ class Library extends Service
{
// 动态加载全局配置
[$dir, $ext] = [$this->app->getBasePath(), $this->app->getConfigExt()];
ToolsExtend::findFilesYield($dir, function (SplFileInfo $info) use ($ext) {
ToolsExtend::findFilesArray($dir, function (SplFileInfo $info) use ($ext) {
$info->getBasename() === "sys{$ext}" && include_once $info->getPathname();
});
}, null, true, 1);
if (is_file($file = "{$dir}common{$ext}")) include_once $file;
if (is_file($file = "{$dir}provider{$ext}")) $this->app->bind(include $file);
if (is_file($file = "{$dir}event{$ext}")) $this->app->loadEvent(include $file);

View File

@ -301,7 +301,7 @@ CODE;
ToolsExtend::removeEmptyDirectory($dataPath);
}
}
});
}, null, true, 1);
// 计算下一个版本号
$version = !empty($versions) ? min($versions) - 1 : $startVersion;

View File

@ -65,60 +65,6 @@ class ToolsExtend
return true;
}
/**
* 扫描目录列表
* @param string $path 扫描目录
* @param string $filterExt 筛选后缀
* @param boolean $shortPath 相对路径
* @return array
*/
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true): array
{
return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) {
return !$filterExt || $info->getExtension() === $filterExt;
}, static function (SplFileInfo $info) {
return $info->getBasename()[0] !== '.';
}, $shortPath);
}
/**
* 扫描指定目录并返回文件路径数组
* @param string $path 要扫描的目录路径
* @param ?Closure $filterFile 用于过滤文件的闭包
* @param ?Closure $filterPath 用于过滤目录的闭包
* @param boolean $shortPath 是否返回相对于给定路径的短路径
* @return array 包含文件路径的数组
*/
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true): array
{
$pathLength = $shortPath ? strlen(realpath($path)) + 1 : 0;
return file_exists($path) ? array_map(function ($file) use ($shortPath, $pathLength) {
return $shortPath ? substr($file->getRealPath(), $pathLength) : $file->getRealPath();
}, iterator_to_array(static::findFilesYield($path, $filterFile, $filterPath))) : [];
}
/**
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
* @param string $path 目录路径。
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
* @param boolean $fullDirectory 是否包含目录本身在结果中。
* @return \Generator 返回 SplFileInfo 对象的生成器。
*/
public static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $fullDirectory = false): Generator
{
if (!file_exists($path)) return;
foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path) as $item) {
$isDir = $item->isDir() && !$item->isLink();
if ($isDir && ($filterPath === null || $filterPath($item))) {
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $fullDirectory);
if ($fullDirectory) yield $item;
} elseif (!$isDir && ($filterFile === null || $filterFile($item))) {
yield $item;
}
}
}
/**
* 移除清空目录
* @param string $path
@ -131,4 +77,66 @@ class ToolsExtend
}
return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path));
}
/**
* 扫描目录列表
* @param string $path 扫描目录
* @param string $filterExt 筛选后缀
* @param boolean $shortPath 相对路径
* @param ?integer $depth 当前递归深度null 表示无限制深度
* @return array
*/
public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true, ?int $depth = null): array
{
return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) {
return !$filterExt || $info->getExtension() === $filterExt;
}, static function (SplFileInfo $info) {
return $info->getBasename()[0] !== '.';
}, $shortPath, $depth);
}
/**
* 扫描指定目录并返回文件路径数组
* @param string $path 要扫描的目录路径
* @param ?Closure $filterFile 用于过滤文件的闭包
* @param ?Closure $filterPath 用于过滤目录的闭包
* @param boolean $shortPath 是否返回相对于给定路径的短路径
* @param ?integer $depth 当前递归深度null 表示无限制深度
* @return array 包含文件路径的数组
*/
public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true, ?int $depth = null): array
{
if (!file_exists($path)) return [];
$pathLength = $shortPath ? strlen(realpath($path)) + 1 : 0;
return file_exists($path) ? array_map(function ($file) use ($shortPath, $pathLength) {
return $shortPath ? substr($file->getRealPath(), $pathLength) : $file->getRealPath();
}, iterator_to_array(static::findFilesYield($path, $filterFile, $filterPath, false, $depth))) : [];
}
/**
* 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。
* @param string $path 目录路径。
* @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。
* @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。
* @param boolean $appendPath 是否包含目录本身在结果中。
* @param ?integer $depth 当前递归深度null 表示无限制深度
* @return \Generator 返回 SplFileInfo 对象的生成器。
*/
private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $appendPath = false, ?int $depth = null): Generator
{
if (!file_exists($path)) return;
foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path) as $item) {
$isDir = $item->isDir() && !$item->isLink();
if ($isDir && ($filterPath === null || $filterPath($item))) {
if ($depth === null || $depth > 0) {
yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $appendPath, $depth !== null ? $depth - 1 : null);
}
if ($appendPath) yield $item;
} elseif (!$isDir && ($filterFile === null || $filterFile($item))) {
yield $item;
}
}
}
}

View File

@ -173,7 +173,7 @@ class MultAccess
if (strtolower(".{$info->getExtension()}") === $ext) {
$this->app->config->load($info->getPathname(), $info->getBasename($ext));
}
});
}, null, true, 1);
// 加载应用路由配置
if (in_array('route', $fmaps) && method_exists($this->app->route, 'reload')) {
$this->app->route->reload();