]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ConfigCache.php
fixing reset()
[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         private $config;
15
16         /**
17          * @param array $config    A initial config array
18          */
19         public function __construct(array $config = [])
20         {
21                 $this->loadConfigArray($config);
22         }
23
24         /**
25          * Tries to load the specified configuration array into the App->config array.
26          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
27          *
28          * @param array $config
29          * @param bool  $overwrite Force value overwrite if the config key already exists
30          */
31         public function loadConfigArray(array $config, $overwrite = false)
32         {
33                 foreach ($config as $category => $values) {
34                         foreach ($values as $key => $value) {
35                                 if ($overwrite) {
36                                         $this->set($category, $key, $value);
37                                 } else {
38                                         $this->setDefault($category, $key, $value);
39                                 }
40                         }
41                 }
42         }
43
44         /**
45          * {@inheritdoc}
46          */
47         public function get($cat, $key = null, $default = null)
48         {
49                 $return = $default;
50
51                 if ($cat === 'config') {
52                         if (isset($this->config[$key])) {
53                                 $return = $this->config[$key];
54                         }
55                 } else {
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                         }
61                 }
62
63                 return $return;
64         }
65
66         /**
67          * Sets a default value in the config cache. Ignores already existing keys.
68          *
69          * @param string $cat Config category
70          * @param string $k   Config key
71          * @param mixed  $v   Default value to set
72          */
73         private function setDefault($cat, $k, $v)
74         {
75                 if (!isset($this->config[$cat][$k])) {
76                         $this->set($cat, $k, $v);
77                 }
78         }
79
80         /**
81          * {@inheritdoc}
82          */
83         public function set($cat, $key, $value)
84         {
85                 // Only arrays are serialized in database, so we have to unserialize sparingly
86                 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
87
88                 if ($cat === 'config') {
89                         $this->config[$key] = $value;
90                 } else {
91                         if (!isset($this->config[$cat])) {
92                                 $this->config[$cat] = [];
93                         }
94
95                         $this->config[$cat][$key] = $value;
96                 }
97
98                 return true;
99         }
100
101         /**
102          * {@inheritdoc}
103          */
104         public function delete($cat, $key)
105         {
106                 if ($cat === 'config') {
107                         if (isset($this->config[$key])) {
108                                 unset($this->config[$key]);
109                         }
110                 } else {
111                         if (isset($this->config[$cat][$key])) {
112                                 unset($this->config[$cat][$key]);
113                                 if (count($this->config[$cat]) == 0) {
114                                         unset($this->config[$cat]);
115                                 }
116                         }
117                 }
118         }
119
120         /**
121          * {@inheritdoc}
122          */
123         public function getP($uid, $cat, $key = null, $default = null)
124         {
125                 $return = $default;
126
127                 if (isset($this->config[$uid][$cat][$key])) {
128                         $return = $this->config[$uid][$cat][$key];
129                 } elseif ($key === null && isset($this->config[$uid][$cat])) {
130                         $return = $this->config[$uid][$cat];
131                 }
132
133                 return $return;
134         }
135
136         /**
137          * {@inheritdoc}
138          */
139         public function setP($uid, $cat, $key, $value)
140         {
141                 // Only arrays are serialized in database, so we have to unserialize sparingly
142                 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
143
144                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
145                         $this->config[$uid] = [];
146                 }
147
148                 if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
149                         $this->config[$uid][$cat] = [];
150                 }
151
152                 if ($key === null) {
153                         $this->config[$uid][$cat] = $value;
154                 } else {
155                         $this->config[$uid][$cat][$key] = $value;
156                 }
157         }
158
159         /**
160          * {@inheritdoc}
161          */
162         public function deleteP($uid, $cat, $key)
163         {
164                 if (isset($this->config[$uid][$cat][$key])) {
165                         unset($this->config[$uid][$cat][$key]);
166                         if (count($this->config[$uid][$cat]) == 0) {
167                                 unset($this->config[$uid][$cat]);
168                                 if (count($this->config[$uid]) == 0) {
169                                         unset($this->config[$uid]);
170                                 }
171                         }
172                 }
173         }
174
175         /**
176          * Returns the whole configuration
177          *
178          * @return array The configuration
179          */
180         public function getAll()
181         {
182                 return $this->config;
183         }
184 }