From 24b5dbcbeb3a9c0c67312b07eedbfe29ab81cee3 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:49:10 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E8=BF=9B=E4=B8=80=E6=AD=A5=E4=BC=98?= =?UTF-8?q?=E5=8C=96=E8=B7=AF=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 | 20 +++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/plugin/think-library/src/extend/ToolsExtend.php b/plugin/think-library/src/extend/ToolsExtend.php index 5e204d0d4..ab2d33272 100644 --- a/plugin/think-library/src/extend/ToolsExtend.php +++ b/plugin/think-library/src/extend/ToolsExtend.php @@ -22,6 +22,7 @@ use Closure; use FilesystemIterator; use Generator; use SplFileInfo; +use think\File; /** * 通用工具扩展 @@ -106,11 +107,16 @@ class ToolsExtend */ 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))) : []; + 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 []; + } } /** @@ -125,7 +131,7 @@ class ToolsExtend 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) { + 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) { @@ -137,6 +143,4 @@ class ToolsExtend } } } - - } \ No newline at end of file