mirror of
https://github.com/friendica/friendica
synced 2025-04-26 13:50:11 +00:00
Refactoring Core class structures ...
This commit is contained in:
parent
57b4c008cb
commit
b216317477
130 changed files with 1625 additions and 1397 deletions
|
@ -19,32 +19,35 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config;
|
||||
namespace Friendica\Core\Config\Capability;
|
||||
|
||||
use Friendica\Core\Config\Cache\Cache;
|
||||
use Friendica\Core\Config\Exception\ConfigPersistenceException;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
|
||||
/**
|
||||
* Interface for accessing system wide configurations
|
||||
* Interface for accessing system-wide configurations
|
||||
*/
|
||||
interface IConfig
|
||||
interface IManageConfigValues
|
||||
{
|
||||
|
||||
/**
|
||||
* Loads all configuration values of family into a cached storage.
|
||||
*
|
||||
* All configuration values of the system are stored in the cache ( @param string $cat The category of the configuration value
|
||||
* All configuration values of the system are stored in the cache.
|
||||
*
|
||||
* @param string $cat The category of the configuration value
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
function load(string $cat = 'config');
|
||||
public function load(string $cat = 'config');
|
||||
|
||||
/**
|
||||
* Get a particular user's config variable given the category name
|
||||
* ($cat) and a $key.
|
||||
*
|
||||
* Get a particular config value from the given category ($cat)
|
||||
* and the $key from a cached storage either from the $this->configAdapter
|
||||
* (@see IConfigAdapter) or from the $this->configCache (@see ConfigCache).
|
||||
* and the $key from a cached storage either from the database or from the cache.
|
||||
*
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to query
|
||||
|
@ -52,8 +55,11 @@ interface IConfig
|
|||
* @param boolean $refresh optional, If true the config is loaded from the db and not from the cache (default: false)
|
||||
*
|
||||
* @return mixed Stored value or null if it does not exist
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*
|
||||
*/
|
||||
function get(string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
public function get(string $cat, string $key, $default_value = null, bool $refresh = false);
|
||||
|
||||
/**
|
||||
* Sets a configuration value for system config
|
||||
|
@ -67,26 +73,30 @@ interface IConfig
|
|||
* @param mixed $value The value to store
|
||||
*
|
||||
* @return bool Operation success
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
function set(string $cat, string $key, $value);
|
||||
public function set(string $cat, string $key, $value): bool;
|
||||
|
||||
/**
|
||||
* Deletes the given key from the system configuration.
|
||||
*
|
||||
* Removes the configured value from the stored cache in $this->configCache
|
||||
* (@see ConfigCache) and removes it from the database (@see IConfigAdapter).
|
||||
* Removes the configured value from the stored cache in the cache and removes it from the database.
|
||||
*
|
||||
* @param string $cat The category of the configuration value
|
||||
* @param string $key The configuration key to delete
|
||||
*
|
||||
* @return bool
|
||||
*
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*
|
||||
*/
|
||||
function delete(string $cat, string $key);
|
||||
public function delete(string $cat, string $key): bool;
|
||||
|
||||
/**
|
||||
* Returns the Config Cache
|
||||
*
|
||||
* @return Cache
|
||||
*/
|
||||
function getCache();
|
||||
public function getCache(): Cache;
|
||||
}
|
13
src/Core/Config/Exception/ConfigFileException.php
Normal file
13
src/Core/Config/Exception/ConfigFileException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class ConfigFileException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
13
src/Core/Config/Exception/ConfigPersistenceException.php
Normal file
13
src/Core/Config/Exception/ConfigPersistenceException.php
Normal file
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Exception;
|
||||
|
||||
use Throwable;
|
||||
|
||||
class ConfigPersistenceException extends \RuntimeException
|
||||
{
|
||||
public function __construct($message = "", Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, 500, $previous);
|
||||
}
|
||||
}
|
|
@ -21,13 +21,13 @@
|
|||
|
||||
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;
|
||||
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;
|
||||
|
||||
class ConfigFactory
|
||||
class Config
|
||||
{
|
||||
/**
|
||||
* The key of the $_SERVER variable to override the config directory
|
||||
|
@ -52,11 +52,11 @@ class ConfigFactory
|
|||
|
||||
/**
|
||||
* @param string $basePath The basepath of FRIENDICA
|
||||
* @param array $serer the $_SERVER array
|
||||
* @param array $server The $_SERVER array
|
||||
*
|
||||
* @return \Friendica\Core\Config\Cache\ConfigFileLoader
|
||||
* @return Util\ConfigFileLoader
|
||||
*/
|
||||
public function createConfigFileLoader(string $basePath, array $server = [])
|
||||
public function createConfigFileLoader(string $basePath, array $server = []): Util\ConfigFileLoader
|
||||
{
|
||||
if (!empty($server[self::CONFIG_DIR_ENV]) && is_dir($server[self::CONFIG_DIR_ENV])) {
|
||||
$configDir = $server[self::CONFIG_DIR_ENV];
|
||||
|
@ -65,17 +65,16 @@ class ConfigFactory
|
|||
}
|
||||
$staticDir = $basePath . DIRECTORY_SEPARATOR . self::STATIC_DIR;
|
||||
|
||||
return new ConfigFileLoader($basePath, $configDir, $staticDir);
|
||||
return new Util\ConfigFileLoader($basePath, $configDir, $staticDir);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param \Friendica\Core\Config\Cache\ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
|
||||
* @param Util\ConfigFileLoader $loader The Config Cache loader (INI/config/.htconfig)
|
||||
* @param array $server
|
||||
*
|
||||
* @return Cache
|
||||
*
|
||||
* @throws Exception
|
||||
*/
|
||||
public function createCache(ConfigFileLoader $loader, array $server = [])
|
||||
public function createCache(Util\ConfigFileLoader $loader, array $server = []): Cache
|
||||
{
|
||||
$configCache = new Cache();
|
||||
$loader->setupCache($configCache, $server);
|
||||
|
@ -84,20 +83,19 @@ class ConfigFactory
|
|||
}
|
||||
|
||||
/**
|
||||
* @param \Friendica\Core\Config\Cache\Cache $configCache The config cache of this adapter
|
||||
* @param ConfigModel $configModel The configuration model
|
||||
* @param Cache $configCache The config cache of this adapter
|
||||
* @param Repository\Config $configRepo The configuration repository
|
||||
*
|
||||
* @return Config\IConfig
|
||||
* @return Capability\IManageConfigValues
|
||||
*/
|
||||
public function create(Cache $configCache, ConfigModel $configModel)
|
||||
public function create(Cache $configCache, Repository\Config $configRepo)
|
||||
{
|
||||
if ($configCache->get('system', 'config_adapter') === 'preload') {
|
||||
$configuration = new Config\Type\PreloadConfig($configCache, $configModel);
|
||||
$configuration = new Type\PreloadConfig($configCache, $configRepo);
|
||||
} else {
|
||||
$configuration = new Config\Type\JitConfig($configCache, $configModel);
|
||||
$configuration = new Type\JitConfig($configCache, $configRepo);
|
||||
}
|
||||
|
||||
|
||||
return $configuration;
|
||||
}
|
||||
}
|
|
@ -19,34 +19,35 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Model;
|
||||
namespace Friendica\Core\Config\Repository;
|
||||
|
||||
use Friendica\Core\Config\Exception\ConfigPersistenceException;
|
||||
use Friendica\Core\Config\Util\ValueConversion;
|
||||
use Friendica\Database\Database;
|
||||
|
||||
/**
|
||||
* The Config model backend, which is using the general DB-model backend for configs
|
||||
* The Config Repository, which is using the general DB-model backend for configs
|
||||
*/
|
||||
class Config
|
||||
{
|
||||
/** @var Database */
|
||||
protected $dba;
|
||||
protected $db;
|
||||
|
||||
/**
|
||||
* @param Database $dba The database connection of this model
|
||||
*/
|
||||
public function __construct(Database $dba)
|
||||
public function __construct(Database $db)
|
||||
{
|
||||
$this->dba = $dba;
|
||||
$this->db = $db;
|
||||
}
|
||||
|
||||
protected static $table_name = 'config';
|
||||
|
||||
/**
|
||||
* Checks if the model is currently connected
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function isConnected()
|
||||
public function isConnected(): bool
|
||||
{
|
||||
return $this->dba->isConnected();
|
||||
return $this->db->isConnected();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -56,29 +57,33 @@ class Config
|
|||
*
|
||||
* @return array The config array
|
||||
*
|
||||
* @throws \Exception In case DB calls are invalid
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function load(string $cat = null)
|
||||
public function load(?string $cat = null): array
|
||||
{
|
||||
$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;
|
||||
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);
|
||||
}
|
||||
$this->dba->close($configs);
|
||||
|
||||
return $return;
|
||||
}
|
||||
|
@ -94,7 +99,7 @@ class Config
|
|||
*
|
||||
* @return array|string|null Stored value or null if it does not exist
|
||||
*
|
||||
* @throws \Exception In case DB calls are invalid
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function get(string $cat, string $key)
|
||||
{
|
||||
|
@ -102,14 +107,18 @@ class Config
|
|||
return null;
|
||||
}
|
||||
|
||||
$config = $this->dba->selectFirst('config', ['v'], ['cat' => $cat, 'k' => $key]);
|
||||
if ($this->dba->isResult($config)) {
|
||||
$value = DbaUtils::toConfigValue($config['v']);
|
||||
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;
|
||||
// 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;
|
||||
|
@ -126,9 +135,9 @@ class Config
|
|||
*
|
||||
* @return bool Operation success
|
||||
*
|
||||
* @throws \Exception In case DB calls are invalid
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function set(string $cat, string $key, $value)
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
|
@ -144,11 +153,13 @@ class Config
|
|||
return true;
|
||||
}
|
||||
|
||||
$dbvalue = DbaUtils::toDbValue($value);
|
||||
$dbValue = ValueConversion::toDbValue($value);
|
||||
|
||||
$result = $this->dba->update('config', ['v' => $dbvalue], ['cat' => $cat, 'k' => $key], true);
|
||||
|
||||
return $result;
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -159,14 +170,18 @@ class Config
|
|||
*
|
||||
* @return bool Operation success
|
||||
*
|
||||
* @throws \Exception In case DB calls are invalid
|
||||
* @throws ConfigPersistenceException In case the persistence layer throws errors
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if (!$this->isConnected()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->dba->delete('config', ['cat' => $cat, 'k' => $key]);
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -21,17 +21,17 @@
|
|||
|
||||
namespace Friendica\Core\Config\Type;
|
||||
|
||||
use Friendica\Core\Config\Cache\Cache;
|
||||
use Friendica\Core\Config\IConfig;
|
||||
use Friendica\Model;
|
||||
use Friendica\Core\Config\Repository\Config;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Config\Capability\IManageConfigValues;
|
||||
|
||||
/**
|
||||
* 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)
|
||||
* - The Config-Files (loaded into the FileCache @see Cache)
|
||||
* - The Config-Repository (per Config-Repository @see Config )
|
||||
*/
|
||||
abstract class BaseConfig implements IConfig
|
||||
abstract class AbstractConfig implements IManageConfigValues
|
||||
{
|
||||
/**
|
||||
* @var Cache
|
||||
|
@ -39,24 +39,24 @@ abstract class BaseConfig implements IConfig
|
|||
protected $configCache;
|
||||
|
||||
/**
|
||||
* @var \Friendica\Core\Config\Model\Config
|
||||
* @var Config
|
||||
*/
|
||||
protected $configModel;
|
||||
protected $configRepo;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param \Friendica\Core\Config\Model\Config $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configRepo The configuration repository
|
||||
*/
|
||||
public function __construct(Cache $configCache, \Friendica\Core\Config\Model\Config $configModel)
|
||||
public function __construct(Cache $configCache, Config $configRepo)
|
||||
{
|
||||
$this->configCache = $configCache;
|
||||
$this->configModel = $configModel;
|
||||
$this->configRepo = $configRepo;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getCache()
|
||||
public function getCache(): Cache
|
||||
{
|
||||
return $this->configCache;
|
||||
}
|
|
@ -21,8 +21,8 @@
|
|||
|
||||
namespace Friendica\Core\Config\Type;
|
||||
|
||||
use Friendica\Core\Config\Cache\Cache;
|
||||
use Friendica\Core\Config\Model\Config;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Config\Repository\Config;
|
||||
|
||||
/**
|
||||
* This class implements the Just-In-Time configuration, which will cache
|
||||
|
@ -31,7 +31,7 @@ use Friendica\Core\Config\Model\Config;
|
|||
* Default Configuration type.
|
||||
* Provides the best performance for pages loading few configuration variables.
|
||||
*/
|
||||
class JitConfig extends BaseConfig
|
||||
class JitConfig extends AbstractConfig
|
||||
{
|
||||
/**
|
||||
* @var array Array of already loaded db values (even if there was no value)
|
||||
|
@ -39,12 +39,12 @@ class JitConfig extends BaseConfig
|
|||
private $db_loaded;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Config $configModel)
|
||||
public function __construct(Cache $configCache, Config $configRepo)
|
||||
{
|
||||
parent::__construct($configCache, $configModel);
|
||||
parent::__construct($configCache, $configRepo);
|
||||
$this->db_loaded = [];
|
||||
|
||||
$this->load();
|
||||
|
@ -52,16 +52,15 @@ class JitConfig extends BaseConfig
|
|||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*
|
||||
*/
|
||||
public function load(string $cat = 'config')
|
||||
{
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = $this->configModel->load($cat);
|
||||
$config = $this->configRepo->load($cat);
|
||||
|
||||
if (!empty($config[$cat])) {
|
||||
foreach ($config[$cat] as $key => $value) {
|
||||
|
@ -79,15 +78,14 @@ class JitConfig extends BaseConfig
|
|||
public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
|
||||
{
|
||||
// if the value isn't loaded or refresh is needed, load it to the cache
|
||||
if ($this->configModel->isConnected() &&
|
||||
(empty($this->db_loaded[$cat][$key]) ||
|
||||
$refresh)) {
|
||||
if ($this->configRepo->isConnected() &&
|
||||
(empty($this->db_loaded[$cat][$key]) ||
|
||||
$refresh)) {
|
||||
$dbValue = $this->configRepo->get($cat, $key);
|
||||
|
||||
$dbvalue = $this->configModel->get($cat, $key);
|
||||
|
||||
if (isset($dbvalue)) {
|
||||
$this->configCache->set($cat, $key, $dbvalue, Cache::SOURCE_DB);
|
||||
unset($dbvalue);
|
||||
if (isset($dbValue)) {
|
||||
$this->configCache->set($cat, $key, $dbValue, Cache::SOURCE_DB);
|
||||
unset($dbValue);
|
||||
}
|
||||
|
||||
$this->db_loaded[$cat][$key] = true;
|
||||
|
@ -102,17 +100,17 @@ class JitConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(string $cat, string $key, $value)
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
// set the cache first
|
||||
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
|
||||
|
||||
// If there is no connected adapter, we're finished
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$stored = $this->configModel->set($cat, $key, $value);
|
||||
$stored = $this->configRepo->set($cat, $key, $value);
|
||||
|
||||
$this->db_loaded[$cat][$key] = $stored;
|
||||
|
||||
|
@ -122,7 +120,7 @@ class JitConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
$cacheRemoved = $this->configCache->delete($cat, $key);
|
||||
|
||||
|
@ -130,11 +128,11 @@ class JitConfig extends BaseConfig
|
|||
unset($this->db_loaded[$cat][$key]);
|
||||
}
|
||||
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cacheRemoved;
|
||||
}
|
||||
|
||||
$storeRemoved = $this->configModel->delete($cat, $key);
|
||||
$storeRemoved = $this->configRepo->delete($cat, $key);
|
||||
|
||||
return $cacheRemoved || $storeRemoved;
|
||||
}
|
||||
|
|
|
@ -21,8 +21,8 @@
|
|||
|
||||
namespace Friendica\Core\Config\Type;
|
||||
|
||||
use Friendica\Core\Config\Cache\Cache;
|
||||
use Friendica\Core\Config\Model\Config;
|
||||
use Friendica\Core\Config\ValueObject\Cache;
|
||||
use Friendica\Core\Config\Repository\Config;
|
||||
|
||||
/**
|
||||
* This class implements the preload configuration, which will cache
|
||||
|
@ -30,18 +30,18 @@ use Friendica\Core\Config\Model\Config;
|
|||
*
|
||||
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
|
||||
*/
|
||||
class PreloadConfig extends BaseConfig
|
||||
class PreloadConfig extends AbstractConfig
|
||||
{
|
||||
/** @var bool */
|
||||
private $config_loaded;
|
||||
|
||||
/**
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configModel The configuration model
|
||||
* @param Cache $configCache The configuration cache (based on the config-files)
|
||||
* @param Config $configRepo The configuration model
|
||||
*/
|
||||
public function __construct(Cache $configCache, Config $configModel)
|
||||
public function __construct(Cache $configCache, Config $configRepo)
|
||||
{
|
||||
parent::__construct($configCache, $configModel);
|
||||
parent::__construct($configCache, $configRepo);
|
||||
$this->config_loaded = false;
|
||||
|
||||
$this->load();
|
||||
|
@ -51,7 +51,6 @@ class PreloadConfig extends BaseConfig
|
|||
* {@inheritDoc}
|
||||
*
|
||||
* This loads all config values everytime load is called
|
||||
*
|
||||
*/
|
||||
public function load(string $cat = 'config')
|
||||
{
|
||||
|
@ -61,11 +60,11 @@ class PreloadConfig extends BaseConfig
|
|||
}
|
||||
|
||||
// If not connected, do nothing
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$config = $this->configModel->load();
|
||||
$config = $this->configRepo->load();
|
||||
$this->config_loaded = true;
|
||||
|
||||
// load the whole category out of the DB into the cache
|
||||
|
@ -78,8 +77,8 @@ class PreloadConfig extends BaseConfig
|
|||
public function get(string $cat, string $key, $default_value = null, bool $refresh = false)
|
||||
{
|
||||
if ($refresh) {
|
||||
if ($this->configModel->isConnected()) {
|
||||
$config = $this->configModel->get($cat, $key);
|
||||
if ($this->configRepo->isConnected()) {
|
||||
$config = $this->configRepo->get($cat, $key);
|
||||
if (isset($config)) {
|
||||
$this->configCache->set($cat, $key, $config, Cache::SOURCE_DB);
|
||||
}
|
||||
|
@ -95,7 +94,7 @@ class PreloadConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function set(string $cat, string $key, $value)
|
||||
public function set(string $cat, string $key, $value): bool
|
||||
{
|
||||
if (!$this->config_loaded) {
|
||||
$this->load();
|
||||
|
@ -105,11 +104,11 @@ class PreloadConfig extends BaseConfig
|
|||
$cached = $this->configCache->set($cat, $key, $value, Cache::SOURCE_DB);
|
||||
|
||||
// If there is no connected adapter, we're finished
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cached;
|
||||
}
|
||||
|
||||
$stored = $this->configModel->set($cat, $key, $value);
|
||||
$stored = $this->configRepo->set($cat, $key, $value);
|
||||
|
||||
return $cached && $stored;
|
||||
}
|
||||
|
@ -117,7 +116,7 @@ class PreloadConfig extends BaseConfig
|
|||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if ($this->config_loaded) {
|
||||
$this->load();
|
||||
|
@ -125,26 +124,12 @@ class PreloadConfig extends BaseConfig
|
|||
|
||||
$cacheRemoved = $this->configCache->delete($cat, $key);
|
||||
|
||||
if (!$this->configModel->isConnected()) {
|
||||
if (!$this->configRepo->isConnected()) {
|
||||
return $cacheRemoved;
|
||||
}
|
||||
|
||||
$storeRemoved = $this->configModel->delete($cat, $key);
|
||||
$storeRemoved = $this->configRepo->delete($cat, $key);
|
||||
|
||||
return $cacheRemoved || $storeRemoved;
|
||||
}
|
||||
|
||||
public function testSetDouble()
|
||||
{
|
||||
$this->configModel->shouldReceive('isConnected')
|
||||
->andReturn(true);
|
||||
|
||||
// constructor loading
|
||||
$this->configModel->shouldReceive('load')
|
||||
->with('config')
|
||||
->andReturn(['config' => ['test' => 'it']])
|
||||
->once();
|
||||
|
||||
parent::testSetDouble();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -19,11 +19,11 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Cache;
|
||||
namespace Friendica\Core\Config\Util;
|
||||
|
||||
use Exception;
|
||||
use Friendica\Core\Addon;
|
||||
use Friendica\Core\Config\Cache\Cache;
|
||||
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 )
|
||||
|
@ -91,7 +91,7 @@ class ConfigFileLoader
|
|||
* @param array $server The $_SERVER array
|
||||
* @param bool $raw Setup the raw config format
|
||||
*
|
||||
* @throws Exception
|
||||
* @throws ConfigFileException
|
||||
*/
|
||||
public function setupCache(Cache $config, array $server = [], bool $raw = false)
|
||||
{
|
||||
|
@ -122,9 +122,9 @@ class ConfigFileLoader
|
|||
*
|
||||
* @return array The config array (empty if no config found)
|
||||
*
|
||||
* @throws Exception if the configuration file isn't readable
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
private function loadStaticConfig($name)
|
||||
private function loadStaticConfig(string $name): array
|
||||
{
|
||||
$configName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.config.php';
|
||||
$iniName = $this->staticDir . DIRECTORY_SEPARATOR . $name . '.ini.php';
|
||||
|
@ -143,9 +143,7 @@ class ConfigFileLoader
|
|||
*
|
||||
* @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
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
private function loadCoreConfig(Cache $config)
|
||||
{
|
||||
|
@ -158,8 +156,6 @@ class ConfigFileLoader
|
|||
foreach ($this->getConfigFiles() as $configFile) {
|
||||
$config->load($this->loadConfigFile($configFile), Cache::SOURCE_FILE);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -169,15 +165,15 @@ class ConfigFileLoader
|
|||
*
|
||||
* @return array The config array (empty if no config found)
|
||||
*
|
||||
* @throws Exception if the configuration file isn't readable
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
public function loadAddonConfig($name)
|
||||
public function loadAddonConfig(string $name): array
|
||||
{
|
||||
$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
|
||||
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);
|
||||
|
@ -193,9 +189,9 @@ class ConfigFileLoader
|
|||
*
|
||||
* @return array The config array (empty if no config was found)
|
||||
*
|
||||
* @throws Exception if the configuration file isn't readable
|
||||
* @throws ConfigFileException if the configuration file isn't readable
|
||||
*/
|
||||
public function loadEnvConfig(array $server)
|
||||
public function loadEnvConfig(array $server): array
|
||||
{
|
||||
$filepath = $this->staticDir . DIRECTORY_SEPARATOR . // /var/www/html/static/
|
||||
"env.config.php"; // env.config.php
|
||||
|
@ -224,10 +220,10 @@ class ConfigFileLoader
|
|||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getConfigFiles(bool $ini = false)
|
||||
private function getConfigFiles(bool $ini = false): array
|
||||
{
|
||||
$files = scandir($this->configDir);
|
||||
$found = array();
|
||||
$found = [];
|
||||
|
||||
$filePattern = ($ini ? '*.ini.php' : '*.config.php');
|
||||
|
||||
|
@ -252,7 +248,7 @@ class ConfigFileLoader
|
|||
*
|
||||
* @deprecated since version 2018.09
|
||||
*/
|
||||
private function loadLegacyConfig($name = '')
|
||||
private function loadLegacyConfig(string $name = ''): array
|
||||
{
|
||||
$name = !empty($name) ? $name : self::CONFIG_HTCONFIG;
|
||||
$fullName = $this->baseDir . DIRECTORY_SEPARATOR . '.' . $name . '.php';
|
||||
|
@ -322,17 +318,17 @@ class ConfigFileLoader
|
|||
* @param string $filepath
|
||||
*
|
||||
* @return array The configuration array
|
||||
* @throws Exception
|
||||
* @throws ConfigFileException
|
||||
* @deprecated since version 2018.12
|
||||
*/
|
||||
private function loadINIConfigFile($filepath)
|
||||
private function loadINIConfigFile(string $filepath): array
|
||||
{
|
||||
$contents = include($filepath);
|
||||
|
||||
$config = parse_ini_string($contents, true, INI_SCANNER_TYPED);
|
||||
|
||||
if ($config === false) {
|
||||
throw new Exception('Error parsing INI config file ' . $filepath);
|
||||
throw new ConfigFileException('Error parsing INI config file ' . $filepath);
|
||||
}
|
||||
|
||||
return $config;
|
||||
|
@ -353,14 +349,14 @@ class ConfigFileLoader
|
|||
*
|
||||
* @return array The config array0
|
||||
*
|
||||
* @throws Exception if the config cannot get loaded.
|
||||
* @throws ConfigFileException if the config cannot get loaded.
|
||||
*/
|
||||
private function loadConfigFile($filepath)
|
||||
private function loadConfigFile(string $filepath): array
|
||||
{
|
||||
$config = include($filepath);
|
||||
|
||||
if (!is_array($config)) {
|
||||
throw new Exception('Error loading config file ' . $filepath);
|
||||
throw new ConfigFileException('Error loading config file ' . $filepath);
|
||||
}
|
||||
|
||||
return $config;
|
|
@ -1,8 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Model;
|
||||
namespace Friendica\Core\Config\Util;
|
||||
|
||||
class DbaUtils
|
||||
/**
|
||||
* Util class to help to convert from/to (p)config values
|
||||
*/
|
||||
class ValueConversion
|
||||
{
|
||||
/**
|
||||
* Formats a DB value to a config value
|
||||
|
@ -13,11 +16,11 @@ class DbaUtils
|
|||
*
|
||||
* Keep in mind that there aren't any numeric/integer config values in the database
|
||||
*
|
||||
* @param null|string $value
|
||||
* @param string|null $value
|
||||
*
|
||||
* @return null|array|string
|
||||
*/
|
||||
public static function toConfigValue($value)
|
||||
public static function toConfigValue(?string $value)
|
||||
{
|
||||
if (!isset($value)) {
|
||||
return null;
|
|
@ -19,8 +19,9 @@
|
|||
*
|
||||
*/
|
||||
|
||||
namespace Friendica\Core\Config\Cache;
|
||||
namespace Friendica\Core\Config\ValueObject;
|
||||
|
||||
use Friendica\Core\Config\Util\ConfigFileLoader;
|
||||
use ParagonIE\HiddenString\HiddenString;
|
||||
|
||||
/**
|
||||
|
@ -45,7 +46,7 @@ class Cache
|
|||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $config;
|
||||
private $config = [];
|
||||
|
||||
/**
|
||||
* @var int[][]
|
||||
|
@ -96,16 +97,16 @@ class Cache
|
|||
/**
|
||||
* Gets a value from the config cache.
|
||||
*
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
* @param string $cat Config category
|
||||
* @param string|null $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)
|
||||
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])) {
|
||||
} elseif (!isset($key) && isset($this->config[$cat])) {
|
||||
return $this->config[$cat];
|
||||
} else {
|
||||
return null;
|
||||
|
@ -122,7 +123,7 @@ class Cache
|
|||
*
|
||||
* @return bool True, if the value is set
|
||||
*/
|
||||
public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
|
||||
public function set(string $cat, string $key, $value, int $source = self::SOURCE_DEFAULT): bool
|
||||
{
|
||||
if (!isset($this->config[$cat])) {
|
||||
$this->config[$cat] = [];
|
||||
|
@ -155,7 +156,7 @@ class Cache
|
|||
*
|
||||
* @return bool true, if deleted
|
||||
*/
|
||||
public function delete(string $cat, string $key)
|
||||
public function delete(string $cat, string $key): bool
|
||||
{
|
||||
if (isset($this->config[$cat][$key])) {
|
||||
unset($this->config[$cat][$key]);
|
||||
|
@ -173,9 +174,9 @@ class Cache
|
|||
/**
|
||||
* Returns the whole configuration
|
||||
*
|
||||
* @return array The configuration
|
||||
* @return string[][] The configuration
|
||||
*/
|
||||
public function getAll()
|
||||
public function getAll(): array
|
||||
{
|
||||
return $this->config;
|
||||
}
|
||||
|
@ -183,11 +184,11 @@ class Cache
|
|||
/**
|
||||
* Returns an array with missing categories/Keys
|
||||
*
|
||||
* @param array $config The array to check
|
||||
* @param string[][] $config The array to check
|
||||
*
|
||||
* @return array
|
||||
* @return string[][]
|
||||
*/
|
||||
public function keyDiff(array $config)
|
||||
public function keyDiff(array $config): array
|
||||
{
|
||||
$return = [];
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue