From 29337c45a0630e9c99bdf6d372276e3b76efe68b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Tue, 10 Sep 2024 09:18:11 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=9B=AE=E5=BD=95?= =?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 | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/plugin/think-library/src/extend/ToolsExtend.php b/plugin/think-library/src/extend/ToolsExtend.php index ab2d33272..9941383bd 100644 --- a/plugin/think-library/src/extend/ToolsExtend.php +++ b/plugin/think-library/src/extend/ToolsExtend.php @@ -73,7 +73,7 @@ class ToolsExtend */ public static function removeEmptyDirectory(string $path): bool { - foreach (self::findFilesYield($path, null, null, true) as $item) { + foreach (self::findFilesYield($path, null, null, null, true) as $item) { ($item->isFile() || $item->isLink()) ? unlink($item->getRealPath()) : rmdir($item->getRealPath()); } return is_file($path) ? unlink($path) : (!is_dir($path) || rmdir($path)); @@ -107,16 +107,17 @@ class ToolsExtend */ public static function findFilesArray(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $shortPath = true, ?int $depth = null): array { - if (is_dir($path)) { - $pathSize = $shortPath ? strlen(realpath($path)) + 1 : 0; - return file_exists($path) ? array_map(function ($file) use ($pathSize, $shortPath) { - return $shortPath ? substr($file->getRealPath(), $pathSize) : $file->getRealPath(); - }, iterator_to_array(static::findFilesYield($path, $filterFile, $filterPath, false, $depth))) : []; - } elseif (is_file($path)) { - return $filterFile === null || $filterFile(new SplFileInfo($path)) ? [basename($path)] : []; - } else { - return []; + $files = []; + if (is_file($path)) { + if (($file = new SplFileInfo($path)) && ($filterFile === null || $filterFile($file))) { + $files[] = $shortPath ? $file->getBasename() : $file->getPathname(); + } + } elseif (is_dir($path)) { + foreach (static::findFilesYield($path, $filterFile, $filterPath, $depth) as $file) { + $files[] = $shortPath ? substr($file->getRealPath(), strlen($path) + 1) : $file->getRealPath(); + } } + return $files; } /** @@ -124,22 +125,22 @@ class ToolsExtend * @param string $path 目录路径。 * @param \Closure|null $filterFile 文件过滤器闭包,返回 true 表示文件被接受。 * @param \Closure|null $filterPath 目录过滤器闭包,返回 true 表示目录被接受。 - * @param boolean $appendPath 是否包含目录本身在结果中。 * @param ?integer $depth 当前递归深度,null 表示无限制深度 + * @param boolean $appendPath 是否包含目录本身在结果中。 * @return \Generator 返回 SplFileInfo 对象的生成器。 */ - private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, bool $appendPath = false, ?int $depth = null): Generator + private static function findFilesYield(string $path, ?Closure $filterFile = null, ?Closure $filterPath = null, ?int $depth = null, bool $appendPath = false): Generator { - if (!file_exists($path)) return; - foreach (is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path, FilesystemIterator::SKIP_DOTS) 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 (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 > 0) { + yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $depth !== null ? $depth - 1 : null, $appendPath); + } + if ($appendPath) yield $item; + } elseif (!$isDir && ($filterFile === null || $filterFile($item))) { + yield $item; } - if ($appendPath) yield $item; - } elseif (!$isDir && ($filterFile === null || $filterFile($item))) { - yield $item; } } }