Refactoring Core class structures ...

This commit is contained in:
Philipp 2021-10-26 21:44:29 +02:00
parent 57b4c008cb
commit b216317477
No known key found for this signature in database
GPG key ID: 24A7501396EB5432
130 changed files with 1625 additions and 1397 deletions

View file

@ -19,37 +19,34 @@
*
*/
namespace Friendica\Core\PConfig;
namespace Friendica\Core\PConfig\Capability;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\PConfig\ValueObject;
/**
* Interface for accessing user specific configurations
*/
interface IPConfig
interface IManagePersonalConfigValues
{
/**
* Loads all configuration values of a user's config family into a cached storage.
*
* All configuration values of the given user are stored with the $uid in the cache
*
* @param int $uid The user_id
* @param int $uid The user_id
* @param string $cat The category of the configuration value
*
* @return array The loaded config array
* @see Cache
*
*/
function load(int $uid, string $cat = 'config');
public function load(int $uid, string $cat = 'config'): array;
/**
* Get a particular user's config variable given the category name
* ($cat) and a key.
*
* Get a particular user's config value from the given category ($cat)
* and the $key with the $uid from a cached storage either from the $this->configAdapter
* (@see IConfigAdapter) or from the $this->configCache (@see PConfigCache).
* and the $key with the $uid from a cached storage either from the database
* or from the configCache
*
* @param int $uid The user_id
* @param string $cat The category of the configuration value
@ -58,8 +55,9 @@ interface IPConfig
* @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
*
*/
function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false);
public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false);
/**
* Sets a configuration value for a user
@ -76,28 +74,26 @@ interface IPConfig
*
* @return bool Operation success
*/
function set(int $uid, string $cat, string $key, $value);
public function set(int $uid, string $cat, string $key, $value): bool;
/**
* Deletes the given key from the users's configuration.
* Deletes the given key from the users configuration.
*
* Removes the configured value from the stored cache in $this->configCache
* (@see ConfigCache) and removes it from the database (@see IConfigAdapter)
* with the given $uid.
* Removes the configured value from the stored cache and removes it from the database with the given $uid.
*
* @param int $uid The user_id
* @param int $uid The user_id
* @param string $cat The category of the configuration value
* @param string $key The configuration key to delete
*
* @return bool
*/
function delete(int $uid, string $cat, string $key);
public function delete(int $uid, string $cat, string $key): bool;
/**
* Returns the Config Cache
*
* @return \Friendica\Core\PConfig\Cache\Cache
* @return ValueObject\Cache
*/
function getCache();
public function getCache(): ValueObject\Cache;
}

View file

@ -0,0 +1,13 @@
<?php
namespace Friendica\Core\PConfig\Exception;
use Throwable;
class PConfigPersistenceException extends \RuntimeException
{
public function __construct($message = "", Throwable $previous = null)
{
parent::__construct($message, 500, $previous);
}
}

View file

@ -21,26 +21,27 @@
namespace Friendica\Core\PConfig\Factory;
use Friendica\Core\Config\Cache\Cache;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Core\PConfig\Model\PConfig as PConfigModel;
use Friendica\Core\Config\ValueObject\Cache;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
use Friendica\Core\PConfig\Repository;
use Friendica\Core\PConfig\Type;
use Friendica\Core\PConfig\ValueObject;
class PConfigFactory
class PConfig
{
/**
* @param Cache $configCache The config cache
* @param \Friendica\Core\PConfig\Cache\Cache $pConfigCache The personal config cache
* @param PConfigModel $configModel The configuration model
* @param Cache $configCache The config cache
* @param ValueObject\Cache $pConfigCache The personal config cache
* @param Repository\PConfig $configRepo The configuration model
*
* @return IPConfig
* @return IManagePersonalConfigValues
*/
public function create(Cache $configCache, \Friendica\Core\PConfig\Cache\Cache $pConfigCache, PConfigModel $configModel)
public function create(Cache $configCache, ValueObject\Cache $pConfigCache, Repository\PConfig $configRepo): IManagePersonalConfigValues
{
if ($configCache->get('system', 'config_adapter') === 'preload') {
$configuration = new Type\PreloadPConfig($pConfigCache, $configModel);
$configuration = new Type\PreloadPConfig($pConfigCache, $configRepo);
} else {
$configuration = new Type\JitPConfig($pConfigCache, $configModel);
$configuration = new Type\JitPConfig($pConfigCache, $configRepo);
}
return $configuration;

View file

@ -19,9 +19,10 @@
*
*/
namespace Friendica\Core\PConfig\Model;
namespace Friendica\Core\PConfig\Repository;
use Friendica\Core\Config\Model\DbaUtils;
use Friendica\Core\Config\Util\ValueConversion;
use Friendica\Core\PConfig\Exception\PConfigPersistenceException;
use Friendica\Database\Database;
/**
@ -29,15 +30,14 @@ use Friendica\Database\Database;
*/
class PConfig
{
/** @var Database */
protected $dba;
protected static $table_name = 'pconfig';
/**
* @param Database $dba The database connection of this model
*/
public function __construct(Database $dba)
/** @var Database */
protected $db;
public function __construct(Database $db)
{
$this->dba = $dba;
$this->db = $db;
}
/**
@ -45,9 +45,9 @@ class PConfig
*
* @return bool
*/
public function isConnected()
public function isConnected(): bool
{
return $this->dba->isConnected();
return $this->db->isConnected();
}
/**
@ -56,30 +56,35 @@ class PConfig
* @param int $uid The id of the user to load
* @param string|null $cat The category of the configuration values to load
*
* @return array The config array
* @return string[][] The config array
*
* @throws \Exception In case DB calls are invalid
* @throws PConfigPersistenceException In case the persistence layer throws errors
*/
public function load(int $uid, string $cat = null)
public function load(int $uid, ?string $cat = null): array
{
$return = [];
if (empty($cat)) {
$configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['uid' => $uid]);
} else {
$configs = $this->dba->select('pconfig', ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
}
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'], ['uid' => $uid]);
} else {
$configs = $this->db->select(static::$table_name, ['cat', 'v', 'k'], ['cat' => $cat, 'uid' => $uid]);
}
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 PConfigPersistenceException(sprintf('Cannot load config category %s for user %d', $cat, $uid), $exception);
} finally {
$this->db->close($configs);
}
$this->dba->close($configs);
return $return;
}
@ -96,7 +101,7 @@ class PConfig
*
* @return array|string|null Stored value or null if it does not exist
*
* @throws \Exception In case DB calls are invalid
* @throws PConfigPersistenceException In case the persistence layer throws errors
*/
public function get(int $uid, string $cat, string $key)
{
@ -104,14 +109,18 @@ class PConfig
return null;
}
$config = $this->dba->selectFirst('pconfig', ['v'], ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
if ($this->dba->isResult($config)) {
$value = DbaUtils::toConfigValue($config['v']);
try {
$config = $this->db->selectFirst('pconfig', ['v'], ['uid' => $uid, '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 PConfigPersistenceException(sprintf('Cannot get config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
}
return null;
@ -130,9 +139,9 @@ class PConfig
*
* @return bool Operation success
*
* @throws \Exception In case DB calls are invalid
* @throws PConfigPersistenceException In case the persistence layer throws errors
*/
public function set(int $uid, string $cat, string $key, $value)
public function set(int $uid, string $cat, string $key, $value): bool
{
if (!$this->isConnected()) {
return false;
@ -148,11 +157,12 @@ class PConfig
return true;
}
$dbvalue = DbaUtils::toDbValue($value);
$result = $this->dba->update('pconfig', ['v' => $dbvalue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
return $result;
try {
$dbValue = ValueConversion::toDbValue($value);
return $this->db->update(static::$table_name, ['v' => $dbValue], ['uid' => $uid, 'cat' => $cat, 'k' => $key], true);
} catch (\Exception $exception) {
throw new PConfigPersistenceException(sprintf('Cannot set config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
}
}
/**
@ -164,14 +174,18 @@ class PConfig
*
* @return bool Operation success
*
* @throws \Exception In case DB calls are invalid
* @throws PConfigPersistenceException In case the persistence layer throws errors
*/
public function delete(int $uid, string $cat, string $key)
public function delete(int $uid, string $cat, string $key): bool
{
if (!$this->isConnected()) {
return false;
}
return $this->dba->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
try {
return $this->db->delete('pconfig', ['uid' => $uid, 'cat' => $cat, 'k' => $key]);
} catch (\Exception $exception) {
throw new PConfigPersistenceException(sprintf('Cannot delete config value for category %s, key %s and user %d', $cat, $key, $uid), $exception);
}
}
}

View file

@ -21,45 +21,45 @@
namespace Friendica\Core\PConfig\Type;
use Friendica\Core\PConfig\Cache\Cache;
use Friendica\Core\PConfig\IPConfig;
use Friendica\Model;
use Friendica\Core\PConfig\Repository;
use Friendica\Core\PConfig\ValueObject\Cache;
use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
/**
* This class is responsible for the user-specific configuration values in Friendica
* The values are set through the Config-DB-Table (per Config-DB-model @see Model\Config\PConfig)
* The values are set through the Config-DB-Table (per Config-DB-model @see Repository\PConfig)
*
* The configuration cache (@see Cache\PConfigCache) is used for temporary caching of database calls. This will
* The configuration cache (@see Cache) is used for temporary caching of database calls. This will
* increase the performance.
*/
abstract class BasePConfig implements IPConfig
abstract class AbstractPConfigValues implements IManagePersonalConfigValues
{
/**
* @var \Friendica\Core\PConfig\Cache\Cache
* @var Cache
*/
protected $configCache;
/**
* @var \Friendica\Core\PConfig\Model\PConfig
* @var Repository\PConfig
*/
protected $configModel;
/**
* @param \Friendica\Core\PConfig\Cache\Cache $configCache The configuration cache
* @param \Friendica\Core\PConfig\Model\PConfig $configModel The configuration model
* @param Cache $configCache The configuration cache
* @param Repository\PConfig $configRepo The configuration model
*/
public function __construct(Cache $configCache, \Friendica\Core\PConfig\Model\PConfig $configModel)
public function __construct(Cache $configCache, Repository\PConfig $configRepo)
{
$this->configCache = $configCache;
$this->configModel = $configModel;
$this->configModel = $configRepo;
}
/**
* Returns the Config Cache
*
* @return \Friendica\Core\PConfig\Cache\Cache
* @return Cache
*/
public function getCache()
public function getCache(): Cache
{
return $this->configCache;
}

View file

@ -21,8 +21,8 @@
namespace Friendica\Core\PConfig\Type;
use Friendica\Core\PConfig\Cache\Cache;
use Friendica\Model;
use Friendica\Core\PConfig\Repository;
use Friendica\Core\PConfig\ValueObject;
/**
* This class implements the Just-In-Time configuration, which will cache
@ -31,7 +31,7 @@ use Friendica\Model;
* Default Configuration type.
* Provides the best performance for pages loading few configuration variables.
*/
class JitPConfig extends BasePConfig
class JitPConfig extends AbstractPConfigValues
{
/**
* @var array Array of already loaded db values (even if there was no value)
@ -39,12 +39,12 @@ class JitPConfig extends BasePConfig
private $db_loaded;
/**
* @param Cache $configCache The configuration cache
* @param \Friendica\Core\PConfig\Model\PConfig $configModel The configuration model
* @param ValueObject\Cache $configCache The configuration cache
* @param Repository\PConfig $configRepo The configuration model
*/
public function __construct(Cache $configCache, \Friendica\Core\PConfig\Model\PConfig $configModel)
public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
{
parent::__construct($configCache, $configModel);
parent::__construct($configCache, $configRepo);
$this->db_loaded = [];
}
@ -52,11 +52,11 @@ class JitPConfig extends BasePConfig
* {@inheritDoc}
*
*/
public function load(int $uid, string $cat = 'config')
public function load(int $uid, string $cat = 'config'): array
{
// If not connected or no uid, do nothing
if (!$uid || !$this->configModel->isConnected()) {
return;
return [];
}
$config = $this->configModel->load($uid, $cat);
@ -84,14 +84,12 @@ class JitPConfig extends BasePConfig
// if the value isn't loaded or refresh is needed, load it to the cache
if ($this->configModel->isConnected() &&
(empty($this->db_loaded[$uid][$cat][$key]) ||
$refresh)) {
(empty($this->db_loaded[$uid][$cat][$key]) || $refresh)) {
$dbValue = $this->configModel->get($uid, $cat, $key);
$dbvalue = $this->configModel->get($uid, $cat, $key);
if (isset($dbvalue)) {
$this->configCache->set($uid, $cat, $key, $dbvalue);
unset($dbvalue);
if (isset($dbValue)) {
$this->configCache->set($uid, $cat, $key, $dbValue);
unset($dbValue);
}
$this->db_loaded[$uid][$cat][$key] = true;
@ -106,7 +104,7 @@ class JitPConfig extends BasePConfig
/**
* {@inheritDoc}
*/
public function set(int $uid, string $cat, string $key, $value)
public function set(int $uid, string $cat, string $key, $value): bool
{
if (!$uid) {
return false;
@ -130,7 +128,7 @@ class JitPConfig extends BasePConfig
/**
* {@inheritDoc}
*/
public function delete(int $uid, string $cat, string $key)
public function delete(int $uid, string $cat, string $key): bool
{
if (!$uid) {
return false;

View file

@ -21,8 +21,8 @@
namespace Friendica\Core\PConfig\Type;
use Friendica\Core\PConfig\Cache\Cache;
use Friendica\Model;
use Friendica\Core\PConfig\Repository;
use Friendica\Core\PConfig\ValueObject;
/**
* This class implements the preload configuration, which will cache
@ -30,18 +30,18 @@ use Friendica\Model;
*
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
*/
class PreloadPConfig extends BasePConfig
class PreloadPConfig extends AbstractPConfigValues
{
/** @var array */
private $config_loaded;
/**
* @param \Friendica\Core\PConfig\Cache\Cache $configCache The configuration cache
* @param \Friendica\Core\PConfig\Model\PConfig $configModel The configuration model
* @param ValueObject\Cache $configCache The configuration cache
* @param Repository\PConfig $configRepo The configuration model
*/
public function __construct(Cache $configCache, \Friendica\Core\PConfig\Model\PConfig $configModel)
public function __construct(ValueObject\Cache $configCache, Repository\PConfig $configRepo)
{
parent::__construct($configCache, $configModel);
parent::__construct($configCache, $configRepo);
$this->config_loaded = [];
}
@ -51,16 +51,16 @@ class PreloadPConfig extends BasePConfig
* This loads all config values everytime load is called
*
*/
public function load(int $uid, string $cat = 'config')
public function load(int $uid, string $cat = 'config'): array
{
// Don't load the whole configuration twice or with invalid uid
if (!$uid || !empty($this->config_loaded[$uid])) {
return;
return [];
}
// If not connected, do nothing
if (!$this->configModel->isConnected()) {
return;
return [];
}
$config = $this->configModel->load($uid);
@ -101,7 +101,7 @@ class PreloadPConfig extends BasePConfig
/**
* {@inheritDoc}
*/
public function set(int $uid, string $cat, string $key, $value)
public function set(int $uid, string $cat, string $key, $value): bool
{
if (!$uid) {
return false;
@ -127,7 +127,7 @@ class PreloadPConfig extends BasePConfig
/**
* {@inheritDoc}
*/
public function delete(int $uid, string $cat, string $key)
public function delete(int $uid, string $cat, string $key): bool
{
if (!$uid) {
return false;

View file

@ -19,7 +19,7 @@
*
*/
namespace Friendica\Core\PConfig\Cache;
namespace Friendica\Core\PConfig\ValueObject;
use ParagonIE\HiddenString\HiddenString;
@ -31,7 +31,7 @@ class Cache
/**
* @var array
*/
private $config;
private $config = [];
/**
* @var bool
@ -53,7 +53,7 @@ class Cache
* @param int $uid
* @param array $config
*/
public function load($uid, array $config)
public function load(int $uid, array $config)
{
if (!is_int($uid)) {
return;
@ -63,7 +63,6 @@ class Cache
foreach ($categories as $category) {
if (isset($config[$category]) && is_array($config[$category])) {
$keys = array_keys($config[$category]);
foreach ($keys as $key) {
@ -81,11 +80,11 @@ class Cache
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $key Config key
* @param string|null $key Config key
*
* @return null|string The value of the config entry or null if not set
* @return null|mixed The value of the config entry or null if not set
*/
public function get($uid, string $cat, string $key = null)
public function get(int $uid, string $cat, ?string $key = null)
{
if (!is_int($uid)) {
return null;
@ -112,7 +111,7 @@ class Cache
*
* @return bool Set successful
*/
public function set($uid, string $cat, string $key, $value)
public function set(int $uid, string $cat, string $key, $value): bool
{
if (!is_int($uid)) {
return false;
@ -127,8 +126,8 @@ class Cache
}
if ($this->hidePasswordOutput &&
$key == 'password' &&
!empty($value) && is_string($value)) {
$key == 'password' &&
!empty($value) && is_string($value)) {
$this->config[$uid][$cat][$key] = new HiddenString((string)$value);
} else {
$this->config[$uid][$cat][$key] = $value;
@ -147,7 +146,7 @@ class Cache
*
* @return bool true, if deleted
*/
public function delete($uid, string $cat, string $key)
public function delete(int $uid, string $cat, string $key): bool
{
if (!is_int($uid)) {
return false;
@ -171,9 +170,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;
}
@ -181,11 +180,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 = [];