Shorten "PConfiguration" to "PConfig" again, since the Wrapper is gone

This commit is contained in:
nupplaPhil 2020-01-19 22:23:44 +01:00
parent cb80108957
commit d5a473abda
No known key found for this signature in database
GPG key ID: D8365C3D36B77D90
41 changed files with 219 additions and 216 deletions

View file

@ -1,6 +1,6 @@
<?php
namespace Friendica\Core\Config\Cache;
namespace Friendica\Core\Config;
use ParagonIE\HiddenString\HiddenString;
@ -9,7 +9,7 @@ use ParagonIE\HiddenString\HiddenString;
* Initial, all *.config.php files are loaded into this cache with the
* ConfigFileLoader ( @see ConfigFileLoader )
*/
class ConfigCache
class Cache
{
/**
* @var array

View file

@ -1,189 +0,0 @@
<?php
namespace Friendica\Core\Config\Cache;
use ParagonIE\HiddenString\HiddenString;
/**
* The Friendica config cache for users
*/
class PConfigCache
{
/**
* @var array
*/
private $config;
/**
* @var bool
*/
private $hidePasswordOutput;
/**
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
*/
public function __construct(bool $hidePasswordOutput = true)
{
$this->hidePasswordOutput = $hidePasswordOutput;
}
/**
* Tries to load the specified configuration array into the user specific config array.
* Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
*
* @param int $uid
* @param array $config
*/
public function load($uid, array $config)
{
if (!is_int($uid)) {
return;
}
$categories = array_keys($config);
foreach ($categories as $category) {
if (isset($config[$category]) && is_array($config[$category])) {
$keys = array_keys($config[$category]);
foreach ($keys as $key) {
$value = $config[$category][$key];
if (isset($value)) {
$this->set($uid, $category, $key, $value);
}
}
}
}
}
/**
* Retrieves a value from the user config cache
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $key Config key
*
* @return null|string The value of the config entry or null if not set
*/
public function get($uid, string $cat, string $key = null)
{
if (!is_int($uid)) {
return null;
}
if (isset($this->config[$uid][$cat][$key])) {
return $this->config[$uid][$cat][$key];
} elseif (!isset($key) && isset($this->config[$uid][$cat])) {
return $this->config[$uid][$cat];
} else {
return null;
}
}
/**
* Sets a value in the user config cache
*
* Accepts raw output from the pconfig table
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $key Config key
* @param mixed $value Value to set
*
* @return bool Set successful
*/
public function set($uid, string $cat, string $key, $value)
{
if (!is_int($uid)) {
return false;
}
if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
$this->config[$uid] = [];
}
if (!isset($this->config[$uid][$cat])) {
$this->config[$uid][$cat] = [];
}
if ($this->hidePasswordOutput &&
$key == 'password' &&
!empty($value) && is_string($value)) {
$this->config[$uid][$cat][$key] = new HiddenString((string)$value);
} else {
$this->config[$uid][$cat][$key] = $value;
}
return true;
}
/**
* Deletes a value from the user config cache
*
* @param int $uid User Id
* @param string $cat Config category
* @param string $key Config key
*
* @return bool true, if deleted
*/
public function delete($uid, string $cat, string $key)
{
if (!is_int($uid)) {
return false;
}
if (isset($this->config[$uid][$cat][$key])) {
unset($this->config[$uid][$cat][$key]);
if (count($this->config[$uid][$cat]) == 0) {
unset($this->config[$uid][$cat]);
if (count($this->config[$uid]) == 0) {
unset($this->config[$uid]);
}
}
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;
}
}

View file

@ -11,9 +11,7 @@ interface IConfig
/**
* Loads all configuration values of family into a cached storage.
*
* All configuration values of the system are stored in the cache ( @see ConfigCache )
*
* @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
*/
@ -67,7 +65,7 @@ interface IConfig
/**
* Returns the Config Cache
*
* @return Cache\ConfigCache
* @return Cache
*/
function getCache();
}

View file

@ -1,82 +0,0 @@
<?php
namespace Friendica\Core\Config;
/**
* Interface for accessing user specific configurations
*/
interface IPConfiguration
{
/**
* 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 string $cat The category of the configuration value
*
* @return void
* @see PConfigCache
*
*/
function load(int $uid, string $cat = 'config');
/**
* 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).
*
* @param int $uid The user_id
* @param string $cat The category of the configuration value
* @param string $key The configuration key to query
* @param mixed $default_value optional, The value to return if key is not set (default: null)
* @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);
/**
* Sets a configuration value for a user
*
* Stores a config value ($value) in the category ($family) under the key ($key)
* for the user_id $uid.
*
* @note Please do not store booleans - convert to 0/1 integer values!
*
* @param int $uid The user_id
* @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
*/
function set(int $uid, string $cat, string $key, $value);
/**
* Deletes the given key from the users's 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.
*
* @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);
/**
* Returns the Config Cache
*
* @return Cache\PConfigCache
*/
function getCache();
}

View file

@ -20,10 +20,10 @@ class JitConfig extends BaseConfig
private $db_loaded;
/**
* @param Cache\ConfigCache $configCache The configuration cache (based on the config-files)
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Model\Config\Config $configModel The configuration model
*/
public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
public function __construct(Cache $configCache, Model\Config\Config $configModel)
{
parent::__construct($configCache, $configModel);
$this->db_loaded = [];

View file

@ -1,131 +0,0 @@
<?php
namespace Friendica\Core\Config;
use Friendica\Model;
/**
* This class implements the Just-In-Time configuration, which will cache
* user config values in a cache, once they are retrieved.
*
* Default Configuration type.
* Provides the best performance for pages loading few configuration variables.
*/
class JitPConfiguration extends PConfiguration
{
/**
* @var array Array of already loaded db values (even if there was no value)
*/
private $db_loaded;
/**
* @param Cache\PConfigCache $configCache The configuration cache
* @param Model\Config\PConfig $configModel The configuration model
*/
public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
{
parent::__construct($configCache, $configModel);
$this->db_loaded = [];
}
/**
* {@inheritDoc}
*
*/
public function load(int $uid, string $cat = 'config')
{
// If not connected or no uid, do nothing
if (!$uid || !$this->configModel->isConnected()) {
return;
}
$config = $this->configModel->load($uid, $cat);
if (!empty($config[$cat])) {
foreach ($config[$cat] as $key => $value) {
$this->db_loaded[$uid][$cat][$key] = true;
}
}
// load the whole category out of the DB into the cache
$this->configCache->load($uid, $config);
}
/**
* {@inheritDoc}
*/
public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
{
if (!$uid) {
return $default_value;
}
// 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)) {
$dbvalue = $this->configModel->get($uid, $cat, $key);
if (isset($dbvalue)) {
$this->configCache->set($uid, $cat, $key, $dbvalue);
unset($dbvalue);
}
$this->db_loaded[$uid][$cat][$key] = true;
}
// use the config cache for return
$result = $this->configCache->get($uid, $cat, $key);
return (isset($result)) ? $result : $default_value;
}
/**
* {@inheritDoc}
*/
public function set(int $uid, string $cat, string $key, $value)
{
if (!$uid) {
return false;
}
// set the cache first
$cached = $this->configCache->set($uid, $cat, $key, $value);
// If there is no connected adapter, we're finished
if (!$this->configModel->isConnected()) {
return $cached;
}
$stored = $this->configModel->set($uid, $cat, $key, $value);
$this->db_loaded[$uid][$cat][$key] = $stored;
return $cached && $stored;
}
/**
* {@inheritDoc}
*/
public function delete(int $uid, string $cat, string $key)
{
if (!$uid) {
return false;
}
$cacheRemoved = $this->configCache->delete($uid, $cat, $key);
if (isset($this->db_loaded[$uid][$cat][$key])) {
unset($this->db_loaded[$uid][$cat][$key]);
}
if (!$this->configModel->isConnected()) {
return $cacheRemoved;
}
$storeRemoved = $this->configModel->delete($uid, $cat, $key);
return $cacheRemoved || $storeRemoved;
}
}

View file

