3 namespace Friendica\Core\Config\Cache;
5 use ParagonIE\HiddenString\HiddenString;
8 * The Friendica config cache for the application
9 * Initial, all *.config.php files are loaded into this cache with the
10 * ConfigFileLoader ( @see ConfigFileLoader )
12 class ConfigCache implements IConfigCache, IPConfigCache
22 private $hidePasswordOutput;
25 * @param array $config A initial config array
26 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
28 public function __construct(array $config = [], $hidePasswordOutput = true)
30 $this->hidePasswordOutput = $hidePasswordOutput;
37 public function load(array $config, $overwrite = false)
39 $categories = array_keys($config);
41 foreach ($categories as $category) {
42 if (is_array($config[$category])) {
43 $keys = array_keys($config[$category]);
45 foreach ($keys as $key) {
46 $value = $config[$category][$key];
49 $this->set($category, $key, $value);
51 $this->setDefault($category, $key, $value);
62 public function get($cat, $key = null)
64 if (isset($this->config[$cat][$key])) {
65 return $this->config[$cat][$key];
66 } elseif (!isset($key) && isset($this->config[$cat])) {
67 return $this->config[$cat];
74 * Sets a default value in the config cache. Ignores already existing keys.
76 * @param string $cat Config category
77 * @param string $k Config key
78 * @param mixed $v Default value to set
80 private function setDefault($cat, $k, $v)
82 if (!isset($this->config[$cat][$k])) {
83 $this->set($cat, $k, $v);
90 public function set($cat, $key, $value)
92 if (!isset($this->config[$cat])) {
93 $this->config[$cat] = [];
96 if ($this->hidePasswordOutput &&
98 $this->config[$cat][$key] = new HiddenString($value);
100 $this->config[$cat][$key] = $value;
108 public function delete($cat, $key)
110 if (isset($this->config[$cat][$key])) {
111 unset($this->config[$cat][$key]);
112 if (count($this->config[$cat]) == 0) {
113 unset($this->config[$cat]);
124 public function loadP($uid, array $config)
126 $categories = array_keys($config);
128 foreach ($categories as $category) {
129 if (isset($config[$category]) && is_array($config[$category])) {
131 $keys = array_keys($config[$category]);
133 foreach ($keys as $key) {
134 $value = $config[$category][$key];
136 $this->setP($uid, $category, $key, $value);
146 public function getP($uid, $cat, $key = null)
148 if (isset($this->config[$uid][$cat][$key])) {
149 return $this->config[$uid][$cat][$key];
150 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
151 return $this->config[$uid][$cat];
160 public function setP($uid, $cat, $key, $value)
162 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
163 $this->config[$uid] = [];
166 if (!isset($this->config[$uid][$cat])) {
167 $this->config[$uid][$cat] = [];
170 $this->config[$uid][$cat][$key] = $value;
178 public function deleteP($uid, $cat, $key)
180 if (isset($this->config[$uid][$cat][$key])) {
181 unset($this->config[$uid][$cat][$key]);
182 if (count($this->config[$uid][$cat]) == 0) {
183 unset($this->config[$uid][$cat]);
184 if (count($this->config[$uid]) == 0) {
185 unset($this->config[$uid]);
196 * Returns the whole configuration
198 * @return array The configuration
200 public function getAll()
202 return $this->config;
206 * Returns an array with missing categories/Keys
208 * @param array $config The array to check
212 public function keyDiff(array $config)
216 $categories = array_keys($config);
218 foreach ($categories as $category) {
219 if (is_array($config[$category])) {
220 $keys = array_keys($config[$category]);
222 foreach ($keys as $key) {
223 if (!isset($this->config[$category][$key])) {
224 $return[$category][$key] = $config[$category][$key];