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