Merge pull request #7058 from sminnee/fix-install-db

FIX: Allow DB::setConfig() in _config.php
This commit is contained in:
Damian Mooyman 2017-06-26 17:29:31 +12:00 committed by GitHub
commit 22f3d5cec0

View File

@ -185,8 +185,10 @@ class CoreKernel implements Kernel
$this->bootPHP(); $this->bootPHP();
$this->bootManifests($flush); $this->bootManifests($flush);
$this->bootErrorHandling(); $this->bootErrorHandling();
$this->bootDatabase(); $this->bootDatabaseEnvVars();
$this->bootConfigs(); $this->bootConfigs();
$this->bootDatabaseGlobals();
$this->validateDatabase();
} }
/** /**
@ -199,27 +201,63 @@ class CoreKernel implements Kernel
} }
/** /**
* Configure database * Load default database configuration from the $database and $databaseConfig globals
*
* @throws HTTPResponse_Exception
*/ */
protected function bootDatabase() protected function bootDatabaseGlobals()
{ {
// Check if a DB is named // Now that configs have been loaded, we can check global for database config
$name = $this->getDatabaseName(); global $databaseConfig;
global $database;
// Gracefully fail if no DB is configured $prefix = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
if (empty($name)) {
$this->detectLegacyEnvironment(); // Case 1: $databaseConfig global exists. Merge $database in as needed
$this->redirectToInstaller(); if (!empty($databaseConfig)) {
if (!empty($database)) {
$databaseConfig['database'] = $prefix . $database;
}
// Only set it if its valid, otherwise ignore $databaseConfig entirely
if (!empty($databaseConfig['database'])) {
DB::setConfig($databaseConfig);
return;
}
} }
// Case 2: $database merged into existing config
if (!empty($database)) {
$existing = DB::getConfig();
$existing['database'] = $prefix . $database;
DB::setConfig($existing);
}
}
/**
* Load default database configuration from environment variable
*/
protected function bootDatabaseEnvVars()
{
// Set default database config // Set default database config
$databaseConfig = $this->getDatabaseConfig(); $databaseConfig = $this->getDatabaseConfig();
$databaseConfig['database'] = $this->getDatabaseName(); $databaseConfig['database'] = $this->getDatabaseName();
DB::setConfig($databaseConfig); DB::setConfig($databaseConfig);
} }
/**
* Check that the database configuration is valid, throwing an HTTPResponse_Exception if it's not
*
* @throws HTTPResponse_Exception
*/
protected function validateDatabase()
{
$databaseConfig = DB::getConfig();
// Gracefully fail if no DB is configured
if (empty($databaseConfig['database'])) {
$this->detectLegacyEnvironment();
$this->redirectToInstaller();
}
}
/** /**
* Check if there's a legacy _ss_environment.php file * Check if there's a legacy _ss_environment.php file
* *
@ -280,11 +318,6 @@ class CoreKernel implements Kernel
*/ */
protected function getDatabaseConfig() protected function getDatabaseConfig()
{ {
// Check global config
global $databaseConfig;
if (!empty($databaseConfig)) {
return $databaseConfig;
}
/** @skipUpgrade */ /** @skipUpgrade */
$databaseConfig = [ $databaseConfig = [