3 namespace Friendica\Core\Config;
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 )
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 = [], bool $hidePasswordOutput = true)
30 $this->hidePasswordOutput = $hidePasswordOutput;
35 * Tries to load the specified configuration array into the config array.
36 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
38 * @param array $config
39 * @param bool $overwrite Force value overwrite if the config key already exists
41 public function load(array $config, bool $overwrite = false)
43 $categories = array_keys($config);
45 foreach ($categories as $category) {
46 if (is_array($config[$category])) {
47 $keys = array_keys($config[$category]);
49 foreach ($keys as $key) {
50 $value = $config[$category][$key];
53 $this->set($category, $key, $value);
55 $this->setDefault($category, $key, $value);
64 * Gets a value from the config cache.
66 * @param string $cat Config category
67 * @param string $key Config key
69 * @return null|mixed Returns the value of the Config entry or null if not set
71 public function get(string $cat, string $key = null)
73 if (isset($this->config[$cat][$key])) {
74 return $this->config[$cat][$key];
75 } elseif (!isset($key) && isset($this->config[$cat])) {
76 return $this->config[$cat];
83 * Sets a default value in the config cache. Ignores already existing keys.
85 * @param string $cat Config category
86 * @param string $key Config key
87 * @param mixed $value Default value to set
89 private function setDefault(string $cat, string $key, $value)
91 if (!isset($this->config[$cat][$key])) {
92 $this->set($cat, $key, $value);
97 * Sets a value in the config cache. Accepts raw output from the config table
99 * @param string $cat Config category
100 * @param string $key Config key
101 * @param mixed $value Value to set
103 * @return bool True, if the value is set
105 public function set(string $cat, string $key, $value)
107 if (!isset($this->config[$cat])) {
108 $this->config[$cat] = [];
111 if ($this->hidePasswordOutput &&
112 $key == 'password' &&
114 $this->config[$cat][$key] = new HiddenString((string)$value);
116 $this->config[$cat][$key] = $value;
122 * Deletes a value from the config cache.
124 * @param string $cat Config category
125 * @param string $key Config key
127 * @return bool true, if deleted
129 public function delete(string $cat, string $key)
131 if (isset($this->config[$cat][$key])) {
132 unset($this->config[$cat][$key]);
133 if (count($this->config[$cat]) == 0) {
134 unset($this->config[$cat]);
143 * Returns the whole configuration
145 * @return array The configuration
147 public function getAll()
149 return $this->config;
153 * Returns an array with missing categories/Keys
155 * @param array $config The array to check
159 public function keyDiff(array $config)
163 $categories = array_keys($config);
165 foreach ($categories as $category) {
166 if (is_array($config[$category])) {
167 $keys = array_keys($config[$category]);
169 foreach ($keys as $key) {
170 if (!isset($this->config[$category][$key])) {
171 $return[$category][$key] = $config[$category][$key];