FIX: Allow DB::setConfig() in _config.php

This wasn’t working because the database was being validated before
_config.php was loaed.

This is how the installer sets config so this is an important fix.
This commit is contained in:
Sam Minnee 2017-06-26 11:27:34 +12:00
parent f60dcba5e7
commit 3c35d25a64
1 changed files with 49 additions and 16 deletions

View File

@ -185,8 +185,10 @@ class CoreKernel implements Kernel
$this->bootPHP();
$this->bootManifests($flush);
$this->bootErrorHandling();
$this->bootDatabase();
$this->bootDatabaseEnvVars();
$this->bootConfigs();
$this->bootDatabaseGlobals();
$this->validateDatabase();
}
/**
@ -199,27 +201,63 @@ class CoreKernel implements Kernel
}
/**
* Configure database
*
* @throws HTTPResponse_Exception
* Load default database configuration from the $database and $databaseConfig globals
*/
protected function bootDatabase()
protected function bootDatabaseGlobals()
{
// Check if a DB is named
$name = $this->getDatabaseName();
// Now that configs have been loaded, we can check global for database config
global $databaseConfig;
global $database;
// Gracefully fail if no DB is configured
if (empty($name)) {
$this->detectLegacyEnvironment();
$this->redirectToInstaller();
$prefix = getenv('SS_DATABASE_PREFIX') ?: 'SS_';
// Case 1: $databaseConfig global exists. Merge $database in as needed
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
$databaseConfig = $this->getDatabaseConfig();
$databaseConfig['database'] = $this->getDatabaseName();
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
*
@ -280,11 +318,6 @@ class CoreKernel implements Kernel
*/
protected function getDatabaseConfig()
{
// Check global config
global $databaseConfig;
if (!empty($databaseConfig)) {
return $databaseConfig;
}
/** @skipUpgrade */
$databaseConfig = [