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 * ConfigCacheLoader ( @see ConfigCacheLoader )
10 * Is used for further caching operations too (depending on the ConfigAdapter )
12 class ConfigCache implements IConfigCache, IPConfigCache
20 * @param array $config A initial config array
22 public function __construct(array $config = [])
30 public function load(array $config, $overwrite = false)
32 $categories = array_keys($config);
34 foreach ($categories as $category) {
35 if (isset($config[$category]) && is_array($config[$category])) {
36 $keys = array_keys($config[$category]);
38 foreach ($keys as $key) {
39 if (isset($config[$category][$key])) {
41 $this->set($category, $key, $config[$category][$key]);
43 $this->setDefault($category, $key, $config[$category][$key]);
54 public function get($cat, $key = null)
56 if (isset($this->config[$cat][$key])) {
57 return $this->config[$cat][$key];
58 } elseif ($key == null && isset($this->config[$cat])) {
59 return $this->config[$cat];
68 public function has($cat, $key = null)
70 return (isset($this->config[$cat][$key]) && $this->config[$cat][$key] !== '!<unset>!') ||
71 ($key == null && isset($this->config[$cat]) && $this->config[$cat] !== '!<unset>!' && is_array($this->config[$cat]));
75 * Sets a default value in the config cache. Ignores already existing keys.
77 * @param string $cat Config category
78 * @param string $k Config key
79 * @param mixed $v Default value to set
81 private function setDefault($cat, $k, $v)
83 if (!isset($this->config[$cat][$k])) {
84 $this->set($cat, $k, $v);
91 public function set($cat, $key, $value)
93 // Only arrays are serialized in database, so we have to unserialize sparingly
94 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
96 if (!isset($this->config[$cat])) {
97 $this->config[$cat] = [];
100 $this->config[$cat][$key] = $value;
108 public function hasP($uid, $cat, $key = null)
110 return (isset($this->config[$uid][$cat][$key]) && $this->config[$uid][$cat][$key] !== '!<unset>!') ||
111 ($key == null && isset($this->config[$uid][$cat]) && $this->config[$uid][$cat] !== '!<unset>!' && is_array($this->config[$uid][$cat]));
117 public function delete($cat, $key)
119 if (isset($this->config[$cat][$key])) {
120 unset($this->config[$cat][$key]);
121 if (count($this->config[$cat]) == 0) {
122 unset($this->config[$cat]);
133 public function loadP($uid, array $config)
135 foreach ($config as $category => $values) {
136 foreach ($values as $key => $value) {
137 $this->setP($uid, $category, $key, $value);
145 public function getP($uid, $cat, $key = null)
147 if (isset($this->config[$uid][$cat][$key])) {
148 return $this->config[$uid][$cat][$key];
149 } elseif ($key == null && isset($this->config[$uid][$cat])) {
150 return $this->config[$uid][$cat];
159 public function setP($uid, $cat, $key, $value)
161 // Only arrays are serialized in database, so we have to unserialize sparingly
162 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
164 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
165 $this->config[$uid] = [];
168 if (!isset($this->config[$uid][$cat])) {
169 $this->config[$uid][$cat] = [];
172 $this->config[$uid][$cat][$key] = $value;
180 public function deleteP($uid, $cat, $key)
182 if (isset($this->config[$uid][$cat][$key])) {
183 unset($this->config[$uid][$cat][$key]);
184 if (count($this->config[$uid][$cat]) == 0) {
185 unset($this->config[$uid][$cat]);
186 if (count($this->config[$uid]) == 0) {
187 unset($this->config[$uid]);
198 * Returns the whole configuration
200 * @return array The configuration
202 public function getAll()
204 return $this->config;