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 &&
99 $this->config[$cat][$key] = new HiddenString((string) $value);
101 $this->config[$cat][$key] = $value;
109 public function delete($cat, $key)
111 if (isset($this->config[$cat][$key])) {
112 unset($this->config[$cat][$key]);
113 if (count($this->config[$cat]) == 0) {
114 unset($this->config[$cat]);
125 public function loadP($uid, array $config)
127 $categories = array_keys($config);
129 foreach ($categories as $category) {
130 if (isset($config[$category]) && is_array($config[$category])) {
132 $keys = array_keys($config[$category]);
134 foreach ($keys as $key) {
135 $value = $config[$category][$key];
137 $this->setP($uid, $category, $key, $value);
147 public function getP($uid, $cat, $key = null)
149 if (isset($this->config[$uid][$cat][$key])) {
150 return $this->config[$uid][$cat][$key];
151 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
152 return $this->config[$uid][$cat];
161 public function setP($uid, $cat, $key, $value)
163 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
164 $this->config[$uid] = [];
167 if (!isset($this->config[$uid][$cat])) {
168 $this->config[$uid][$cat] = [];
171 $this->config[$uid][$cat][$key] = $value;
179 public function deleteP($uid, $cat, $key)
181 if (isset($this->config[$uid][$cat][$key])) {
182 unset($this->config[$uid][$cat][$key]);
183 if (count($this->config[$uid][$cat]) == 0) {
184 unset($this->config[$uid][$cat]);
185 if (count($this->config[$uid]) == 0) {
186 unset($this->config[$uid]);
197 * Returns the whole configuration
199 * @return array The configuration
201 public function getAll()
203 return $this->config;
207 * Returns an array with missing categories/Keys
209 * @param array $config The array to check
213 public function keyDiff(array $config)
217 $categories = array_keys($config);
219 foreach ($categories as $category) {
220 if (is_array($config[$category])) {
221 $keys = array_keys($config[$category]);
223 foreach ($keys as $key) {
224 if (!isset($this->config[$category][$key])) {
225 $return[$category][$key] = $config[$category][$key];