mirror of
https://git.friendi.ca/friendica/friendica.git
synced 2025-06-12 02:44:26 +02:00
Restructure (P)Config to follow new paradigm
This commit is contained in:
parent
68046573a4
commit
ab83d0dd27
49 changed files with 368 additions and 331 deletions
210
src/Core/Config/Cache/Cache.php
Normal file
210
src/Core/Config/Cache/Cache.php
Normal file
|
@ -0,0 +1,210 @@
|
|||
<?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 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 )
|
||||
*/
|
||||
class Cache
|
||||
{
|
||||
/** @var int Indicates that the cache entry is set by file - Low Priority */
|
||||
const SOURCE_FILE = 0;
|
||||
/** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
|
||||
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;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
|
||||
/**
|
||||
* @var int[][]
|
||||
*/
|
||||
private $source = [];
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $hidePasswordOutput;
|
||||
|
||||
/**
|
||||
* @param array $config A initial config array
|
||||
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
|
||||
* @param int $source Sets a source of the initial config values
|
||||
*/
|
||||
public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
|
||||
{
|
||||
$this->hidePasswordOutput = $hidePasswordOutput;
|
||||
$this->load($config, $source);
|
||||
}
|
||||
|
||||
/**
|
||||
* Tries to load the specified configuration array into the config array.
|
||||
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
|
||||
*
|
||||
* @param array $config
|
||||
* @param int $source Indicates the source of the config entry
|
||||
*/
|
||||
public function load(array $config, int $source = self::SOURCE_DEFAULT)
|
||||
{
|
||||
$categories = array_keys($config);
|
||||
|
||||
foreach ($categories as $category) {
|
||||
if (is_array($config[$category])) {
|
||||
$keys = array_keys($config[$category]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
$value = $config[$category][$key];
|
||||
if (isset($value)) {
|
||||
$this->set($category, $key, $value, $source);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a value from the config cache.
|
||||
*
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
*
|
||||
* @return null|mixed Returns the value of the Config entry or null if not set
|
||||
*/
|
||||
public function get(string $cat, string $key = null)
|
||||
{
|
||||
if (isset($this->config[$cat][$key])) {
|
||||
return $this->config[$cat][$key];
|
||||
} else if (!isset($key) && isset($this->config[$cat])) {
|
||||
return $this->config[$cat];
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a value in the config cache. Accepts raw output from the config table
|
||||
*
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
* @param mixed $value Value to set
|
||||
* @param int $source The source of the current config key
|
||||
*
|
||||
* @return bool True, if the value is set
|
||||
*/
|
||||
public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
|
||||
{
|
||||
if (!isset($this->config[$cat])) {
|
||||
$this->config[$cat] = [];
|
||||
$this->source[$cat] = [];
|
||||
}
|
||||
|
||||
if (isset($this->source[$cat][$key]) &&
|
||||
$source < $this->source[$cat][$key]) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->hidePasswordOutput &&
|
||||
$key == 'password' &&
|
||||
is_string($value)) {
|
||||
$this->config[$cat][$key] = new HiddenString((string)$value);
|
||||
} else {
|
||||
$this->config[$cat][$key] = $value;
|
||||
}
|
||||
|
||||
$this->source[$cat][$key] = $source;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a value from the config cache.
|
||||
*
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
*
|
||||
* @return bool true, if deleted
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
{
|
||||
if (isset($this->config[$cat][$key])) {
|
||||
unset($this->config[$cat][$key]);
|
||||
unset($this->source[$cat][$key]);
|
||||
if (count($this->config[$cat]) == 0) {
|
||||
unset($this->config[$cat]);
|
||||
unset($this->source[$cat]);
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the whole configuration
|
||||
*
|
||||
* @return array The configuration
|
||||
*/
|
||||
public function getAll()
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an array with missing categories/Keys
|
||||
*
|
||||
* @param array $config The array to check
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function keyDiff(array $config)
|
||||
{
|
||||
$return = [];
|
||||
|
||||
$categories = array_keys($config);
|
||||
|
||||
foreach ($categories as $category) {
|
||||
if (is_array($config[$category])) {
|
||||
$keys = array_keys($config[$category]);
|
||||
|
||||
foreach ($keys as $key) {
|
||||
if (!isset($this->config[$category][$key])) {
|
||||
$return[$category][$key] = $config[$category][$key];
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $return;
|
||||
}
|
||||
}
|
368
src/Core/Config/Cache/ConfigFileLoader.php
Normal file
368
src/Core/Config/Cache/ConfigFileLoader.php
Normal 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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue