Introduce "static/env.config.php" for environment variable mapping to config cache entries

- Added new database.port config value (used for MYSQL_PORT)
- Removed now obsolete db environment variable functionality
- Added functionality to load env variables (overwrites DB based cached)
This commit is contained in:
Philipp Holzer 2020-10-06 20:06:52 +02:00
commit d39ee428f0
No known key found for this signature in database
GPG key ID: 9A28B7D4FF5667BD
8 changed files with 93 additions and 8 deletions

View file

@ -36,6 +36,8 @@ class Cache
const SOURCE_DB = 1;
/** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
const SOURCE_ENV = 3;
/** @var int Indicates that the cache entry is fixed and must not be changed */
const SOURCE_FIX = 4;
/** @var int Default value for a config source */
const SOURCE_DEFAULT = self::SOURCE_FILE;

View file

@ -66,7 +66,7 @@ class Database
protected $testmode = false;
private $relation = [];
public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger, array $server = [])
public function __construct(Cache $configCache, Profiler $profiler, LoggerInterface $logger)
{
// We are storing these values for being able to perform a reconnect
$this->configCache = $configCache;

View file

@ -37,10 +37,10 @@ class ConfigFactory
*
* @throws Exception
*/
public function createCache(ConfigFileLoader $loader)
public function createCache(ConfigFileLoader $loader, array $server = [])
{
$configCache = new Cache();
$loader->setupCache($configCache);
$loader->setupCache($configCache, $server);
return $configCache;
}

View file

@ -97,11 +97,12 @@ class ConfigFileLoader
* expected local.config.php
*
* @param Cache $config The config cache to load to
* @param array $server The $_SERVER array
* @param bool $raw Setup the raw config format
*
* @throws Exception
*/
public function setupCache(Cache $config, $raw = false)
public function setupCache(Cache $config, array $server = [], $raw = false)
{
// Load static config files first, the order is important
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
@ -114,10 +115,12 @@ class ConfigFileLoader
// Now load every other config you find inside the 'config/' directory
$this->loadCoreConfig($config);
$config->load($this->loadEnvConfig($server), Cache::SOURCE_ENV);
// In case of install mode, add the found basepath (because there isn't a basepath set yet
if (!$raw && empty($config->get('system', 'basepath'))) {
// Setting at least the basepath we know
$config->set('system', 'basepath', $this->baseDir);
$config->set('system', 'basepath', $this->baseDir, Cache::SOURCE_FILE);
}
}
@ -192,6 +195,38 @@ class ConfigFileLoader
}
}
/**
* Tries to load environment specific variables, based on the `env.config.php` mapping table
*
* @param array $server The $_SERVER variable
*
* @return array The config array (empty if no config was found)
*
* @throws Exception if the configuration file isn't readable
*/
public function loadEnvConfig(array $server)
{
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
self::STATIC_DIR . DIRECTORY_SEPARATOR . // static/
"env.config.php"; // env.config.php
if (!file_exists($filepath)) {
return [];
}
$envConfig = $this->loadConfigFile($filepath);
$return = [];
foreach ($envConfig as $envKey => $configStructure) {
if (isset($server[$envKey])) {
$return[$configStructure[0]][$configStructure[1]] = $server[$envKey];
}
}
return $return;
}
/**
* Get the config files of the config-directory
*