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];
66 public function has($cat, $key = null)
68 return isset($this->config[$cat][$key])
69 && $this->config[$cat][$key] !== '!<unset>!';
73 * Sets a default value in the config cache. Ignores already existing keys.
75 * @param string $cat Config category
76 * @param string $k Config key
77 * @param mixed $v Default value to set
79 private function setDefault($cat, $k, $v)
81 if (!isset($this->config[$cat][$k])) {
82 $this->set($cat, $k, $v);
89 public function set($cat, $key, $value)
91 // Only arrays are serialized in database, so we have to unserialize sparingly
92 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
94 if (!isset($this->config[$cat])) {
95 $this->config[$cat] = [];
98 $this->config[$cat][$key] = $value;
106 public function hasP($uid, $cat, $key = null)
108 return isset($this->config[$uid][$cat][$key])
109 && $this->config[$uid][$cat][$key] !== '!<unset>!';
115 public function delete($cat, $key)
117 if (isset($this->config[$cat][$key])) {
118 unset($this->config[$cat][$key]);
119 if (count($this->config[$cat]) == 0) {
120 unset($this->config[$cat]);
131 public function loadP($uid, array $config)
133 foreach ($config as $category => $values) {
134 foreach ($values as $key => $value) {
135 $this->setP($uid, $category, $key, $value);
143 public function getP($uid, $cat, $key = null)
145 if (isset($this->config[$uid][$cat][$key])) {
146 return $this->config[$uid][$cat][$key];
155 public function setP($uid, $cat, $key, $value)
157 // Only arrays are serialized in database, so we have to unserialize sparingly
158 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
160 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
161 $this->config[$uid] = [];
164 if (!isset($this->config[$uid][$cat])) {
165 $this->config[$uid][$cat] = [];
168 $this->config[$uid][$cat][$key] = $value;
176 public function deleteP($uid, $cat, $key)
178 if (isset($this->config[$uid][$cat][$key])) {
179 unset($this->config[$uid][$cat][$key]);
180 if (count($this->config[$uid][$cat]) == 0) {
181 unset($this->config[$uid][$cat]);
182 if (count($this->config[$uid]) == 0) {
183 unset($this->config[$uid]);
194 * Returns the whole configuration
196 * @return array The configuration
198 public function getAll()
200 return $this->config;