refactor: 提取文件收集逻辑到独立方法

将重复的文件收集和忽略前缀检查代码提取为 `collectTrackedFiles` 和 `isIgnoredPath` 方法,提高代码复用性和可读性。
This commit is contained in:
augushong
2026-03-27 20:26:34 +08:00
parent c0d3acbd4d
commit 7ef19f6357

View File

@@ -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/';