Restructure (P)Config to follow new paradigm

This commit is contained in:
Philipp 2021-10-23 11:29:16 +02:00
parent 68046573a4
commit ab83d0dd27
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
49 changed files with 368 additions and 331 deletions

View file

@ -19,7 +19,7 @@
*
*/
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Cache;
use ParagonIE\HiddenString\HiddenString;

View file

@ -0,0 +1,368 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Config\Cache;
use Exception;
use Friendica\Core\Addon;
use Friendica\Core\Config\Cache\Cache;
/**
* The ConfigFileLoader loads 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
{
/**
* 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
*
* @var string
*/
const CONFIG_HTCONFIG = 'htconfig';
/**
* The sample string inside the configs, which shouldn't get loaded
*
* @var string
*/
const SAMPLE_END = '-sample';
/**
* @var string
*/
private $baseDir;
/**
* @var string
*/
private $configDir;
/**
* @var string
*/
private $staticDir;
/**
* @param string $baseDir The base
* @param string $configDir
* @param string $staticDir
*/
public function __construct(string $baseDir, string $configDir, string $staticDir)
{
$this->baseDir = $baseDir;
$this->configDir = $configDir;
$this->staticDir = $staticDir;
}
/**
* Load the configuration files into an configuration cache
*
* First loads the default value for all the configuration keys, then the legacy configuration files, then the
* 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, array $server = [], bool $raw = false)
{
// Load static config files first, the order is important
$config->load($this->loadStaticConfig('defaults'), Cache::SOURCE_FILE);
$config->load($this->loadStaticConfig('settings'), Cache::SOURCE_FILE);
// try to load the legacy config first
$config->load($this->loadLegacyConfig('htpreconfig'), Cache::SOURCE_FILE);
$config->load($this->loadLegacyConfig('htconfig'), Cache::SOURCE_FILE);
// 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, Cache::SOURCE_FILE);
}
}
/**
* Tries to load the static core-configuration and returns the config array.
*
* @param string $name The name of the configuration
*
* @return array The config array (empty if no config found)
*
* @throws Exception if the configuration file isn't readable
*/
private function loadStaticConfig($name)
{
$configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
$iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
if (file_exists($configName)) {
return $this->loadConfigFile($configName);
} elseif (file_exists($iniName)) {
return $this->loadINIConfigFile($iniName);
} else {
return [];
}
}
/**
* Tries to load the specified core-configuration into the config cache.
*
* @param Cache $config The Config cache
*
* @return array The config array (empty if no config found)
*
* @throws Exception if the configuration file isn't readable
*/
private function loadCoreConfig(Cache $config)
{
// try to load legacy ini-files first
foreach ($this->getConfigFiles(true) as $configFile) {
$config->load($this->loadINIConfigFile($configFile), Cache::SOURCE_FILE);
}
// try to load supported config at last to overwrite it
foreach ($this->getConfigFiles() as $configFile) {
$config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
}
return [];
}
/**
* Tries to load the specified addon-configuration and returns the config array.
*
* @param string $name The name of the configuration
*
* @return array The config array (empty if no config found)
*
* @throws Exception if the configuration file isn't readable
*/
public function loadAddonConfig($name)
{
$filepath = $this->baseDir . DIRECTORY_SEPARATOR . // /var/www/html/
Addon::DIRECTORY . DIRECTORY_SEPARATOR . // addon/
$name . DIRECTORY_SEPARATOR . // openstreetmap/
'config'. DIRECTORY_SEPARATOR . // config/
$name . ".config.php"; // openstreetmap.config.php
if (file_exists($filepath)) {
return $this->loadConfigFile($filepath);
} else {
return [];
}
}
/**
* 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->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/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
*
* @param bool $ini True, if scan for ini-files instead of config files
*
* @return array
*/
private function getConfigFiles(bool $ini = false)
{
$files = scandir($this->configDir);
$found = array();
$filePattern = ($ini ? '*.ini.php' : '*.config.php');
// Don't load sample files
$sampleEnd = self::SAMPLE_END . ($ini ? '.ini.php' : '.config.php');
foreach ($files as $filename) {
if (fnmatch($filePattern, $filename) && substr_compare($filename, $sampleEnd, -strlen($sampleEnd))) {
$found[] = $this->configDir . '/' . $filename;
}
}
return $found;
}
/**
* Tries to load the legacy config files (.htconfig.php, .htpreconfig.php) and returns the config array.
*
* @param string $name The name of the config file (default is empty, which means .htconfig.php)
*
* @return array The configuration array (empty if no config found)
*
* @deprecated since version 2018.09
*/
private function loadLegacyConfig($name = '')
{
$name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
$fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
$config = [];
if (file_exists($fullName)) {
$a = new \stdClass();
$a->config = [];
include $fullName;
$htConfigCategories = array_keys($a->config);
// map the legacy configuration structure to the current structure
foreach ($htConfigCategories as $htConfigCategory) {
if (is_array($a->config[$htConfigCategory])) {
$keys = array_keys($a->config[$htConfigCategory]);
foreach ($keys as $key) {
$config[$htConfigCategory][$key] = $a->config[$htConfigCategory][$key];
}
} else {
$config['config'][$htConfigCategory] = $a->config[$htConfigCategory];
}
}
unset($a);
if (isset($db_host)) {
$config['database']['hostname'] = $db_host;
unset($db_host);
}
if (isset($db_user)) {
$config['database']['username'] = $db_user;
unset($db_user);
}
if (isset($db_pass)) {
$config['database']['password'] = $db_pass;
unset($db_pass);
}
if (isset($db_data)) {
$config['database']['database'] = $db_data;
unset($db_data);
}
if (isset($config['system']['db_charset'])) {
$config['database']['charset'] = $config['system']['db_charset'];
}
if (isset($pidfile)) {
$config['system']['pidfile'] = $pidfile;
unset($pidfile);
}
if (isset($default_timezone)) {
$config['system']['default_timezone'] = $default_timezone;
unset($default_timezone);
}
if (isset($lang)) {
$config['system']['language'] = $lang;
unset($lang);
}
}
return $config;
}
/**
* Tries to load the specified legacy configuration file and returns the config array.
*
* @param string $filepath
*
* @return array The configuration array
* @throws Exception
* @deprecated since version 2018.12
*/
private function loadINIConfigFile($filepath)
{
$contents = include($filepath);
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
if ($config === false) {
throw new Exception('Error parsing INI config file ' . $filepath);
}
return $config;
}
/**
* Tries to load the specified configuration file and returns the config array.
*
* The config format is PHP array and the template for configuration files is the following:
*
* <?php return [
* 'section' => [
* 'key' => 'value',
* ],
* ];
*
* @param string $filepath The filepath of the
*
* @return array The config array0
*
* @throws Exception if the config cannot get loaded.
*/
private function loadConfigFile($filepath)
{
$config = include($filepath);
if (!is_array($config)) {
throw new Exception('Error loading config file ' . $filepath);
}
return $config;
}
}

