From 7ef19f635779ca8a98a579d1247f94d2b38d140d Mon Sep 17 00:00:00 2001 From: augushong Date: Fri, 27 Mar 2026 20:26:34 +0800 Subject: [PATCH] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=94=B6=E9=9B=86=E9=80=BB=E8=BE=91=E5=88=B0=E7=8B=AC?= =?UTF-8?q?=E7=AB=8B=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将重复的文件收集和忽略前缀检查代码提取为 `collectTrackedFiles` 和 `isIgnoredPath` 方法,提高代码复用性和可读性。 --- .../admin/service/AdminUpdateServiceBase.php | 60 +++++++++++-------- 1 file changed, 34 insertions(+), 26 deletions(-) diff --git a/extend/base/admin/service/AdminUpdateServiceBase.php b/extend/base/admin/service/AdminUpdateServiceBase.php index 6291ab2..7e39057 100644 --- a/extend/base/admin/service/AdminUpdateServiceBase.php +++ b/extend/base/admin/service/AdminUpdateServiceBase.php @@ -126,37 +126,14 @@ class AdminUpdateServiceBase '.git', ]; - $filter_files_function = function (StorageAttributes $attributes) use ($ignore_prefix) { - if ($attributes->isDir()) { - return false; - } - - foreach ($ignore_prefix as $prefix) { - if (str_starts_with($attributes->path(), $prefix)) { - return false; - } - } - - return true; - }; - // 当前版本的应该被处理所有文件 - $current_version_files = $current_version_filesystem->listContents('/', true) - ->filter($filter_files_function) - ->map(fn (StorageAttributes $attributes) => $attributes->path()) - ->toArray(); + $current_version_files = $this->collectTrackedFiles($current_version_filesystem, $ignore_prefix); // 最新版本的所有文件 - $last_version_files = $last_version_filesystem->listContents('/', true) - ->filter($filter_files_function) - ->map(fn (StorageAttributes $attributes) => $attributes->path()) - ->toArray(); + $last_version_files = $this->collectTrackedFiles($last_version_filesystem, $ignore_prefix); // 本身的所有文件 - $now_files = $now_filesystem->listContents('/', true) - ->filter($filter_files_function) - ->map(fn (StorageAttributes $attributes) => $attributes->path()) - ->toArray(); + $now_files = $this->collectTrackedFiles($now_filesystem, $ignore_prefix); $changed_files = []; @@ -393,6 +370,37 @@ class AdminUpdateServiceBase return false; } + protected function collectTrackedFiles(Filesystem $filesystem, array $ignore_prefix, string $path = '/') + { + $files = []; + + foreach ($filesystem->listContents($path, false) as $attributes) { + if ($this->isIgnoredPath($attributes->path(), $ignore_prefix)) { + continue; + } + + if ($attributes->isDir()) { + $files = array_merge($files, $this->collectTrackedFiles($filesystem, $ignore_prefix, $attributes->path())); + continue; + } + + $files[] = $attributes->path(); + } + + return $files; + } + + protected function isIgnoredPath(string $path, array $ignore_prefix) + { + foreach ($ignore_prefix as $prefix) { + if (str_starts_with($path, $prefix)) { + return true; + } + } + + return false; + } + protected function cleanWorkpaceDir() { $dir = App::getRuntimePath() . '/update/';