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

@ -71,12 +71,18 @@ interface IManageConfigValues
* @param string $cat The category of the configuration value
* @param string $key The configuration key to set
* @param mixed $value The value to store
* @param bool $autosave If true, implicit save the value
*
* @return bool Operation success
*
* @throws ConfigPersistenceException In case the persistence layer throws errors
*/
public function set(string $cat, string $key, $value): bool;
public function set(string $cat, string $key, $value, bool $autosave = true): bool;
/**
* Save back the overridden values of the config cache
*/
public function save();
/**
* Deletes the given key from the system configuration.
@ -85,13 +91,14 @@ interface IManageConfigValues
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
* @param bool $autosave If true, implicit save the value
*
* @return bool
*
* @throws ConfigPersistenceException In case the persistence layer throws errors
*
*/
public function delete(string $cat, string $key): bool;
public function delete(string $cat, string $key, bool $autosave = true): bool;
/**
* Returns the Config Cache

View file

@ -21,12 +21,12 @@
namespace Friendica\Core\Config\Factory;
use Friendica\Core\Config\Capability;
use Friendica\Core\Config\Repository;
use Friendica\Core\Config\Type;
use Friendica\Core\Config\Util;
use Friendica\Core\Config\ValueObject\Cache;
/**
* The config factory for creating either the cache or the whole model
*/
class Config
{
/**
@ -54,9 +54,9 @@ class Config
* @param string $basePath The basepath of FRIENDICA
* @param array $server The $_SERVER array
*
* @return Util\ConfigFileLoader
* @return Util\ConfigFileManager
*/
public function createConfigFileLoader(string $basePath, array $server = []): Util\ConfigFileLoader
public function createConfigFileLoader(string $basePath, array $server = []): Util\ConfigFileManager
{
if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
$configDir = $server[self::CONFIG_DIR_ENV];
@ -65,19 +65,19 @@ class Config
}
$staticDir = $basePath . DIRECTORY_SEPARATOR . self::STATIC_DIR;
return new Util\ConfigFileLoader($basePath, $configDir, $staticDir);
return new Util\ConfigFileManager($basePath, $configDir, $staticDir, new Util\ConfigFileTransformer());
}
/**
* @param Util\ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
* @param array $server
* @param Util\ConfigFileManager $configFileManager The Config Cache manager (INI/config/.htconfig)
* @param array $server
*
* @return Cache
*/
public function createCache(Util\ConfigFileLoader $loader, array $server = []): Cache
public function createCache(Util\ConfigFileManager $configFileManager, array $server = []): Cache
{
$configCache = new Cache();
$loader->setupCache($configCache, $server);
$configFileManager->setupCache($configCache, $server);
return $configCache;
}
@ -88,12 +88,12 @@ class Config
*
* @return Capability\IManageConfigValues
*/
public function create(Cache $configCache, Repository\Config $configRepo)
public function create(Util\ConfigFileManager $loader, Cache $configCache, Repository\Config $configRepo)
{
if ($configCache->get('system', 'config_adapter') === 'preload') {
$configuration = new Type\PreloadConfig($configCache, $configRepo);
$configuration = new Type\PreloadConfig($loader, $configCache, $configRepo);
} else {
$configuration = new Type\JitConfig($configCache, $configRepo);
$configuration = new Type\JitConfig($loader, $configCache, $configRepo);
}
return $configuration;

View file

@ -51,7 +51,7 @@ class Config
*/
public function isConnected(): bool
{
return $this->db->isConnected() && !$this->mode->isInstall();
return true;
}
/**
@ -65,31 +65,7 @@ class Config
*/
public function load(?string $cat = null): array
{
$return = [];
try {
if (empty($cat)) {
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k']);
} else {
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat]);
}
while ($config = $this->db->fetch($configs)) {
$key = $config['k'];
$value = ValueConversion::toConfigValue($config['v']);
// just save it in case it is set
if (isset($value)) {
$return[$config['cat']][$key] = $value;
}
}
} catch (\Exception $exception) {
throw new ConfigPersistenceException(sprintf('Cannot load config category %s', $cat), $exception);
} finally {
$this->db->close($configs);
}
return $return;
return [];
}
/**
@ -107,24 +83,6 @@ class Config
*/
public function get(string $cat, string $key)
{
if (!$this->isConnected()) {
return null;
}
try {
$config = $this->db->selectFirst(static::$table_name, ['v'], ['cat' => $cat, 'k' => $key]);
if ($this->db->isResult($config)) {
$value = ValueConversion::toConfigValue($config['v']);
// just return it in case it is set
if (isset($value)) {
return $value;
}
}
} catch (\Exception $exception) {
throw new ConfigPersistenceException(sprintf('Cannot get config with category %s and key %s', $cat, $key), $exception);
}
return null;
}
@ -143,27 +101,7 @@ class Config
*/
public function set(string $cat, string $key, $value): bool
{
if (!$this->isConnected()) {
return false;
}
// We store our setting values in a string variable.
// So we have to do the conversion here so that the compare below works.
// The exception are array values.
$compare_value = (!is_array($value) ? (string)$value : $value);
$stored_value = $this->get($cat, $key);
if (isset($stored_value) && ($stored_value === $compare_value)) {
return true;
}
$dbValue = ValueConversion::toDbValue($value);
try {
return $this->db->update(static::$table_name, ['v' => $dbValue], ['cat' => $cat, 'k' => $key], true);
} catch (\Exception $exception) {
throw new ConfigPersistenceException(sprintf('Cannot set config with category %s and key %s', $cat, $key), $exception);
}
return true;
}
/**
@ -178,14 +116,6 @@ class Config
*/
public function delete(string $cat, string $key): bool
{
if (!$this->isConnected()) {
return false;
}
try {
return $this->db->delete(static::$table_name, ['cat' => $cat, 'k' => $key]);
} catch (\Exception $exception) {
throw new ConfigPersistenceException(sprintf('Cannot delete config with category %s and key %s', $cat, $key), $exception);
}
return true;
}
}