@ -1,45 +0,0 @@
<?php
namespace Friendica\Core\Config;
use Friendica\Model;
/**
* 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 configuration cache (@see Cache\PConfigCache) is used for temporary caching of database calls. This will
* increase the performance.
*/
abstract class PConfiguration implements IPConfiguration
{
/**
* @var Cache\PConfigCache
*/
protected $configCache;
/**
* @var Model\Config\PConfig
*/
protected $configModel;
/**
* @param Cache\PConfigCache $configCache The configuration cache
* @param Model\Config\PConfig $configModel The configuration model
*/
public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
{
$this->configCache = $configCache;
$this->configModel = $configModel;
}
/**
* Returns the Config Cache
*
* @return Cache\PConfigCache
*/
public function getCache()
{
return $this->configCache;
}
}

View file

@ -17,10 +17,10 @@ class PreloadConfig extends BaseConfig
private $config_loaded;
/**
* @param Cache\ConfigCache $configCache The configuration cache (based on the config-files)
* @param Cache $configCache The configuration cache (based on the config-files)
* @param Model\Config\Config $configModel The configuration model
*/
public function __construct(Cache\ConfigCache $configCache, Model\Config\Config $configModel)
public function __construct(Cache $configCache, Model\Config\Config $configModel)
{
parent::__construct($configCache, $configModel);
$this->config_loaded = false;

View file

@ -1,128 +0,0 @@
<?php
namespace Friendica\Core\Config;
use Friendica\Model;
/**
* This class implements the preload configuration, which will cache
* all user config values per call in a cache.
*
* Minimizes the number of database queries to retrieve configuration values at the cost of memory.
*/
class PreloadPConfiguration extends PConfiguration
{
/** @var array */
private $config_loaded;
/**
* @param Cache\PConfigCache $configCache The configuration cache
* @param Model\Config\PConfig $configModel The configuration model
*/
public function __construct(Cache\PConfigCache $configCache, Model\Config\PConfig $configModel)
{
parent::__construct($configCache, $configModel);
$this->config_loaded = [];
}
/**
* {@inheritDoc}
*
* This loads all config values everytime load is called
*
*/
public function load(int $uid, string $cat = 'config')
{
// Don't load the whole configuration twice or with invalid uid
if (!$uid || !empty($this->config_loaded[$uid])) {
return;
}
// If not connected, do nothing
if (!$this->configModel->isConnected()) {
return;
}
$config = $this->configModel->load($uid);
$this->config_loaded[$uid] = true;
// load the whole category out of the DB into the cache
$this->configCache->load($uid, $config);
}
/**
* {@inheritDoc}
*/
public function get(int $uid, string $cat, string $key, $default_value = null, bool $refresh = false)
{
if (!$uid) {
return $default_value;
}
if (empty($this->config_loaded[$uid])) {
$this->load($uid);
} elseif ($refresh) {
if ($this->configModel->isConnected()) {
$config = $this->configModel->get($uid, $cat, $key);
if (isset($config)) {
$this->configCache->set($uid, $cat, $key, $config);
}
}
}
// use the config cache for return
$result = $this->configCache->get($uid, $cat, $key);
return (isset($result)) ? $result : $default_value;
}
/**
* {@inheritDoc}
*/
public function set(int $uid, string $cat, string $key, $value)
{
if (!$uid) {
return false;
}
if (empty($this->config_loaded[$uid])) {
$this->load($uid);
}
// set the cache first
$cached = $this->configCache->set($uid, $cat, $key, $value);
// If there is no connected adapter, we're finished
if (!$this->configModel->isConnected()) {
return $cached;
}
$stored = $this->configModel->set($uid, $cat, $key, $value);
return $cached && $stored;
}
/**
* {@inheritDoc}
*/
public function delete(int $uid, string $cat, string $key)
{
if (!$uid) {
return false;
}
if (empty($this->config_loaded[$uid])) {
$this->load($uid);
}
$cacheRemoved = $this->configCache->delete($uid, $cat, $key);
if (!$this->configModel->isConnected()) {
return $cacheRemoved;
}
$storeRemoved = $this->configModel->delete($uid, $cat, $key);
return $cacheRemoved || $storeRemoved;
}
}