]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ConfigCache.php
Merge pull request #6601 from annando/false-notifications
[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->config = $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
99         /**
100          * {@inheritdoc}
101          */
102         public function delete($cat, $key)
103         {
104                 if ($cat === 'config') {
105                         if (isset($this->config[$key])) {
106                                 unset($this->config[$key]);
107                         }
108                 } else {
109                         if (isset($this->config[$cat][$key])) {
110                                 unset($this->config[$cat][$key]);
111                         }
112                 }
113         }
114
115         /**
116          * {@inheritdoc}
117          */
118         public function getP($uid, $cat, $key = null, $default = null)
119         {
120                 $return = $default;
121
122                 if (isset($this->config[$uid][$cat][$key])) {
123                         $return = $this->config[$uid][$cat][$key];
124                 } elseif ($key === null && isset($this->config[$uid][$cat])) {
125                         $return = $this->config[$uid][$cat];
126                 }
127
128                 return $return;
129         }
130
131         /**
132          * {@inheritdoc}
133          */
134         public function setP($uid, $cat, $key, $value)
135         {
136                 // Only arrays are serialized in database, so we have to unserialize sparingly
137                 $value = is_string($value) && preg_match("|^a:[0-9]+:{.*}$|s", $value) ? unserialize($value) : $value;
138
139                 if (!isset($this->config[$uid]) || !is_array($this->config[$uid])) {
140                         $this->config[$uid] = [];
141                 }
142
143                 if (!isset($this->config[$uid][$cat]) || !is_array($this->config[$uid][$cat])) {
144                         $this->config[$uid][$cat] = [];
145                 }
146
147                 if ($key === null) {
148                         $this->config[$uid][$cat] = $value;
149                 } else {
150                         $this->config[$uid][$cat][$key] = $value;
151                 }
152         }
153
154         /**
155          * {@inheritdoc}
156          */
157         public function deleteP($uid, $cat, $key)
158         {
159                 if (isset($this->config[$uid][$cat][$key])) {
160                         unset($this->config[$uid][$cat][$key]);
161                 }
162         }
163
164         /**
165          * Returns the whole configuration
166          *
167          * @return array The configuration
168          */
169         public function getAll()
170         {
171                 return $this->config;
172         }
173 }