]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCache.php
Config FollowUp
[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                                         if (isset($config[$category][$key])) {
40                                                 if ($overwrite) {
41                                                         $this->set($category, $key, $config[$category][$key]);
42                                                 } else {
43                                                         $this->setDefault($category, $key, $config[$category][$key]);
44                                                 }
45                                         }
46                                 }
47                         }
48                 }
49         }
50
51         /**
52          * {@inheritdoc}
53          */
54         public function get($cat, $key = null)
55         {
56                 if (isset($this->config[$cat][$key])) {
57                         return $this->config[$cat][$key];
58                 } else {
59                         return '!<unset>!';
60                 }
61         }
62
63         /**
64          * {@inheritdoc}
65          */
66         public function has($cat, $key = null)
67         {
68                 return isset($this->config[$cat][$key])
69                         && $this->config[$cat][$key] !== '!<unset>!';
70         }
71
72         /**
73          * Sets a default value in the config cache. Ignores already existing keys.
74          *
75          * @param string $cat Config category
76          * @param string $k   Config key
77          * @param mixed  $v   Default value to set
78          */
79         private function setDefault($cat, $k, $v)
80         {
81                 if (!isset($this->config[$cat][$k])) {
82                         $this->set($cat, $k, $v);
83                 }
84         }
85
86         /**
87          * {@inheritdoc}
88          */
89         public function set($cat, $key, $value)
90         {
91                 // Only arrays are serialized in database, so we have to unserialize sparingly
92                 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
93
94                 if (!isset($this->config[$cat])) {
95                         $this->config[$cat] = [];
96                 }
97
98                 $this->config[$cat][$key] = $value;
99
100                 return true;
101         }
102
103         /**
104          * {@inheritdoc}
105          */
106         public function hasP($uid, $cat, $key = null)
107         {
108                 return isset($this->config[$uid][$cat][$key])
109                         && $this->config[$uid][$cat][$key] !== '!<unset>!';
110         }
111
112         /**
113          * {@inheritdoc}
114          */
115         public function delete($cat, $key)
116         {
117                 if (isset($this->config[$cat][$key])) {
118                         unset($this->config[$cat][$key]);
119                         if (count($this->config[$cat]) == 0) {
120                                 unset($this->config[$cat]);
121                         }
122                         return true;
123                 } else {
124                         return false;
125                 }
126         }
127
128         /**
129          * {@inheritdoc}
130          */
131         public function loadP($uid, array $config)
132         {
133                 foreach ($config as $category => $values) {
134                         foreach ($values as $key => $value) {
135                                 $this->setP($uid, $category, $key, $value);
136                         }
137                 }
138         }
139
140         /**
141          * {@inheritdoc}
142          */
143         public function getP($uid, $cat, $key = null)
144         {
145                 if (isset($this->config[$uid][$cat][$key])) {
146                         return $this->config[$uid][$cat][$key];
147                 } else {
148                         return '!<unset>!';
149                 }
150         }
151
152         /**
153          * {@inheritdoc}
154          */
155         public function setP($uid, $cat, $key, $value)
156         {
157                 // Only arrays are serialized in database, so we have to unserialize sparingly
158                 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
159
160                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
161                         $this->config[$uid] = [];
162                 }
163
164                 if (!isset($this->config[$uid][$cat])) {
165                         $this->config[$uid][$cat] = [];
166                 }
167
168                 $this->config[$uid][$cat][$key] = $value;
169
170                 return true;
171         }
172
173         /**
174          * {@inheritdoc}
175          */
176         public function deleteP($uid, $cat, $key)
177         {
178                 if (isset($this->config[$uid][$cat][$key])) {
179                         unset($this->config[$uid][$cat][$key]);
180                         if (count($this->config[$uid][$cat]) == 0) {
181                                 unset($this->config[$uid][$cat]);
182                                 if (count($this->config[$uid]) == 0) {
183                                         unset($this->config[$uid]);
184                                 }
185                         }
186
187                         return true;
188                 } else {
189                         return false;
190                 }
191         }
192
193         /**
194          * Returns the whole configuration
195          *
196          * @return array The configuration
197          */
198         public function getAll()
199         {
200                 return $this->config;
201         }
202 }