View file

@ -0,0 +1,103 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Config\Factory;
use Exception;
use Friendica\Core\Config;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\Model\Config as ConfigModel;
use Friendica\Core\Config\Cache\ConfigFileLoader;
class ConfigFactory
{
/**
* The key of the $_SERVER variable to override the config directory
*
* @var string
*/
const CONFIG_DIR_ENV = 'FRIENDICA_CONFIG_DIR';
/**
* The Sub directory of the config-files
*
* @var string
*/
const CONFIG_DIR = 'config';
/**
* The Sub directory of the static config-files
*
* @var string
*/
const STATIC_DIR = 'static';
/**
* @param string $basePath The basepath of FRIENDICA
* @param array $serer the $_SERVER array
*
* @return \Friendica\Core\Config\Cache\ConfigFileLoader
*/
public function createConfigFileLoader(string $basePath, array $server = [])
{
if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
$configDir = $server[self::CONFIG_DIR_ENV];
} else {
$configDir = $basePath . DIRECTORY_SEPARATOR . self::CONFIG_DIR;
}
$staticDir = $basePath . DIRECTORY_SEPARATOR . self::STATIC_DIR;
return new ConfigFileLoader($basePath, $configDir, $staticDir);
}
/**
* @param \Friendica\Core\Config\Cache\ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
*
* @return Cache
*
* @throws Exception
*/
public function createCache(ConfigFileLoader $loader, array $server = [])
{
$configCache = new Cache();
$loader->setupCache($configCache, $server);
return $configCache;
}
/**
* @param \Friendica\Core\Config\Cache\Cache $configCache The config cache of this adapter
* @param ConfigModel $configModel The configuration model
*
* @return Config\IConfig
*/
public function create(Cache $configCache, ConfigModel $configModel)
{
if ($configCache->get('system', 'config_adapter') === 'preload') {
$configuration = new Config\Type\PreloadConfig($configCache, $configModel);
} else {
$configuration = new Config\Type\JitConfig($configCache, $configModel);
}
return $configuration;
}
}

View file

@ -21,6 +21,8 @@
namespace Friendica\Core\Config;
use Friendica\Core\Config\Cache\Cache;
/**
* Interface for accessing system wide configurations
*/

View file

