Splitting ConfigCache & PConfigCache
- Remove IConfigCache & IPConfigCache - Add new PConfigCache - Add missing Logger::init() (bugfixing tests)
This commit is contained in:
parent
b56709d802
commit
c82127ffb7
27 changed files with 527 additions and 334 deletions
|
@ -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 implements IConfigCache, IPConfigCache
|
||||
class ConfigCache
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
|
@ -22,7 +22,7 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
|||
private $hidePasswordOutput;
|
||||
|
||||
/**
|
||||
* @param array $config A initial config array
|
||||
* @param array $config A initial config array
|
||||
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
|
||||
*/
|
||||
public function __construct(array $config = [], $hidePasswordOutput = true)
|
||||
|
@ -32,7 +32,11 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* 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 bool $overwrite Force value overwrite if the config key already exists
|
||||
*/
|
||||
public function load(array $config, $overwrite = false)
|
||||
{
|
||||
|
@ -57,7 +61,12 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* 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($cat, $key = null)
|
||||
{
|
||||
|
@ -85,7 +94,13 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* 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
|
||||
*
|
||||
* @return bool True, if the value is set
|
||||
*/
|
||||
public function set($cat, $key, $value)
|
||||
{
|
||||
|
@ -104,7 +119,12 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
|||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
* 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($cat, $key)
|
||||
{
|
||||
|
@ -119,80 +139,6 @@ class ConfigCache implements IConfigCache, IPConfigCache
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function loadP($uid, array $config)
|
||||
{
|
||||
$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->setP($uid, $category, $key, $value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getP($uid, $cat, $key = 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;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function setP($uid, $cat, $key, $value)
|
||||
{
|
||||
if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
|
||||
$this->config[$uid] = [];
|
||||
}
|
||||
|
||||
if (!isset($this->config[$uid][$cat])) {
|
||||
$this->config[$uid][$cat] = [];
|
||||
}
|
||||
|
||||
$this->config[$uid][$cat][$key] = $value;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function deleteP($uid, $cat, $key)
|
||||
{
|
||||
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
|
||||
*
|
||||
|
|
|
@ -1,56 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Cache;
|
||||
|
||||
/**
|
||||
* The interface for a system-wide ConfigCache
|
||||
*/
|
||||
interface IConfigCache
|
||||
{
|
||||
/**
|
||||
* 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 bool $overwrite Force value overwrite if the config key already exists
|
||||
*/
|
||||
function load(array $config, $overwrite = false);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function get($cat, $key = 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
|
||||
*
|
||||
* @return bool True, if the value is set
|
||||
*/
|
||||
function set($cat, $key, $value);
|
||||
|
||||
/**
|
||||
* Deletes a value from the config cache.
|
||||
*
|
||||
* @param string $cat Config category
|
||||
* @param string $key Config key
|
||||
*
|
||||
* @return bool true, if deleted
|
||||
*/
|
||||
function delete($cat, $key);
|
||||
|
||||
/**
|
||||
* Returns the whole configuration cache
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getAll();
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Core\Config\Cache;
|
||||
|
||||
/**
|
||||
* The interface for a user-specific config cache
|
||||
*/
|
||||
interface IPConfigCache
|
||||
{
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function loadP($uid, array $config);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function getP($uid, $cat, $key = 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
|
||||
*/
|
||||
function setP($uid, $cat, $key, $value);
|
||||
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
function deleteP($uid, $cat, $key);
|
||||
|
||||
/**
|
||||
* Returns the whole configuration cache
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
function getAll();
|
||||
}
|
173
src/Core/Config/Cache/PConfigCache.php
Normal file
173
src/Core/Config/Cache/PConfigCache.php
Normal file
|
@ -0,0 +1,173 @@
|
|||
<?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($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)
|
||||
{
|
||||
$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, $cat, $key = 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, $cat, $key, $value)
|
||||
{
|
||||
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, $cat, $key)
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue