]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ConfigCache.php
355b1df2f5c3d3b56d7b67acd7d4222379d13c03
[friendica.git] / src / Core / Config / ConfigCache.php
1 <?php
2
3 namespace Friendica\Core\Config;
4
5 /**
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 )
9  *
10  * Is used for further caching operations too (depending on the ConfigAdapter )
11  */
12 class ConfigCache implements IConfigCache, IPConfigCache
13 {
14         /**
15          * NEVER, EVER use this public config array outside of the class
16          * It is only public due to backward compatibility to .htconfig.php
17          *
18          * @var array The cached config array
19          */
20         public $config;
21
22         /**
23          * @param array $config    A initial config array
24          */
25         public function __construct($config = [])
26         {
27                 $this->config = [];
28
29                 if (isset($config)) {
30                         $this->loadConfigArray($config, true);
31                 }
32         }
33
34         /**
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.
37          *
38          * @param array $config
39          * @param bool  $overwrite Force value overwrite if the config key already exists
40          */
41         public function loadConfigArray(array $config, $overwrite = false)
42         {
43                 foreach ($config as $category => $values) {
44                         foreach ($values as $key => $value) {
45                                 if ($overwrite) {
46                                         self::set($category, $key, $value);
47                                 } else {
48                                         self::setDefault($category, $key, $value);
49                                 }
50                         }
51                 }
52         }
53
54         /**
55          * {@inheritdoc}
56          */
57         public function get($cat, $key = null, $default = null)
58         {
59                 $return = $default;
60
61                 if ($cat === 'config') {
62                         if (isset($this->config[$key])) {
63                                 $return = $this->config[$key];
64                         }
65                 } else {
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];
70                         }
71                 }
72
73                 return $return;
74         }
75
76         /**
77          * Sets a default value in the config cache. Ignores already existing keys.
78          *
79          * @param string $cat Config category
80          * @param string $k   Config key
81          * @param mixed  $v   Default value to set
82          */
83         private function setDefault($cat, $k, $v)
84         {
85                 if (!isset($this->config[$cat][$k])) {
86                         self::set($cat, $k, $v);
87                 }
88         }
89
90         /**
91          * {@inheritdoc}
92          */
93         public function set($cat, $key, $value)
94         {
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;
97
98                 if ($cat === 'config') {
99                         $this->config[$key] = $value;
100                 } else {
101                         if (!isset($this->config[$cat])) {
102                                 $this->config[$cat] = [];
103                         }
104
105                         $this->config[$cat][$key] = $value;
106                 }
107         }
108
109         /**
110          * {@inheritdoc}
111          */
112         public function delete($cat, $key)
113         {
114                 if ($cat === 'config') {
115                         if (isset($this->config[$key])) {
116                                 unset($this->config[$key]);
117                         }
118                 } else {
119                         if (isset($this->config[$cat][$key])) {
120                                 unset($this->config[$cat][$key]);
121                         }
122                 }
123         }
124
125         /**
126          * {@inheritdoc}
127          */
128         public function getP($uid, $cat, $key = null, $default = null)
129         {
130                 $return = $default;
131
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];
136                 }
137
138                 return $return;
139         }
140
141         /**
142          * {@inheritdoc}
143          */
144         public function setP($uid, $cat, $key, $value)
145         {
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;
148
149                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
150                         $this->config[$uid] = [];
151                 }
152
153                 if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
154                         $this->config[$uid][$cat] = [];
155                 }
156
157                 if ($key === null) {
158                         $this->config[$uid][$cat] = $value;
159                 } else {
160                         $this->config[$uid][$cat][$key] = $value;
161                 }
162         }
163
164         /**
165          * {@inheritdoc}
166          */
167         public function deleteP($uid, $cat, $key)
168         {
169                 if (isset($this->config[$uid][$cat][$key])) {
170                         unset($this->config[$uid][$cat][$key]);
171                 }
172         }
173
174         /**
175          * Returns the whole configuration
176          *
177          * @return array The configuration
178          */
179         public function getAll()
180         {
181                 return $this->config;
182         }
183 }