]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCache.php
wrong indent
[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  * ConfigFileLoader ( @see ConfigFileLoader )
9  */
10 class ConfigCache implements IConfigCache, IPConfigCache
11 {
12         /**
13          * @var array
14          */
15         private $config;
16
17         /**
18          * @param array $config    A initial config array
19          */
20         public function __construct(array $config = [])
21         {
22                 $this->load($config);
23         }
24
25         /**
26          * {@inheritdoc}
27          */
28         public function load(array $config, $overwrite = false)
29         {
30                 $categories = array_keys($config);
31
32                 foreach ($categories as $category) {
33                         if (is_array($config[$category])) {
34                                 $keys = array_keys($config[$category]);
35
36                                 foreach ($keys as $key) {
37                                         $value = $config[$category][$key];
38                                         if (isset($value)) {
39                                                 if ($overwrite) {
40                                                         $this->set($category, $key, $value);
41                                                 } else {
42                                                         $this->setDefault($category, $key, $value);
43                                                 }
44                                         }
45                                 }
46                         }
47                 }
48         }
49
50         /**
51          * {@inheritdoc}
52          */
53         public function get($cat, $key = null)
54         {
55                 if (isset($this->config[$cat][$key])) {
56                         return $this->config[$cat][$key];
57                 } elseif (!isset($key) && isset($this->config[$cat])) {
58                         return $this->config[$cat];
59                 } else {
60                         return null;
61                 }
62         }
63
64         /**
65          * Sets a default value in the config cache. Ignores already existing keys.
66          *
67          * @param string $cat Config category
68          * @param string $k   Config key
69          * @param mixed  $v   Default value to set
70          */
71         private function setDefault($cat, $k, $v)
72         {
73                 if (!isset($this->config[$cat][$k])) {
74                         $this->set($cat, $k, $v);
75                 }
76         }
77
78         /**
79          * {@inheritdoc}
80          */
81         public function set($cat, $key, $value)
82         {
83                 if (!isset($this->config[$cat])) {
84                         $this->config[$cat] = [];
85                 }
86
87                 $this->config[$cat][$key] = $value;
88
89                 return true;
90         }
91
92         /**
93          * {@inheritdoc}
94          */
95         public function delete($cat, $key)
96         {
97                 if (isset($this->config[$cat][$key])) {
98                         unset($this->config[$cat][$key]);
99                         if (count($this->config[$cat]) == 0) {
100                                 unset($this->config[$cat]);
101                         }
102                         return true;
103                 } else {
104                         return false;
105                 }
106         }
107
108         /**
109          * {@inheritdoc}
110          */
111         public function loadP($uid, array $config)
112         {
113                 $categories = array_keys($config);
114
115                 foreach ($categories as $category) {
116                         if (isset($config[$category]) && is_array($config[$category])) {
117
118                                 $keys = array_keys($config[$category]);
119
120                                 foreach ($keys as $key) {
121                                         $value = $config[$category][$key];
122                                         if (isset($value)) {
123                                                 $this->setP($uid, $category, $key, $value);
124                                         }
125                                 }
126                         }
127                 }
128         }
129
130         /**
131          * {@inheritdoc}
132          */
133         public function getP($uid, $cat, $key = null)
134         {
135                 if (isset($this->config[$uid][$cat][$key])) {
136                         return $this->config[$uid][$cat][$key];
137                 } elseif (!isset($key) && isset($this->config[$uid][$cat])) {
138                         return $this->config[$uid][$cat];
139                 } else {
140                         return null;
141                 }
142         }
143
144         /**
145          * {@inheritdoc}
146          */
147         public function setP($uid, $cat, $key, $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])) {
154                         $this->config[$uid][$cat] = [];
155                 }
156
157                 $this->config[$uid][$cat][$key] = $value;
158
159                 return true;
160         }
161
162         /**
163          * {@inheritdoc}
164          */
165         public function deleteP($uid, $cat, $key)
166         {
167                 if (isset($this->config[$uid][$cat][$key])) {
168                         unset($this->config[$uid][$cat][$key]);
169                         if (count($this->config[$uid][$cat]) == 0) {
170                                 unset($this->config[$uid][$cat]);
171                                 if (count($this->config[$uid]) == 0) {
172                                         unset($this->config[$uid]);
173                                 }
174                         }
175
176                         return true;
177                 } else {
178                         return false;
179                 }
180         }
181
182         /**
183          * Returns the whole configuration
184          *
185          * @return array The configuration
186          */
187         public function getAll()
188         {
189                 return $this->config;
190         }
191
192         /**
193          * Returns an array with missing categories/Keys
194          *
195          * @param array $config The array to check
196          *
197          * @return array
198          */
199         public function keyDiff(array $config)
200         {
201                 $return = [];
202
203                 $categories = array_keys($config);
204
205                 foreach ($categories as $category) {
206                         if (is_array($config[$category])) {
207                                 $keys = array_keys($config[$category]);
208
209                                 foreach ($keys as $key) {
210                                         if (!isset($this->config[$category][$key])) {
211                                                 $return[$category][$key] = $config[$category][$key];
212                                         }
213                                 }
214                         }
215                 }
216
217                 return $return;
218         }
219 }