From c94723315a6149bbbb05a4308654027e264e7f01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Tue, 15 Oct 2024 14:17:20 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E6=89=AB=E6=8F=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../think-library/src/extend/ToolsExtend.php | 27 ++++++++++--------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/plugin/think-library/src/extend/ToolsExtend.php b/plugin/think-library/src/extend/ToolsExtend.php index b7da49f36..e3b51e18b 100644 --- a/plugin/think-library/src/extend/ToolsExtend.php +++ b/plugin/think-library/src/extend/ToolsExtend.php @@ -21,6 +21,8 @@ namespace think\admin\extend; use Closure; use FilesystemIterator; use Generator; +use RecursiveDirectoryIterator; +use RecursiveIteratorIterator; use SplFileInfo; /** @@ -112,32 +114,31 @@ class ToolsExtend $files[] = $short ? $info->getBasename() : $info->getPathname(); } } elseif ($info->isDir()) { - foreach (static::findFilesYield($info->getPathname(), $filterFile, $filterPath, $depth) as $file) { - $files[] = $short ? substr($file->getRealPath(), strlen($info->getPathname()) + 1) : $file->getRealPath(); + foreach (static::findFilesYield($info->getRealPath(), $filterFile, $filterPath, $depth) as $file) { + $files[] = $short ? substr($file->getRealPath(), strlen($info->getRealPath()) + 1) : $file->getRealPath(); } } return $files; } /** - * 递归扫描指定目录,返回文件或目录的 SplFileInfo 对象。 + * 非递归方式扫描指定目录,返回文件或目录的 SplFileInfo 对象。 * @param string $path 目录路径。 * @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。 * @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。 - * @param ?integer $depth 当前递归深度,null 表示无限制深度 - * @param boolean $appendPath 是否包含目录本身在结果中。 + * @param ?integer $depth 当前深度限制,null 表示无限制深度。 + * @param boolean $apath 是否包含目录本身在结果中。 * @return \Generator 返回 SplFileInfo 对象的生成器。 */ - private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, ?int $depth = null, bool $appendPath = false): Generator + private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, ?int $depth = null, bool $apath = false): Generator { if (file_exists($path)) { - foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS) as $item) { - if (($isDir = $item->isDir() && !$item->isLink()) && ($filterPath === null || $filterPath($item))) { - if ($depth === null || $depth > 1) { - yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $depth !== null ? $depth - 1 : null, $appendPath); - } - if ($appendPath) yield $item; - } elseif (!$isDir && ($filterFile === null || $filterFile($item))) { + $iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS), RecursiveIteratorIterator::SELF_FIRST); + foreach ($iterator as $item) { + if (is_numeric($depth) && $iterator->getDepth() >= $depth) continue; + if ($item->isDir() && !$item->isLink()) { + ($filterPath === null || $filterPath($item)) && $apath && yield $item; + } elseif ($filterFile === null || $filterFile($item)) { yield $item; } }