guard missing bootstrap env templates on legacy installs

This commit is contained in:
root
2026-04-15 14:06:24 +08:00
parent 0794c903d7
commit 7ece23688f

View File

@@ -18,24 +18,34 @@ class DomainBootstrapEnvTemplateHelper
$arrTemplates = []; $arrTemplates = [];
foreach ($arrCatalog as $strKey => $arrTemplate) { 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); $arrCompare = self::compareTemplateWithCurrent($arrCurrentMap, $arrAssignments, $arrAllowedKeys);
$arrTemplates[] = [ $arrTemplates[] = [
'key' => $strKey, 'key' => $strKey,
'name' => (string)($arrTemplate['name'] ?? $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', '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', '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), 'compared_key_count' => (int)($arrCompare['compared_key_count'] ?? 0),
'matched_key_count' => (int)($arrCompare['matched_key_count'] ?? 0), 'matched_key_count' => (int)($arrCompare['matched_key_count'] ?? 0),
'drift_count' => (int)($arrCompare['drift_count'] ?? 0), 'drift_count' => (int)($arrCompare['drift_count'] ?? 0),
'drift_items' => array_values((array)($arrCompare['drift_items'] ?? [])), 'drift_items' => array_values((array)($arrCompare['drift_items'] ?? [])),
'state' => 'drift', 'state' => $boolTemplateExists ? 'drift' : 'missing',
'state_label' => '存在偏移', 'state_label' => $boolTemplateExists ? '存在偏移' : '模板缺失',
]; ];
} }
usort($arrTemplates, static function (array $arrLeft, array $arrRight): int { 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)); $intDriftCompare = ((int)($arrLeft['drift_count'] ?? 0)) <=> ((int)($arrRight['drift_count'] ?? 0));
if ($intDriftCompare !== 0) { if ($intDriftCompare !== 0) {
return $intDriftCompare; return $intDriftCompare;
@@ -55,6 +65,10 @@ class DomainBootstrapEnvTemplateHelper
]; ];
foreach ($arrTemplates as $intIndex => $arrTemplate) { foreach ($arrTemplates as $intIndex => $arrTemplate) {
if (!(bool)($arrTemplate['exists'] ?? false)) {
continue;
}
$boolExactMatch = ((int)($arrTemplate['drift_count'] ?? 0)) === 0; $boolExactMatch = ((int)($arrTemplate['drift_count'] ?? 0)) === 0;
$boolClosest = $intIndex === 0; $boolClosest = $intIndex === 0;
@@ -334,6 +348,13 @@ class DomainBootstrapEnvTemplateHelper
{ {
$arrAssignments = []; $arrAssignments = [];
$intIgnored = 0; $intIgnored = 0;
if ($strTemplatePath === '' || !is_file($strTemplatePath)) {
return [
'assignments' => [],
'ignored_count' => 0,
];
}
$arrLines = preg_split("/\r\n|\n|\r/", (string)file_get_contents($strTemplatePath)); $arrLines = preg_split("/\r\n|\n|\r/", (string)file_get_contents($strTemplatePath));
foreach ((array)$arrLines as $strLine) { foreach ((array)$arrLines as $strLine) {