3 namespace Friendica\Core\Config;
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
15 * NEVER, EVER use this public config array outside of the class
16 * It is only public due to backward compatibility to .htconfig.php
18 * @var array The cached config array
23 * @param array $config A initial config array
25 public function __construct($config = [])
30 $this->loadConfigArray($config, true);
35 * Tries to load the specified configuration array into the App->config array.
36 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
38 * @param array $config
39 * @param bool $overwrite Force value overwrite if the config key already exists
41 public function loadConfigArray(array $config, $overwrite = false)
43 foreach ($config as $category => $values) {
44 foreach ($values as $key => $value) {
46 self::set($category, $key, $value);
48 self::setDefault($category, $key, $value);
57 public function get($cat, $key = null, $default = null)
61 if ($cat === 'config') {
62 if (isset($this->config[$key])) {
63 $return = $this->config[$key];
66 if (isset($this->config[$cat][$key])) {
67 $return = $this->config[$cat][$key];
68 } elseif ($key == null && isset($this->config[$cat])) {
69 $return = $this->config[$cat];
77 * Sets a default value in the config cache. Ignores already existing keys.
79 * @param string $cat Config category
80 * @param string $k Config key
81 * @param mixed $v Default value to set
83 private function setDefault($cat, $k, $v)
85 if (!isset($this->config[$cat][$k])) {
86 self::set($cat, $k, $v);
93 public function set($cat, $key, $value)
95 // Only arrays are serialized in database, so we have to unserialize sparingly
96 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
98 if ($cat === 'config') {
99 $this->config[$key] = $value;
101 if (!isset($this->config[$cat])) {
102 $this->config[$cat] = [];
105 $this->config[$cat][$key] = $value;
112 public function delete($cat, $key)
114 if ($cat === 'config') {
115 if (isset($this->config[$key])) {
116 unset($this->config[$key]);
119 if (isset($this->config[$cat][$key])) {
120 unset($this->config[$cat][$key]);
128 public function getP($uid, $cat, $key = null, $default = null)
132 if (isset($this->config[$uid][$cat][$key])) {
133 $return = $this->config[$uid][$cat][$key];
134 } elseif ($key === null && isset($this->config[$uid][$cat])) {
135 $return = $this->config[$uid][$cat];
144 public function setP($uid, $cat, $key, $value)
146 // Only arrays are serialized in database, so we have to unserialize sparingly
147 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
149 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
150 $this->config[$uid] = [];
153 if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
154 $this->config[$uid][$cat] = [];
158 $this->config[$uid][$cat] = $value;
160 $this->config[$uid][$cat][$key] = $value;
167 public function deleteP($uid, $cat, $key)
169 if (isset($this->config[$uid][$cat][$key])) {
170 unset($this->config[$uid][$cat][$key]);
175 * Returns the whole configuration
177 * @return array The configuration
179 public function getAll()
181 return $this->config;