3 * @copyright Copyright (C) 2010-2021, the Friendica project
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\ValueObject;
24 use Friendica\Core\Config\Util\ConfigFileLoader;
25 use ParagonIE\HiddenString\HiddenString;
28 * The Friendica config cache for the application
29 * Initial, all *.config.php files are loaded into this cache with the
30 * ConfigFileLoader ( @see ConfigFileLoader )
34 /** @var int Indicates that the cache entry is set by file - Low Priority */
35 const SOURCE_FILE = 0;
36 /** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
38 /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
40 /** @var int Indicates that the cache entry is fixed and must not be changed */
43 /** @var int Default value for a config source */
44 const SOURCE_DEFAULT = self::SOURCE_FILE;
59 private $hidePasswordOutput;
62 * @param array $config A initial config array
63 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
64 * @param int $source Sets a source of the initial config values
66 public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
68 $this->hidePasswordOutput = $hidePasswordOutput;
69 $this->load($config, $source);
73 * Tries to load the specified configuration array into the config array.
74 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
76 * @param array $config
77 * @param int $source Indicates the source of the config entry
79 public function load(array $config, int $source = self::SOURCE_DEFAULT)
81 $categories = array_keys($config);
83 foreach ($categories as $category) {
84 if (is_array($config[$category])) {
85 $keys = array_keys($config[$category]);
87 foreach ($keys as $key) {
88 $value = $config[$category][$key];
90 $this->set($category, $key, $value, $source);
98 * Gets a value from the config cache.
100 * @param string $cat Config category
101 * @param string|null $key Config key
103 * @return null|mixed Returns the value of the Config entry or null if not set
105 public function get(string $cat, ?string $key = null)
107 if (isset($this->config[$cat][$key])) {
108 return $this->config[$cat][$key];
109 } elseif (!isset($key) && isset($this->config[$cat])) {
110 return $this->config[$cat];
117 * Sets a value in the config cache. Accepts raw output from the config table
119 * @param string $cat Config category
120 * @param string $key Config key
121 * @param mixed $value Value to set
122 * @param int $source The source of the current config key
124 * @return bool True, if the value is set
126 public function set(string $cat, string $key, $value, int $source = self::SOURCE_DEFAULT): bool
128 if (!isset($this->config[$cat])) {
129 $this->config[$cat] = [];
130 $this->source[$cat] = [];
133 if (isset($this->source[$cat][$key]) &&
134 $source < $this->source[$cat][$key]) {
138 if ($this->hidePasswordOutput &&
139 $key == 'password' &&
141 $this->config[$cat][$key] = new HiddenString((string)$value);
143 $this->config[$cat][$key] = $value;
146 $this->source[$cat][$key] = $source;
152 * Deletes a value from the config cache.
154 * @param string $cat Config category
155 * @param string $key Config key
157 * @return bool true, if deleted
159 public function delete(string $cat, string $key): bool
161 if (isset($this->config[$cat][$key])) {
162 unset($this->config[$cat][$key]);
163 unset($this->source[$cat][$key]);
164 if (count($this->config[$cat]) == 0) {
165 unset($this->config[$cat]);
166 unset($this->source[$cat]);
175 * Returns the whole configuration
177 * @return string[][] The configuration
179 public function getAll(): array
181 return $this->config;
185 * Returns an array with missing categories/Keys
187 * @param string[][] $config The array to check
191 public function keyDiff(array $config): array
195 $categories = array_keys($config);
197 foreach ($categories as $category) {
198 if (is_array($config[$category])) {
199 $keys = array_keys($config[$category]);
201 foreach ($keys as $key) {
202 if (!isset($this->config[$category][$key])) {
203 $return[$category][$key] = $config[$category][$key];