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 $value = $config[$category][$key];
40 if (isset($value) && $value !== '!<unset>!') {
42 $this->set($category, $key, $value);
44 $this->setDefault($category, $key, $value);
55 public function get($cat, $key = null)
57 if (isset($this->config[$cat][$key])) {
58 return $this->config[$cat][$key];
59 } elseif ($key == null && isset($this->config[$cat])) {
60 return $this->config[$cat];
69 public function has($cat, $key = null)
71 return (isset($this->config[$cat][$key]) && $this->config[$cat][$key] !== '!<unset>!') ||
72 ($key == null && isset($this->config[$cat]) && $this->config[$cat] !== '!<unset>!' && is_array($this->config[$cat]));
76 * Sets a default value in the config cache. Ignores already existing keys.
78 * @param string $cat Config category
79 * @param string $k Config key
80 * @param mixed $v Default value to set
82 private function setDefault($cat, $k, $v)
84 if (!isset($this->config[$cat][$k])) {
85 $this->set($cat, $k, $v);
92 public function set($cat, $key, $value)
94 // Only arrays are serialized in database, so we have to unserialize sparingly
95 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
97 if (!isset($this->config[$cat])) {
98 $this->config[$cat] = [];
101 $this->config[$cat][$key] = $value;
109 public function hasP($uid, $cat, $key = null)
111 return (isset($this->config[$uid][$cat][$key]) && $this->config[$uid][$cat][$key] !== '!<unset>!') ||
112 ($key == null && isset($this->config[$uid][$cat]) && $this->config[$uid][$cat] !== '!<unset>!' && is_array($this->config[$uid][$cat]));
118 public function delete($cat, $key)
120 if (isset($this->config[$cat][$key])) {
121 unset($this->config[$cat][$key]);
122 if (count($this->config[$cat]) == 0) {
123 unset($this->config[$cat]);
134 public function loadP($uid, array $config)
136 $categories = array_keys($config);
138 foreach ($categories as $category) {
139 if (isset($config[$category]) && is_array($config[$category])) {
141 $keys = array_keys($config[$category]);
143 foreach ($keys as $key) {
144 $value = $config[$category][$key];
145 if (isset($value) && $value !== '!<unset>!') {
146 $this->setP($uid, $category, $key, $value);
156 public function getP($uid, $cat, $key = null)
158 if (isset($this->config[$uid][$cat][$key])) {
159 return $this->config[$uid][$cat][$key];
160 } elseif ($key == null && isset($this->config[$uid][$cat])) {
161 return $this->config[$uid][$cat];
170 public function setP($uid, $cat, $key, $value)
172 // Only arrays are serialized in database, so we have to unserialize sparingly
173 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
175 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
176 $this->config[$uid] = [];
179 if (!isset($this->config[$uid][$cat])) {
180 $this->config[$uid][$cat] = [];
183 $this->config[$uid][$cat][$key] = $value;
191 public function deleteP($uid, $cat, $key)
193 if (isset($this->config[$uid][$cat][$key])) {
194 unset($this->config[$uid][$cat][$key]);
195 if (count($this->config[$uid][$cat]) == 0) {
196 unset($this->config[$uid][$cat]);
197 if (count($this->config[$uid]) == 0) {
198 unset($this->config[$uid]);
209 * Returns the whole configuration
211 * @return array The configuration
213 public function getAll()
215 return $this->config;