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->loadConfigArray($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;
104 public function delete($cat, $key)
106 if ($cat === 'config') {
107 if (isset($this->config[$key])) {
108 unset($this->config[$key]);
111 if (isset($this->config[$cat][$key])) {
112 unset($this->config[$cat][$key]);
113 if (count($this->config[$cat]) == 0) {
114 unset($this->config[$cat]);
123 public function getP($uid, $cat, $key = null, $default = null)
127 if (isset($this->config[$uid][$cat][$key])) {
128 $return = $this->config[$uid][$cat][$key];
129 } elseif ($key === null && isset($this->config[$uid][$cat])) {
130 $return = $this->config[$uid][$cat];
139 public function setP($uid, $cat, $key, $value)
141 // Only arrays are serialized in database, so we have to unserialize sparingly
142 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
144 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
145 $this->config[$uid] = [];
148 if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
149 $this->config[$uid][$cat] = [];
153 $this->config[$uid][$cat] = $value;
155 $this->config[$uid][$cat][$key] = $value;
162 public function deleteP($uid, $cat, $key)
164 if (isset($this->config[$uid][$cat][$key])) {
165 unset($this->config[$uid][$cat][$key]);
166 if (count($this->config[$uid][$cat]) == 0) {
167 unset($this->config[$uid][$cat]);
168 if (count($this->config[$uid]) == 0) {
169 unset($this->config[$uid]);
176 * Returns the whole configuration
178 * @return array The configuration
180 public function getAll()
182 return $this->config;