Introduce ConfigFileManager for config files

This commit is contained in:
Philipp 2023-01-01 21:10:37 +01:00
parent fea4b202c1
commit 0f91d1cbde
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
21 changed files with 343 additions and 302 deletions

View file

@ -26,22 +26,15 @@ use Friendica\Core\Config\Exception\ConfigFileException;
use Friendica\Core\Config\ValueObject\Cache;
/**
* The ConfigFileLoader loads config-files and stores them in a ConfigCache ( @see Cache )
* The ConfigFileLoader loads and saves config-files and stores them in a ConfigCache ( @see Cache )
*
* It is capable of loading the following config files:
* - *.config.php (current)
* - *.ini.php (deprecated)
* - *.htconfig.php (deprecated)
*/
class ConfigFileLoader
class ConfigFileManager
{
/**
* The default name of the user defined ini file
*
* @var string
*/
const CONFIG_INI = 'local';
/**
* The default name of the user defined legacy config file
*
@ -49,6 +42,13 @@ class ConfigFileLoader
*/
const CONFIG_HTCONFIG = 'htconfig';
/**
* The config file, where overrides per admin page/console are saved at
*
* @var string
*/
const CONFIG_DATA_FILE = 'node.config.php';
/**
* The sample string inside the configs, which shouldn't get loaded
*
@ -89,7 +89,7 @@ class ConfigFileLoader
*
* @param Cache $config The config cache to load to
* @param array $server The $_SERVER array
* @param bool $raw Setup the raw config format
* @param bool $raw Set up the raw config format
*
* @throws ConfigFileException
*/
@ -106,6 +106,9 @@ class ConfigFileLoader
// Now load every other config you find inside the 'config/' directory
$this->loadCoreConfig($config);
// Now load the node.config.php file with the node specific config values (based on admin gui/console actions)
$this->loadDataConfig($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
@ -158,6 +161,50 @@ class ConfigFileLoader
}
}
/**
* Tries to load the data config file with the overridden data
*
* @param Cache $config The Config cache
*
* @throws ConfigFileException In case the config file isn't loadable
*/
private function loadDataConfig(Cache $config)
{
$filename = $this->configDir . '/' . self::CONFIG_DATA_FILE;
if (file_exists($filename)) {
$dataArray = include $filename;
if (!is_array($dataArray)) {
throw new ConfigFileException(sprintf('Error loading config file %s', $filename));
}
$config->load($dataArray, Cache::SOURCE_DATA);
}
}
/**
* Saves overridden config entries back into the data.config.phpR
*
* @param Cache $config The config cache
*
* @throws ConfigFileException In case the config file isn't writeable or the data is invalid
*/
public function saveData(Cache $config)
{
$data = $config->getDataBySource(Cache::SOURCE_DATA);
$encodedData = ConfigFileTransformer::encode($data);
if (!$encodedData) {
throw new ConfigFileException('config source cannot get encoded');
}
if (!file_put_contents($this->configDir . '/' . self::CONFIG_DATA_FILE, $encodedData)) {
throw new ConfigFileException(sprintf('Cannot save data to file %s/%s', $this->configDir, self::CONFIG_DATA_FILE));
}
}
/**
* Tries to load the specified addon-configuration and returns the config array.
*
@ -353,12 +400,16 @@ class ConfigFileLoader
*/
private function loadConfigFile(string $filepath): array
{
$config = include($filepath);
if (file_exists($filepath)) {
$config = include($filepath);
if (!is_array($config)) {
throw new ConfigFileException('Error loading config file ' . $filepath);
if (!is_array($config)) {
throw new ConfigFileException('Error loading config file ' . $filepath);
}
return $config;
} else {
return [];
}
return $config;
}
}