mirror of
https://github.com/librenms/librenms.git
synced 2024-10-07 16:52:45 +00:00
refactor: Refactor database and config init (#8527)
* Refactor database and config init Connect to the database without loading full config Load config completely so post-processing is always done consistently. Erase existing $config when loading, fixes issues in case we load the config twice. If the database is not connected, don't try to load database settings. (Fixes some db errors on install) Attempt to remove $config access/modification before init.php Remove usage of db_name, that might not match the connected database. Centralize db config loading, so we consistently apply db_test database settings. Many of these changes are influenced by Laravel port. * Some safety so we don't assign strings to numeric port field Smooth out phpunit bootstrap * Fix a couple of scrutinizer warnings.
This commit is contained in:
committed by
Neil Lathwood
parent
c09e44aa94
commit
d841625f12
@@ -28,19 +28,44 @@ namespace LibreNMS;
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* Load the user config from config.php
|
||||
* Load the config, if the database connected, pull in database settings.
|
||||
*
|
||||
* @param string $install_dir
|
||||
* @return array
|
||||
* return &array
|
||||
*/
|
||||
public static function &load($install_dir = null)
|
||||
public static function &load()
|
||||
{
|
||||
global $config;
|
||||
|
||||
if (empty($install_dir)) {
|
||||
$install_dir = __DIR__ . '/../';
|
||||
self::loadFiles();
|
||||
|
||||
// Make sure the database is connected
|
||||
if (dbIsConnected()) {
|
||||
// pull in the database config settings
|
||||
self::mergeDb();
|
||||
|
||||
// load graph types from the database
|
||||
self::loadGraphsFromDb();
|
||||
}
|
||||
$install_dir = realpath($install_dir);
|
||||
|
||||
// Process $config to tidy up
|
||||
self::processConfig();
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the user config from config.php, defaults.inc.php and definitions.inc.php, etc.
|
||||
* Erases existing config.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private static function &loadFiles()
|
||||
{
|
||||
global $config;
|
||||
|
||||
$config = []; // start fresh
|
||||
|
||||
$install_dir = realpath(__DIR__ . '/../');
|
||||
$config['install_dir'] = $install_dir;
|
||||
|
||||
// load defaults
|
||||
@@ -60,34 +85,6 @@ class Config
|
||||
return $config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Load Config from the database
|
||||
*
|
||||
* @throws Exceptions\DatabaseConnectException
|
||||
*/
|
||||
public static function &loadFromDatabase()
|
||||
{
|
||||
global $config;
|
||||
|
||||
if (empty($config)) {
|
||||
self::load();
|
||||
}
|
||||
|
||||
// Make sure the database is connected
|
||||
dbConnect();
|
||||
|
||||
// pull in the database config settings
|
||||
self::mergeDb();
|
||||
|
||||
// load graph types from the database
|
||||
self::loadGraphsFromDb();
|
||||
|
||||
// Process $config to tidy up
|
||||
self::processConfig();
|
||||
|
||||
return $config;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get a config value, if non existent null (or default if set) will be returned
|
||||
@@ -428,4 +425,42 @@ class Config
|
||||
self::set($new, self::get($old));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get just the database connection settings from config.php
|
||||
*
|
||||
* @return array (keys: db_host, db_port, db_name, db_user, db_pass, db_socket)
|
||||
*/
|
||||
public static function getDatabaseSettings()
|
||||
{
|
||||
// Do not access global $config in this function!
|
||||
|
||||
$keys = $config = [
|
||||
'db_host' => '',
|
||||
'db_port' => '',
|
||||
'db_name' => '',
|
||||
'db_user' => '',
|
||||
'db_pass' => '',
|
||||
'db_socket' => '',
|
||||
];
|
||||
|
||||
if (is_file(__DIR__ . '/../config.php')) {
|
||||
include __DIR__ . '/../config.php';
|
||||
}
|
||||
|
||||
// Check for testing database
|
||||
if (getenv('DBTEST')) {
|
||||
if (isset($config['test_db_name'])) {
|
||||
$config['db_name'] = $config['test_db_name'];
|
||||
}
|
||||
if (isset($config['test_db_user'])) {
|
||||
$config['db_user'] = $config['test_db_user'];
|
||||
}
|
||||
if (isset($config['test_db_pass'])) {
|
||||
$config['db_pass'] = $config['test_db_pass'];
|
||||
}
|
||||
}
|
||||
|
||||
return array_intersect_key($config, $keys); // return only the db settings
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user