From ee6783804edf4593fea75a8bcf6b1577443d6569 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Mon, 9 Sep 2024 23:39:39 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E6=94=B9=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=89=AB=E6=8F=8F=EF=BC=8C=E5=A2=9E=E5=8A=A0=E6=B7=B1=E5=BA=A6?= =?UTF-8?q?=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/think-library/src/Library.php | 4 +- .../think-library/src/extend/PhinxExtend.php | 2 +- .../think-library/src/extend/ToolsExtend.php | 116 ++++++++++-------- .../src/support/middleware/MultAccess.php | 2 +- 4 files changed, 66 insertions(+), 58 deletions(-) diff --git a/plugin/think-library/src/Library.php b/plugin/think-library/src/Library.php index f958003c6..901c3d643 100644 --- a/plugin/think-library/src/Library.php +++ b/plugin/think-library/src/Library.php @@ -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); diff --git a/plugin/think-library/src/extend/PhinxExtend.php b/plugin/think-library/src/extend/PhinxExtend.php index 8f6f46f4f..22afa9107 100644 --- a/plugin/think-library/src/extend/PhinxExtend.php +++ b/plugin/think-library/src/extend/PhinxExtend.php @@ -301,7 +301,7 @@ CODE; ToolsExtend::removeEmptyDirectory($dataPath); } } - }); + }, null, true, 1); // 计算下一个版本号 $version = !empty($versions) ? min($versions) - 1 : $startVersion; diff --git a/plugin/think-library/src/extend/ToolsExtend.php b/plugin/think-library/src/extend/ToolsExtend.php index 92d5a7d5a..5e204d0d4 100644 --- a/plugin/think-library/src/extend/ToolsExtend.php +++ b/plugin/think-library/src/extend/ToolsExtend.php @@ -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; + } + } + } + + } \ No newline at end of file diff --git a/plugin/think-library/src/support/middleware/MultAccess.php b/plugin/think-library/src/support/middleware/MultAccess.php index 4ae50108f..ab47836ca 100644 --- a/plugin/think-library/src/support/middleware/MultAccess.php +++ b/plugin/think-library/src/support/middleware/MultAccess.php @@ -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();