3 namespace Friendica\Core\Config\Cache;
6 * The Friendica config cache for the application
7 * Initial, all *.config.php files are loaded into this cache with the
8 * ConfigFileLoader ( @see ConfigFileLoader )
10 class ConfigCache implements IConfigCache, IPConfigCache
18 * @param array $config A initial config array
20 public function __construct(array $config = [])
28 public function load(array $config, $overwrite = false)
30 $categories = array_keys($config);
32 foreach ($categories as $category) {
33 if (is_array($config[$category])) {
34 $keys = array_keys($config[$category]);
36 foreach ($keys as $key) {
37 $value = $config[$category][$key];
40 $this->set($category, $key, $value);
42 $this->setDefault($category, $key, $value);
53 public function get($cat, $key = null)
55 if (isset($this->config[$cat][$key])) {
56 return $this->config[$cat][$key];
57 } elseif (!isset($key) && isset($this->config[$cat])) {
58 return $this->config[$cat];
65 * Sets a default value in the config cache. Ignores already existing keys.
67 * @param string $cat Config category
68 * @param string $k Config key
69 * @param mixed $v Default value to set
71 private function setDefault($cat, $k, $v)
73 if (!isset($this->config[$cat][$k])) {
74 $this->set($cat, $k, $v);
81 public function set($cat, $key, $value)
83 if (!isset($this->config[$cat])) {
84 $this->config[$cat] = [];
87 $this->config[$cat][$key] = $value;
95 public function delete($cat, $key)
97 if (isset($this->config[$cat][$key])) {
98 unset($this->config[$cat][$key]);
99 if (count($this->config[$cat]) == 0) {
100 unset($this->config[$cat]);
111 public function loadP($uid, array $config)
113 $categories = array_keys($config);
115 foreach ($categories as $category) {
116 if (isset($config[$category]) && is_array($config[$category])) {
118 $keys = array_keys($config[$category]);
120 foreach ($keys as $key) {
121 $value = $config[$category][$key];
123 $this->setP($uid, $category, $key, $value);
133 public function getP($uid, $cat, $key = null)
135 if (isset($this->config[$uid][$cat][$key])) {
136 return $this->config[$uid][$cat][$key];
137 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
138 return $this->config[$uid][$cat];
147 public function setP($uid, $cat, $key, $value)
149 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
150 $this->config[$uid] = [];
153 if (!isset($this->config[$uid][$cat])) {
154 $this->config[$uid][$cat] = [];
157 $this->config[$uid][$cat][$key] = $value;
165 public function deleteP($uid, $cat, $key)
167 if (isset($this->config[$uid][$cat][$key])) {
168 unset($this->config[$uid][$cat][$key]);
169 if (count($this->config[$uid][$cat]) == 0) {
170 unset($this->config[$uid][$cat]);
171 if (count($this->config[$uid]) == 0) {
172 unset($this->config[$uid]);
183 * Returns the whole configuration
185 * @return array The configuration
187 public function getAll()
189 return $this->config;
193 * Returns an array with missing categories/Keys
195 * @param array $config The array to check
199 public function keyDiff(array $config)
203 $categories = array_keys($config);
205 foreach ($categories as $category) {
206 if (is_array($config[$category])) {
207 $keys = array_keys($config[$category]);
209 foreach ($keys as $key) {
210 if (!isset($this->config[$category][$key])) {
211 $return[$category][$key] = $config[$category][$key];