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
17 * @param array $config A initial config array
19 public function __construct(array $config = [])
21 $this->config = $config;
25 * Tries to load the specified configuration array into the App->config array.
26 * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
28 * @param array $config
29 * @param bool $overwrite Force value overwrite if the config key already exists
31 public function loadConfigArray(array $config, $overwrite = false)
33 foreach ($config as $category => $values) {
34 foreach ($values as $key => $value) {
36 $this->set($category, $key, $value);
38 $this->setDefault($category, $key, $value);
47 public function get($cat, $key = null, $default = null)
51 if ($cat === 'config') {
52 if (isset($this->config[$key])) {
53 $return = $this->config[$key];
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];
67 * Sets a default value in the config cache. Ignores already existing keys.
69 * @param string $cat Config category
70 * @param string $k Config key
71 * @param mixed $v Default value to set
73 private function setDefault($cat, $k, $v)
75 if (!isset($this->config[$cat][$k])) {
76 $this->set($cat, $k, $v);
83 public function set($cat, $key, $value)
85 // Only arrays are serialized in database, so we have to unserialize sparingly
86 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
88 if ($cat === 'config') {
89 $this->config[$key] = $value;
91 if (!isset($this->config[$cat])) {
92 $this->config[$cat] = [];
95 $this->config[$cat][$key] = $value;
102 public function delete($cat, $key)
104 if ($cat === 'config') {
105 if (isset($this->config[$key])) {
106 unset($this->config[$key]);
109 if (isset($this->config[$cat][$key])) {
110 unset($this->config[$cat][$key]);
118 public function getP($uid, $cat, $key = null, $default = null)
122 if (isset($this->config[$uid][$cat][$key])) {
123 $return = $this->config[$uid][$cat][$key];
124 } elseif ($key === null && isset($this->config[$uid][$cat])) {
125 $return = $this->config[$uid][$cat];
134 public function setP($uid, $cat, $key, $value)
136 // Only arrays are serialized in database, so we have to unserialize sparingly
137 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
139 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
140 $this->config[$uid] = [];
143 if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
144 $this->config[$uid][$cat] = [];
148 $this->config[$uid][$cat] = $value;
150 $this->config[$uid][$cat][$key] = $value;
157 public function deleteP($uid, $cat, $key)
159 if (isset($this->config[$uid][$cat][$key])) {
160 unset($this->config[$uid][$cat][$key]);
165 * Returns the whole configuration
167 * @return array The configuration
169 public function getAll()
171 return $this->config;