friendica/src/Core/Config/Cache/ConfigCache.php

233 lines
4.5 KiB
PHP
Raw Normal View History

2019-02-03 22:22:04 +01:00
<?php
namespace Friendica\Core\Config\Cache;
2019-02-03 22:22:04 +01:00
use ParagonIE\HiddenString\HiddenString;
2019-02-03 23:39:30 +01:00
/**
* The Friendica config cache for the application
* Initial, all *.config.php files are loaded into this cache with the
2019-03-24 12:54:26 +01:00
* ConfigFileLoader ( @see ConfigFileLoader )
2019-02-03 23:39:30 +01:00
*/
2019-02-03 22:22:04 +01:00
class ConfigCache implements IConfigCache, IPConfigCache
{
/**
* @var array
*/
2019-02-05 22:27:57 +01:00
private $config;
2019-02-03 22:22:04 +01:00
/**
* @var bool
*/
private $hidePasswordOutput;
2019-02-03 23:39:30 +01:00
/**
* @param array $config A initial config array
* @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
2019-02-03 23:39:30 +01:00
*/
public function __construct(array $config = [], $hidePasswordOutput = true)
2019-02-03 22:22:04 +01:00
{
$this->hidePasswordOutput = $hidePasswordOutput;
$this->load($config);
2019-02-03 22:22:04 +01:00
}
/**
* {@inheritdoc}
2019-02-03 22:22:04 +01:00
*/
public function load(array $config, $overwrite = false)
2019-02-03 22:22:04 +01:00
{
$categories = array_keys($config);
foreach ($categories as $category) {
2019-03-17 09:57:34 +01:00
if (is_array($config[$category])) {
$keys = array_keys($config[$category]);
foreach ($keys as $key) {
2019-02-18 14:00:34 +01:00
$value = $config[$category][$key];
if (isset($value)) {
if ($overwrite) {
2019-02-18 14:00:34 +01:00
$this->set($category, $key, $value);
} else {
2019-02-18 14:00:34 +01:00
$this->setDefault($category, $key, $value);
}
}
2019-02-03 22:22:04 +01:00
}
}
}
}
/**
* {@inheritdoc}
*/
public function get($cat, $key = null)
2019-02-03 22:22:04 +01:00
{
if (isset($this->config[$cat][$key])) {
return $this->config[$cat][$key];
} elseif (!isset($key) && isset($this->config[$cat])) {
return $this->config[$cat];
2019-02-03 22:22:04 +01:00
} else {
return null;
2019-02-03 22:22:04 +01:00
}
}
2019-02-03 22:22:04 +01:00
/**
* Sets a default value in the config cache. Ignores already existing keys.
*
* @param string $cat Config category
* @param string $k Config key
* @param mixed $v Default value to set
*/
private function setDefault($cat, $k, $v)
{
if (!isset($this->config[$cat][$k])) {
$this->set($cat, $k, $v);
2019-02-03 22:22:04 +01:00
}
}
/**
* {@inheritdoc}
*/
public function set($cat, $key, $value)
{
if (!isset($this->config[$cat])) {
$this->config[$cat] = [];
2019-02-03 22:22:04 +01:00
}
if ($this->hidePasswordOutput &&
$key == 'password') {
$this->config[$cat][$key] = new HiddenString($value);
} else {
$this->config[$cat][$key] = $value;
}
return true;
2019-02-03 22:22:04 +01:00
}
/**
* {@inheritdoc}
*/
public function delete($cat, $key)
{
if (isset($this->config[$cat][$key])) {
unset($this->config[$cat][$key]);
if (count($this->config[$cat]) == 0) {
unset($this->config[$cat]);
2019-02-03 22:22:04 +01:00
}
return true;
2019-02-03 22:22:04 +01:00
} else {
return false;
2019-02-03 22:22:04 +01:00
}
}
/**
* {@inheritdoc}
*/
public function loadP($uid, array $config)
2019-02-03 22:22:04 +01:00
{
2019-02-18 14:00:34 +01:00
$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)) {
2019-02-18 14:00:34 +01:00
$this->setP($uid, $category, $key, $value);
}
}
}
}
}
2019-02-03 22:22:04 +01:00
/**
* {@inheritdoc}
*/
public function getP($uid, $cat, $key = null)
{
2019-02-03 22:22:04 +01:00
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;
2019-02-03 22:22:04 +01:00
}
}
/**
* {@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])) {
2019-02-03 22:22:04 +01:00
$this->config[$uid][$cat] = [];
}
$this->config[$uid][$cat][$key] = $value;
return true;
2019-02-03 22:22:04 +01:00
}
/**
* {@inheritdoc}
*/
public function deleteP($uid, $cat, $key)
{
if (isset($this->config[$uid][$cat][$key])) {
unset($this->config[$uid][$cat][$key]);
2019-02-10 12:30:13 +01:00
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;
2019-02-03 22:22:04 +01:00
}
}
/**
* Returns the whole configuration
*
* @return array The configuration
*/
public function getAll()
{
return $this->config;
}
2019-03-30 18:54:22 +01:00
2019-03-30 19:08:47 +01:00
/**
* Returns an array with missing categories/Keys
*
* @param array $config The array to check
*
* @return array
*/
2019-03-30 18:54:22 +01:00
public function keyDiff(array $config)
2019-03-30 19:08:47 +01:00
{
$return = [];
2019-03-30 18:54:22 +01:00
2019-03-30 19:08:47 +01:00
$categories = array_keys($config);
2019-03-30 18:54:22 +01:00
2019-03-30 19:08:47 +01:00
foreach ($categories as $category) {
if (is_array($config[$category])) {
$keys = array_keys($config[$category]);
2019-03-30 18:54:22 +01:00
2019-03-30 19:08:47 +01:00
foreach ($keys as $key) {
if (!isset($this->config[$category][$key])) {
$return[$category][$key] = $config[$category][$key];
}
}
}
}
2019-03-30 18:54:22 +01:00
2019-03-30 19:08:47 +01:00
return $return;
}
2019-02-03 22:22:04 +01:00
}