don’t error when trying to force —safe

Signed-off-by: Andy Miller <rhuk@mac.com>
This commit is contained in:
Andy Miller
2025-11-09 15:50:42 +00:00
parent a07a1b332a
commit 4c324ef4b8
3 changed files with 44 additions and 3 deletions

View File

@@ -10,6 +10,31 @@ if (!defined('GRAV_ROOT')) {
die();
}
// Check if Install class is already loaded (from an older Grav version)
// This happens when upgrading from older versions where the OLD Install class
// was loaded via autoloader before extracting the update package (e.g., via Install::forceSafeUpgrade())
if (class_exists('Grav\\Installer\\Install', false)) {
// OLD Install class is already loaded. We cannot load the NEW one due to PHP limitations.
// However, we can work around this by:
// 1. Using a different class name for the NEW installer
// 2. Or, accepting that the OLD Install class will run but ensuring it can still upgrade properly
// For now, use the OLD Install class but set its location to this extracted package
// so it processes files from here
$install = Grav\Installer\Install::instance();
// Use reflection to update the location property to point to this package
$reflection = new \ReflectionClass($install);
if ($reflection->hasProperty('location')) {
$locationProp = $reflection->getProperty('location');
$locationProp->setAccessible(true);
$locationProp->setValue($install, __DIR__ . '/..');
}
return $install;
}
// Normal case: Install class not yet loaded, load the NEW one
require_once __DIR__ . '/src/Grav/Installer/Install.php';
return Grav\Installer\Install::instance();

View File

@@ -132,14 +132,17 @@ class SelfupgradeCommand extends GpmCommand
if ($forceSafe || $forceLegacy) {
$forcedMode = $forceSafe ? true : false;
Install::forceSafeUpgrade($forcedMode);
// NOTE: Do not call Install::forceSafeUpgrade() here as it would load the old Install class
// before the upgrade package is extracted, causing a class redeclaration error.
// Instead, we set the config and also use an environment variable as a fallback.
putenv('GRAV_FORCE_SAFE_UPGRADE=' . ($forcedMode ? '1' : '0'));
try {
$grav = Grav::instance();
if ($grav && isset($grav['config'])) {
$grav['config']->set('system.updates.safe_upgrade', $forcedMode);
}
} catch (\Throwable $e) {
// Ignore container bootstrap failures; mode override still applies.
// Ignore container bootstrap failures; mode override still applies via env var.
}
if ($forceSafe) {
@@ -344,7 +347,12 @@ class SelfupgradeCommand extends GpmCommand
return $error;
} finally {
if (null !== $forcedMode) {
Install::forceSafeUpgrade(null);
// Clean up environment variable
putenv('GRAV_FORCE_SAFE_UPGRADE');
// Only call Install::forceSafeUpgrade if Install class has been loaded
if (class_exists(\Grav\Installer\Install::class, false)) {
Install::forceSafeUpgrade(null);
}
}
}
}

View File

@@ -471,10 +471,18 @@ ERR;
require_once $serviceFile;
}
// Check static override first
if (null !== self::$forceSafeUpgrade) {
return self::$forceSafeUpgrade;
}
// Check environment variable set by SelfupgradeCommand (avoids early class loading)
$envValue = getenv('GRAV_FORCE_SAFE_UPGRADE');
if (false !== $envValue && '' !== $envValue) {
return $envValue === '1';
}
// Check Grav config
try {
$grav = Grav::instance();
if ($grav && isset($grav['config'])) {