3 * @copyright Copyright (C) 2020, Friendica
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Core\Config;
24 use ParagonIE\HiddenString\HiddenString;
27 * The Friendica config cache for the application
28 * Initial, all *.config.php files are loaded into this cache with the
29 * ConfigFileLoader ( @see ConfigFileLoader )
41 private $hidePasswordOutput;
44 * @param array $config A initial config array
45 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
47 public function __construct(array $config = [], bool $hidePasswordOutput = true)
49 $this->hidePasswordOutput = $hidePasswordOutput;
54 * Tries to load the specified configuration array into the config array.
55 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
57 * @param array $config
58 * @param bool $overwrite Force value overwrite if the config key already exists
60 public function load(array $config, bool $overwrite = false)
62 $categories = array_keys($config);
64 foreach ($categories as $category) {
65 if (is_array($config[$category])) {
66 $keys = array_keys($config[$category]);
68 foreach ($keys as $key) {
69 $value = $config[$category][$key];
72 $this->set($category, $key, $value);
74 $this->setDefault($category, $key, $value);
83 * Gets a value from the config cache.
85 * @param string $cat Config category
86 * @param string $key Config key
88 * @return null|mixed Returns the value of the Config entry or null if not set
90 public function get(string $cat, string $key = null)
92 if (isset($this->config[$cat][$key])) {
93 return $this->config[$cat][$key];
94 } elseif (!isset($key) && isset($this->config[$cat])) {
95 return $this->config[$cat];
102 * Sets a default value in the config cache. Ignores already existing keys.
104 * @param string $cat Config category
105 * @param string $key Config key
106 * @param mixed $value Default value to set
108 private function setDefault(string $cat, string $key, $value)
110 if (!isset($this->config[$cat][$key])) {
111 $this->set($cat, $key, $value);
116 * Sets a value in the config cache. Accepts raw output from the config table
118 * @param string $cat Config category
119 * @param string $key Config key
120 * @param mixed $value Value to set
122 * @return bool True, if the value is set
124 public function set(string $cat, string $key, $value)
126 if (!isset($this->config[$cat])) {
127 $this->config[$cat] = [];
130 if ($this->hidePasswordOutput &&
131 $key == 'password' &&
133 $this->config[$cat][$key] = new HiddenString((string)$value);
135 $this->config[$cat][$key] = $value;
141 * Deletes a value from the config cache.
143 * @param string $cat Config category
144 * @param string $key Config key
146 * @return bool true, if deleted
148 public function delete(string $cat, string $key)
150 if (isset($this->config[$cat][$key])) {
151 unset($this->config[$cat][$key]);
152 if (count($this->config[$cat]) == 0) {
153 unset($this->config[$cat]);
162 * Returns the whole configuration
164 * @return array The configuration
166 public function getAll()
168 return $this->config;
172 * Returns an array with missing categories/Keys
174 * @param array $config The array to check
178 public function keyDiff(array $config)
182 $categories = array_keys($config);
184 foreach ($categories as $category) {
185 if (is_array($config[$category])) {
186 $keys = array_keys($config[$category]);
188 foreach ($keys as $key) {
189 if (!isset($this->config[$category][$key])) {
190 $return[$category][$key] = $config[$category][$key];