From 9c6b1c430e8cc94a6c9defbd428279c21b8eeee2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=B9=E6=99=AF=E7=AB=8B?= Date: Sun, 1 Sep 2024 00:33:36 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BC=98=E5=8C=96=E7=B3=BB=E7=BB=9F?= =?UTF-8?q?=E7=9B=AE=E5=BD=95=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 | 75 +++++++++---------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/plugin/think-library/src/extend/ToolsExtend.php b/plugin/think-library/src/extend/ToolsExtend.php index cc858488c..92d5a7d5a 100644 --- a/plugin/think-library/src/extend/ToolsExtend.php +++ b/plugin/think-library/src/extend/ToolsExtend.php @@ -45,21 +45,20 @@ class ToolsExtend $todir = rtrim($todir, '\\/') . DIRECTORY_SEPARATOR; // 扫描目录文件 if (empty($files) && is_dir($frdir)) { - $files = static::findFilesArray($frdir, static function (SplFileInfo $info) { - return substr($info->getBasename(), 0, 1) !== '.'; - }, static function (SplFileInfo $info) { - return substr($info->getBasename(), 0, 1) !== '.'; - }); + $filter = function (SplFileInfo $info) { + return $info->getBasename()[0] !== '.'; + }; + $files = static::findFilesArray($frdir, $filter, $filter); } // 复制文件列表 foreach ($files as $target) { - if ($force || !is_file($todir . $target)) { - $dir = dirname($todir . $target); - is_dir($dir) or mkdir($dir, 0777, true); - copy($frdir . $target, $todir . $target); + [$fromPath, $destPath] = [$frdir . $target, $todir . $target]; + if ($force || !is_file($destPath)) { + is_dir($dir = dirname($destPath)) || mkdir($dir, 0777, true); + copy($fromPath, $destPath); } // 删除来源文件 - $remove && unlink($frdir . $target); + $remove && unlink($fromPath); } // 删除来源目录 $remove && static::removeEmptyDirectory($frdir); @@ -76,51 +75,45 @@ class ToolsExtend public static function scanDirectory(string $path, string $filterExt = '', bool $shortPath = true): array { return static::findFilesArray($path, static function (SplFileInfo $info) use ($filterExt) { - return empty($filterExt) || $info->getExtension() === $filterExt; + return !$filterExt || $info->getExtension() === $filterExt; }, static function (SplFileInfo $info) { - return substr($info->getBasename(), 0, 1) !== '.'; + return $info->getBasename()[0] !== '.'; }, $shortPath); } /** - * 扫描指定目录 - * @param string $path - * @param ?Closure $filterFile - * @param ?Closure $filterPath - * @param boolean $shortPath - * @return array + * 扫描指定目录并返回文件路径数组 + * @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 { - $items = []; - if (file_exists($path)) { - $files = static::findFilesYield($path, $filterFile, $filterPath); - foreach ($files as $file) $items[] = $file->getRealPath(); - if ($shortPath && ($offset = strlen(realpath($path)) + 1)) { - foreach ($items as &$item) $item = substr($item, $offset); - } - } - return $items; + $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))) : []; } /** - * 扫描指定目录 - * @param string $path - * @param \Closure|null $filterFile - * @param \Closure|null $filterPath - * @param boolean $fullDirectory - * @return \Generator|\SplFileInfo[] + * 递归扫描指定目录,返回文件或目录的 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)) { - $items = is_file($path) ? [new SplFileInfo($path)] : new FilesystemIterator($path); - foreach ($items as $item) if ($item->isDir() && !$item->isLink()) { - if (is_null($filterPath) || $filterPath($item)) { - yield from static::findFilesYield($item->getPathname(), $filterFile, $filterPath, $fullDirectory); - } - $fullDirectory && yield $item; - } elseif (is_null($filterFile) || $filterFile($item)) { + 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; } }