@ -0,0 +1,172 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Config\Model;
use Friendica\Database\Database;
/**
* The Config model backend, which is using the general DB-model backend for configs
*/
class Config
{
/** @var Database */
protected $dba;
/**
* @param Database $dba The database connection of this model
*/
public function __construct(Database $dba)
{
$this->dba = $dba;
}
/**
* Checks if the model is currently connected
*
* @return bool
*/
public function isConnected()
{
return $this->dba->isConnected();
}
/**
* Loads all configuration values and returns the loaded category as an array.
*
* @param string|null $cat The category of the configuration values to load
*
* @return array The config array
*
* @throws \Exception In case DB calls are invalid
*/
public function load(string $cat = null)
{
$return = [];
if (empty($cat)) {
$configs = $this->dba->select('config', ['cat', 'v', 'k']);
} else {
$configs = $this->dba->select('config', ['cat', 'v', 'k'], ['cat' => $cat]);
}
while ($config = $this->dba->fetch($configs)) {
$key = $config['k'];
$value = DbaUtils::toConfigValue($config['v']);
// just save it in case it is set
if (isset($value)) {
$return[$config['cat']][$key] = $value;
}
}
$this->dba->close($configs);
return $return;
}
/**
* Get a particular, system-wide config variable out of the DB with the
* given category name ($cat) and a key ($key).
*
* Note: Boolean variables are defined as 0/1 in the database
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to query
*
* @return array|string|null Stored value or null if it does not exist
*
* @throws \Exception In case DB calls are invalid
*/
public function get(string $cat, string $key)
{
if (!$this->isConnected()) {
return null;
}
$config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
if ($this->dba->isResult($config)) {
$value = DbaUtils::toConfigValue($config['v']);
// just return it in case it is set
if (isset($value)) {
return $value;
}
}
return null;
}
/**
* Stores a config value ($value) in the category ($cat) under the key ($key).
*
* Note: Please do not store booleans - convert to 0/1 integer values!
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to set
* @param mixed $value The value to store
*
* @return bool Operation success
*
* @throws \Exception In case DB calls are invalid
*/
public function set(string $cat, string $key, $value)
{
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 = DbaUtils::toDbValue($value);
$result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
return $result;
}
/**
* Removes the configured value from the database.
*
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
*
* @return bool Operation success
*
* @throws \Exception In case DB calls are invalid
*/
public function delete(string $cat, string $key)
{
if (!$this->isConnected()) {
return false;
}
return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);
}
}

View file

@ -0,0 +1,59 @@
<?php
namespace Friendica\Core\Config\Model;
class DbaUtils
{
/**
* Formats a DB value to a config value
* - null = The db-value isn't set
* - bool = The db-value is either '0' or '1'
* - array = The db-value is a serialized array
* - string = The db-value is a string
*
* Keep in mind that there aren't any numeric/integer config values in the database
*
* @param null|string $value
*
* @return null|array|string
*/
public static function toConfigValue($value)
{
if (!isset($value)) {
return null;
}
switch (true) {
// manage array value
case preg_match("|^a:[0-9]+:{.*}$|s", $value):
return unserialize($value);
default:
return $value;
}
}
/**
* Formats a config value to a DB value (string)
*
* @param mixed $value
*
* @return string
*/
public static function toDbValue($value): string
{
// if not set, save an empty string
if (!isset($value)) {
return '';
}
switch (true) {
// manage arrays
case is_array($value):
return serialize($value);
default:
return (string)$value;
}
}
}

View file

@ -0,0 +1,63 @@
<?php
/**
* @copyright Copyright (C) 2010-2021, the Friendica project
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Core\Config\Type;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\IConfig;
use Friendica\Model;
/**
* This class is responsible for all system-wide configuration values in Friendica
* There are two types of storage
* - The Config-Files (loaded into the FileCache @see ConfigCache)
* - The Config-DB-Table (per Config-DB-model @see Model\Config\Config)
*/
abstract class BaseConfig implements IConfig
{
/**
* @var Cache
*/
protected $configCache;
/**
* @var \Friendica\Core\Config\Model\Config
*/
protected $configModel;
/**
* @param Cache $configCache The configuration cache (based on the config-files)
* @param \Friendica\Core\Config\Model\Config $configModel The configuration model
*/
public function __construct(Cache $configCache, \Friendica\Core\Config\Model\Config $configModel)
{
$this->configCache = $configCache;
$this->configModel = $configModel;
}
/**
* {@inheritDoc}
*/
public function getCache()
{
return $this->configCache;
}
}

View file

@ -19,10 +19,10 @@
*
*/
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Type;
use Friendica\Core\BaseConfig;
use Friendica\Model;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\Model\Config;
/**
* This class implements the Just-In-Time configuration, which will cache
@ -39,10 +39,10 @@ class JitConfig extends BaseConfig
private $db_loaded;
/**
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Model\Config\Config $configModel The configuration model
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configModel The configuration model
*/
public function __construct(Cache $configCache, Model\Config\Config $configModel)
public function __construct(Cache $configCache, Config $configModel)
{
parent::__construct($configCache, $configModel);
$this->db_loaded = [];

View file

@ -19,10 +19,10 @@
*
*/
namespace Friendica\Core\Config;
namespace Friendica\Core\Config\Type;
use Friendica\Core\BaseConfig;
use Friendica\Model;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\Config\Model\Config;
/**
* This class implements the preload configuration, which will cache
@ -36,10 +36,10 @@ class PreloadConfig extends BaseConfig
private $config_loaded;
/**
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Model\Config\Config $configModel The configuration model
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Config $configModel The configuration model
*/
public function __construct(Cache $configCache, Model\Config\Config $configModel)
public function __construct(Cache $configCache, Config $configModel)
{
parent::__construct($configCache, $configModel);
$this->config_loaded = false;