friendica/src/Core/Config/Cache.php

199 lines
4.7 KiB
PHP
Raw Normal View History

2019-02-03 22:22:04 +01:00
<?php
2020-02-09 15:45:36 +01:00
/**
* @copyright Copyright (C) 2020, Friendica
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
2019-02-03 22:22:04 +01:00
namespace Friendica\Core\Config;
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
*/
class Cache
2019-02-03 22:22:04 +01:00
{
/**
* @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
*/
2019-07-14 22:31:53 +02:00
public function __construct(array $config = [], bool $hidePasswordOutput = true)
2019-02-03 22:22:04 +01:00
{
$this->hidePasswordOutput = $hidePasswordOutput;
$this->load($config);
2019-02-03 22:22:04 +01:00
}
/**
* 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
2019-02-03 22:22:04 +01:00
*/
2019-07-14 22:31:53 +02:00
public function load(array $config, bool $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
}
}
}
}
/**
* 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
2019-02-03 22:22:04 +01:00
*/
public function get(string $cat, string $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.
*
2019-07-14 22:31:53 +02:00
* @param string $cat Config category
* @param string $key Config key
* @param mixed $value Default value to set
2019-02-03 22:22:04 +01:00
*/
2019-07-14 22:31:53 +02:00
private function setDefault(string $cat, string $key, $value)
2019-02-03 22:22:04 +01:00
{
2019-07-14 22:31:53 +02:00
if (!isset($this->config[$cat][$key])) {
$this->set($cat, $key, $value);
2019-02-03 22:22:04 +01:00
}
}
/**
* 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
2019-02-03 22:22:04 +01:00
*/
2019-07-14 22:31:53 +02:00
public function set(string $cat, string $key, $value)
2019-02-03 22:22:04 +01:00
{
if (!isset($this->config[$cat])) {
$this->config[$cat] = [];
2019-02-03 22:22:04 +01:00
}
if ($this->hidePasswordOutput &&
2019-06-10 15:39:21 +02:00
$key == 'password' &&
is_string($value)) {
2019-07-14 22:31:53 +02:00
$this->config[$cat][$key] = new HiddenString((string)$value);
} else {
$this->config[$cat][$key] = $value;
}
return true;
2019-02-03 22:22:04 +01:00
}
/**
* Deletes a value from the config cache.
*
* @param string $cat Config category
* @param string $key Config key
*
* @return bool true, if deleted
2019-02-03 22:22:04 +01:00
*/
2019-07-14 22:31:53 +02:00
public function delete(string $cat, string $key)
2019-02-03 22:22:04 +01:00
{
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
}
}
/**
* 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
}