View file

@ -22,8 +22,10 @@
namespace Friendica\Core\Config\Type;
use Friendica\Core\Config\Repository\Config;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Capability\IManageConfigValues;
use Friendica\DI;
/**
* This class is responsible for all system-wide configuration values in Friendica
@ -43,14 +45,19 @@ abstract class AbstractConfig implements IManageConfigValues
*/
protected $configRepo;
/** @var ConfigFileManager */
protected $configFileManager;
/**
* @param ConfigFileManager $configFileManager The configuration file manager to save back configs
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configRepo The configuration repository
*/
public function __construct(Cache $configCache, Config $configRepo)
public function __construct(ConfigFileManager $configFileManager, Cache $configCache, Config $configRepo)
{
$this->configCache = $configCache;
$this->configRepo = $configRepo;
$this->configFileManager = $configFileManager;
$this->configCache = $configCache;
$this->configRepo = $configRepo;
}
/**
@ -60,4 +67,9 @@ abstract class AbstractConfig implements IManageConfigValues
{
return $this->configCache;
}
public function save()
{
$this->configFileManager->saveData($this->configCache);
}
}

View file

@ -21,6 +21,7 @@
namespace Friendica\Core\Config\Type;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Repository\Config;
@ -39,12 +40,13 @@ class JitConfig extends AbstractConfig
private $db_loaded;
/**
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configRepo The configuration model
* @param ConfigFileManager $configFileManager The configuration file manager to save back configs
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configRepo The configuration model
*/
public function __construct(Cache $configCache, Config $configRepo)
public function __construct(ConfigFileManager $configFileManager, Cache $configCache, Config $configRepo)
{
parent::__construct($configCache, $configRepo);
parent::__construct($configFileManager, $configCache, $configRepo);
$this->db_loaded = [];
$this->load();
@ -69,7 +71,7 @@ class JitConfig extends AbstractConfig
}
// load the whole category out of the DB into the cache
$this->configCache->load($config, Cache::SOURCE_DB);
$this->configCache->load($config, Cache::SOURCE_DATA);
}
/**
@ -84,7 +86,7 @@ class JitConfig extends AbstractConfig
$dbValue = $this->configRepo->get($cat, $key);
if (isset($dbValue)) {
$this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DB);
$this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DATA);
unset($dbValue);
}
@ -100,10 +102,10 @@ class JitConfig extends AbstractConfig
/**
* {@inheritDoc}
*/
public function set(string $cat, string $key, $value): bool
public function set(string $cat, string $key, $value, bool $autosave = true): bool
{
// set the cache first
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA);
// If there is no connected adapter, we're finished
if (!$this->configRepo->isConnected()) {
@ -114,13 +116,17 @@ class JitConfig extends AbstractConfig
$this->db_loaded[$cat][$key] = $stored;
if ($autosave) {
$this->save();
}
return $cached && $stored;
}
/**
* {@inheritDoc}
*/
public function delete(string $cat, string $key): bool
public function delete(string $cat, string $key, bool $autosave = true): bool
{
$cacheRemoved = $this->configCache->delete($cat, $key);
@ -134,6 +140,10 @@ class JitConfig extends AbstractConfig
$storeRemoved = $this->configRepo->delete($cat, $key);
if ($autosave) {
$this->save();
}
return $cacheRemoved || $storeRemoved;
}
}

View file

@ -21,6 +21,7 @@
namespace Friendica\Core\Config\Type;
use Friendica\Core\Config\Util\ConfigFileManager;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\Config\Repository\Config;
@ -36,12 +37,13 @@ class PreloadConfig extends AbstractConfig
private $config_loaded;
/**
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configRepo The configuration model
* @param ConfigFileManager $configFileManager The configuration file manager to save back configs
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configRepo The configuration model
*/
public function __construct(Cache $configCache, Config $configRepo)
public function __construct(ConfigFileManager $configFileManager, Cache $configCache, Config $configRepo)
{
parent::__construct($configCache, $configRepo);
parent::__construct($configFileManager, $configCache, $configRepo);
$this->config_loaded = false;
$this->load();
@ -68,7 +70,7 @@ class PreloadConfig extends AbstractConfig
$this->config_loaded = true;
// load the whole category out of the DB into the cache
$this->configCache->load($config, Cache::SOURCE_DB);
$this->configCache->load($config, Cache::SOURCE_DATA);
}
/**
@ -80,7 +82,7 @@ class PreloadConfig extends AbstractConfig
if ($this->configRepo->isConnected()) {
$config = $this->configRepo->get($cat, $key);
if (isset($config)) {
$this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
$this->configCache->set($cat, $key, $config, Cache::SOURCE_DATA);
}
}
}
@ -94,14 +96,14 @@ class PreloadConfig extends AbstractConfig
/**
* {@inheritDoc}
*/
public function set(string $cat, string $key, $value): bool
public function set(string $cat, string $key, $value, bool $autosave = true): bool
{
if (!$this->config_loaded) {
$this->load();
}
// set the cache first
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DATA);
// If there is no connected adapter, we're finished
if (!$this->configRepo->isConnected()) {
@ -110,13 +112,17 @@ class PreloadConfig extends AbstractConfig
$stored = $this->configRepo->set($cat, $key, $value);
if ($autosave) {
$this->save();
}
return $cached && $stored;
}
/**
* {@inheritDoc}
*/
public function delete(string $cat, string $key): bool
public function delete(string $cat, string $key, bool $autosave = true): bool
{
if ($this->config_loaded) {
$this->load();
@ -130,6 +136,10 @@ class PreloadConfig extends AbstractConfig
$storeRemoved = $this->configRepo->delete($cat, $key);
if ($autosave) {
$this->save();
}
return $cacheRemoved || $storeRemoved;
}
}

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;
}
}

View file

@ -21,13 +21,13 @@
namespace Friendica\Core\Config\ValueObject;
use Friendica\Core\Config\Util\ConfigFileLoader;
use Friendica\Core\Config\Util\ConfigFileManager;
use ParagonIE\HiddenString\HiddenString;
/**
* The Friendica config cache for the application
* Initial, all *.config.php files are loaded into this cache with the
* ConfigFileLoader ( @see ConfigFileLoader )
* ConfigFileManager ( @see ConfigFileManager )
*/
class Cache
{
@ -35,8 +35,8 @@ class Cache
const SOURCE_STATIC = 0;
/** @var int Indicates that the cache entry is set by file - Low Priority */
const SOURCE_FILE = 1;
/** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
const SOURCE_DB = 2;
/** @var int Indicates that the cache entry is manually set by the application (per admin page/console) - Middle Priority */
const SOURCE_DATA = 2;
/** @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 */
@ -128,6 +128,34 @@ class Cache
return $this->source[$cat][$key] ?? -1;
}
/**
* Returns the whole config array based on the given source type
*
* @param int $source Indicates the source of the config entry
*
* @return array The config array part of the given source
*/
public function getDataBySource(int $source): array
{
$data = [];
$categories = array_keys($this->source);
foreach ($categories as $category) {
if (is_array($this->source[$category])) {
$keys = array_keys($this->source[$category]);
foreach ($keys as $key) {
if ($this->source[$category][$key] === $source) {
$data[$category][$key] = $this->config[$category][$key];
}
}
}
}
return $data;
}
/**
* Sets a value in the config cache. Accepts raw output from the config table
*