From 7ece23688fb75d1f7b6a7f442f10cf5092f2374b Mon Sep 17 00:00:00 2001 From: root Date: Wed, 15 Apr 2026 14:06:24 +0800 Subject: [PATCH] guard missing bootstrap env templates on legacy installs --- .../DomainBootstrapEnvTemplateHelper.php | 29 ++++++++++++++++--- 1 file changed, 25 insertions(+), 4 deletions(-) diff --git a/code/app/common/helper/DomainBootstrapEnvTemplateHelper.php b/code/app/common/helper/DomainBootstrapEnvTemplateHelper.php index 9c0a798..807af68 100644 --- a/code/app/common/helper/DomainBootstrapEnvTemplateHelper.php +++ b/code/app/common/helper/DomainBootstrapEnvTemplateHelper.php @@ -18,24 +18,34 @@ class DomainBootstrapEnvTemplateHelper $arrTemplates = []; foreach ($arrCatalog as $strKey => $arrTemplate) { - $arrAssignments = (array)(self::parseTemplateAssignments((string)($arrTemplate['path'] ?? ''))['assignments'] ?? []); + $strTemplatePath = (string)($arrTemplate['path'] ?? ''); + $boolTemplateExists = $strTemplatePath !== '' && is_file($strTemplatePath); + $arrTemplateParse = self::parseTemplateAssignments($strTemplatePath); + $arrAssignments = (array)($arrTemplateParse['assignments'] ?? []); $arrCompare = self::compareTemplateWithCurrent($arrCurrentMap, $arrAssignments, $arrAllowedKeys); $arrTemplates[] = [ 'key' => $strKey, 'name' => (string)($arrTemplate['name'] ?? $strKey), - 'path' => (string)($arrTemplate['path'] ?? ''), + 'path' => $strTemplatePath, + 'exists' => $boolTemplateExists, 'preview_command' => 'php scripts/domain_bootstrap_env_apply.php --template=' . $strKey . ' --env-file=.env --dry-run=1 --format=text', 'apply_command' => 'php scripts/domain_bootstrap_env_apply.php --template=' . $strKey . ' --env-file=.env --dry-run=0 --format=text', 'compared_key_count' => (int)($arrCompare['compared_key_count'] ?? 0), 'matched_key_count' => (int)($arrCompare['matched_key_count'] ?? 0), 'drift_count' => (int)($arrCompare['drift_count'] ?? 0), 'drift_items' => array_values((array)($arrCompare['drift_items'] ?? [])), - 'state' => 'drift', - 'state_label' => '存在偏移', + 'state' => $boolTemplateExists ? 'drift' : 'missing', + 'state_label' => $boolTemplateExists ? '存在偏移' : '模板缺失', ]; } usort($arrTemplates, static function (array $arrLeft, array $arrRight): int { + $boolLeftExists = (bool)($arrLeft['exists'] ?? false); + $boolRightExists = (bool)($arrRight['exists'] ?? false); + if ($boolLeftExists !== $boolRightExists) { + return $boolLeftExists ? -1 : 1; + } + $intDriftCompare = ((int)($arrLeft['drift_count'] ?? 0)) <=> ((int)($arrRight['drift_count'] ?? 0)); if ($intDriftCompare !== 0) { return $intDriftCompare; @@ -55,6 +65,10 @@ class DomainBootstrapEnvTemplateHelper ]; foreach ($arrTemplates as $intIndex => $arrTemplate) { + if (!(bool)($arrTemplate['exists'] ?? false)) { + continue; + } + $boolExactMatch = ((int)($arrTemplate['drift_count'] ?? 0)) === 0; $boolClosest = $intIndex === 0; @@ -334,6 +348,13 @@ class DomainBootstrapEnvTemplateHelper { $arrAssignments = []; $intIgnored = 0; + if ($strTemplatePath === '' || !is_file($strTemplatePath)) { + return [ + 'assignments' => [], + 'ignored_count' => 0, + ]; + } + $arrLines = preg_split("/\r\n|\n|\r/", (string)file_get_contents($strTemplatePath)); foreach ((array)$arrLines as $strLine) {