]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCache.php
updated the credits
[friendica.git] / src / Core / Config / Cache / ConfigCache.php
1 <?php
2
3 namespace Friendica\Core\Config\Cache;
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          * @var array
16          */
17         private $config;
18
19         /**
20          * @param array $config    A initial config array
21          */
22         public function __construct(array $config = [])
23         {
24                 $this->load($config);
25         }
26
27         /**
28          * {@inheritdoc}
29          */
30         public function load(array $config, $overwrite = false)
31         {
32                 $categories = array_keys($config);
33
34                 foreach ($categories as $category) {
35                         if (isset($config[$category]) && is_array($config[$category])) {
36                                 $keys = array_keys($config[$category]);
37
38                                 foreach ($keys as $key) {
39                                         $value = $config[$category][$key];
40                                         if (isset($value) && $value !== '!<unset>!') {
41                                                 if ($overwrite) {
42                                                         $this->set($category, $key, $value);
43                                                 } else {
44                                                         $this->setDefault($category, $key, $value);
45                                                 }
46                                         }
47                                 }
48                         }
49                 }
50         }
51
52         /**
53          * {@inheritdoc}
54          */
55         public function get($cat, $key = null)
56         {
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];
61                 } else {
62                         return '!<unset>!';
63                 }
64         }
65
66         /**
67          * {@inheritdoc}
68          */
69         public function has($cat, $key = null)
70         {
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]));
73         }
74
75         /**
76          * Sets a default value in the config cache. Ignores already existing keys.
77          *
78          * @param string $cat Config category
79          * @param string $k   Config key
80          * @param mixed  $v   Default value to set
81          */
82         private function setDefault($cat, $k, $v)
83         {
84                 if (!isset($this->config[$cat][$k])) {
85                         $this->set($cat, $k, $v);
86                 }
87         }
88
89         /**
90          * {@inheritdoc}
91          */
92         public function set($cat, $key, $value)
93         {
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;
96
97                 if (!isset($this->config[$cat])) {
98                         $this->config[$cat] = [];
99                 }
100
101                 $this->config[$cat][$key] = $value;
102
103                 return true;
104         }
105
106         /**
107          * {@inheritdoc}
108          */
109         public function hasP($uid, $cat, $key = null)
110         {
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]));
113         }
114
115         /**
116          * {@inheritdoc}
117          */
118         public function delete($cat, $key)
119         {
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]);
124                         }
125                         return true;
126                 } else {
127                         return false;
128                 }
129         }
130
131         /**
132          * {@inheritdoc}
133          */
134         public function loadP($uid, array $config)
135         {
136                 $categories = array_keys($config);
137
138                 foreach ($categories as $category) {
139                         if (isset($config[$category]) && is_array($config[$category])) {
140
141                                 $keys = array_keys($config[$category]);
142
143                                 foreach ($keys as $key) {
144                                         $value = $config[$category][$key];
145                                         if (isset($value) && $value !== '!<unset>!') {
146                                                 $this->setP($uid, $category, $key, $value);
147                                         }
148                                 }
149                         }
150                 }
151         }
152
153         /**
154          * {@inheritdoc}
155          */
156         public function getP($uid, $cat, $key = null)
157         {
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];
162                 } else {
163                         return '!<unset>!';
164                 }
165         }
166
167         /**
168          * {@inheritdoc}
169          */
170         public function setP($uid, $cat, $key, $value)
171         {
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;
174
175                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
176                         $this->config[$uid] = [];
177                 }
178
179                 if (!isset($this->config[$uid][$cat])) {
180                         $this->config[$uid][$cat] = [];
181                 }
182
183                 $this->config[$uid][$cat][$key] = $value;
184
185                 return true;
186         }
187
188         /**
189          * {@inheritdoc}
190          */
191         public function deleteP($uid, $cat, $key)
192         {
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]);
199                                 }
200                         }
201
202                         return true;
203                 } else {
204                         return false;
205                 }
206         }
207
208         /**
209          * Returns the whole configuration
210          *
211          * @return array The configuration
212          */
213         public function getAll()
214         {
215                 return $this->config;
216         }
217 }