3 * @copyright Copyright (C) 2010-2022, 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 a default value - Lowest Priority */
35 const SOURCE_STATIC = 0;
36 /** @var int Indicates that the cache entry is set by file - Low Priority */
37 const SOURCE_FILE = 1;
38 /** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
40 /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
42 /** @var int Indicates that the cache entry is fixed and must not be changed */
45 /** @var int Default value for a config source */
46 const SOURCE_DEFAULT = self::SOURCE_FILE;
61 private $hidePasswordOutput;
64 * @param array $config A initial config array
65 * @param bool $hidePasswordOutput True, if cache variables should take extra care of password values
66 * @param int $source Sets a source of the initial config values
68 public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
70 $this->hidePasswordOutput = $hidePasswordOutput;
71 $this->load($config, $source);
75 * Tries to load the specified configuration array into the config array.
76 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
78 * @param array $config
79 * @param int $source Indicates the source of the config entry
81 public function load(array $config, int $source = self::SOURCE_DEFAULT)
83 $categories = array_keys($config);
85 foreach ($categories as $category) {
86 if (is_array($config[$category])) {
87 $keys = array_keys($config[$category]);
89 foreach ($keys as $key) {
90 $value = $config[$category][$key];
92 $this->set($category, $key, $value, $source);
100 * Gets a value from the config cache.
102 * @param string $cat Config category
103 * @param string|null $key Config key
105 * @return null|mixed Returns the value of the Config entry or null if not set
107 public function get(string $cat, ?string $key = null)
109 if (isset($this->config[$cat][$key])) {
110 return $this->config[$cat][$key];
111 } elseif (!isset($key) && isset($this->config[$cat])) {
112 return $this->config[$cat];
119 * Returns the source value of the current, cached config value
121 * @param string $cat Config category
122 * @param string $key Config key
126 public function getSource(string $cat, string $key): int
128 return $this->source[$cat][$key] ?? -1;
132 * Sets a value in the config cache. Accepts raw output from the config table
134 * @param string $cat Config category
135 * @param string $key Config key
136 * @param mixed $value Value to set
137 * @param int $source The source of the current config key
139 * @return bool True, if the value is set
141 public function set(string $cat, string $key, $value, int $source = self::SOURCE_DEFAULT): bool
143 if (!isset($this->config[$cat])) {
144 $this->config[$cat] = [];
145 $this->source[$cat] = [];
148 if (isset($this->source[$cat][$key]) &&
149 $source < $this->source[$cat][$key]) {
153 if ($this->hidePasswordOutput &&
154 $key == 'password' &&
156 $this->config[$cat][$key] = new HiddenString((string)$value);
158 $this->config[$cat][$key] = $value;
161 $this->source[$cat][$key] = $source;
167 * Deletes a value from the config cache.
169 * @param string $cat Config category
170 * @param string $key Config key
172 * @return bool true, if deleted
174 public function delete(string $cat, string $key): bool
176 if (isset($this->config[$cat][$key])) {
177 unset($this->config[$cat][$key]);
178 unset($this->source[$cat][$key]);
179 if (count($this->config[$cat]) == 0) {
180 unset($this->config[$cat]);
181 unset($this->source[$cat]);
190 * Returns the whole configuration
192 * @return string[][] The configuration
194 public function getAll(): array
196 return $this->config;
200 * Returns an array with missing categories/Keys
202 * @param string[][] $config The array to check
206 public function keyDiff(array $config): array
210 $categories = array_keys($config);
212 foreach ($categories as $category) {
213 if (is_array($config[$category])) {
214 $keys = array_keys($config[$category]);
216 foreach ($keys as $key) {
217 if (!isset($this->config[$category][$key])) {
218 $return[$category][$key] = $config[$category][$key];