]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache/ConfigCache.php
fix phpdoc
[friendica.git] / src / Core / Config / Cache / ConfigCache.php
1 <?php
2
3 namespace Friendica\Core\Config\Cache;
4
5 use ParagonIE\HiddenString\HiddenString;
6
7 /**
8  * The Friendica config cache for the application
9  * Initial, all *.config.php files are loaded into this cache with the
10  * ConfigFileLoader ( @see ConfigFileLoader )
11  */
12 class ConfigCache
13 {
14         /**
15          * @var array
16          */
17         private $config;
18
19         /**
20          * @var bool
21          */
22         private $hidePasswordOutput;
23
24         /**
25          * @param array $config             A initial config array
26          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
27          */
28         public function __construct(array $config = [], bool $hidePasswordOutput = true)
29         {
30                 $this->hidePasswordOutput = $hidePasswordOutput;
31                 $this->load($config);
32         }
33
34         /**
35          * Tries to load the specified configuration array into the config array.
36          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
37          *
38          * @param array $config
39          * @param bool  $overwrite Force value overwrite if the config key already exists
40          */
41         public function load(array $config, bool $overwrite = false)
42         {
43                 $categories = array_keys($config);
44
45                 foreach ($categories as $category) {
46                         if (is_array($config[$category])) {
47                                 $keys = array_keys($config[$category]);
48
49                                 foreach ($keys as $key) {
50                                         $value = $config[$category][$key];
51                                         if (isset($value)) {
52                                                 if ($overwrite) {
53                                                         $this->set($category, $key, $value);
54                                                 } else {
55                                                         $this->setDefault($category, $key, $value);
56                                                 }
57                                         }
58                                 }
59                         }
60                 }
61         }
62
63         /**
64          * Gets a value from the config cache.
65          *
66          * @param string $cat Config category
67          * @param string $key Config key
68          *
69          * @return null|mixed Returns the value of the Config entry or null if not set
70          */
71         public function get(string $cat, $key = null)
72         {
73                 if (isset($this->config[$cat][$key])) {
74                         return $this->config[$cat][$key];
75                 } elseif (!isset($key) && isset($this->config[$cat])) {
76                         return $this->config[$cat];
77                 } else {
78                         return null;
79                 }
80         }
81
82         /**
83          * Sets a default value in the config cache. Ignores already existing keys.
84          *
85          * @param string $cat   Config category
86          * @param string $key   Config key
87          * @param mixed  $value Default value to set
88          */
89         private function setDefault(string $cat, string $key, $value)
90         {
91                 if (!isset($this->config[$cat][$key])) {
92                         $this->set($cat, $key, $value);
93                 }
94         }
95
96         /**
97          * Sets a value in the config cache. Accepts raw output from the config table
98          *
99          * @param string $cat   Config category
100          * @param string $key   Config key
101          * @param mixed  $value Value to set
102          *
103          * @return bool True, if the value is set
104          */
105         public function set(string $cat, string $key, $value)
106         {
107                 if (!isset($this->config[$cat])) {
108                         $this->config[$cat] = [];
109                 }
110
111                 if ($this->hidePasswordOutput &&
112                     $key == 'password' &&
113                     is_string($value)) {
114                         $this->config[$cat][$key] = new HiddenString((string)$value);
115                 } else {
116                         $this->config[$cat][$key] = $value;
117                 }
118                 return true;
119         }
120
121         /**
122          * Deletes a value from the config cache.
123          *
124          * @param string $cat Config category
125          * @param string $key Config key
126          *
127          * @return bool true, if deleted
128          */
129         public function delete(string $cat, string $key)
130         {
131                 if (isset($this->config[$cat][$key])) {
132                         unset($this->config[$cat][$key]);
133                         if (count($this->config[$cat]) == 0) {
134                                 unset($this->config[$cat]);
135                         }
136                         return true;
137                 } else {
138                         return false;
139                 }
140         }
141
142         /**
143          * Returns the whole configuration
144          *
145          * @return array The configuration
146          */
147         public function getAll()
148         {
149                 return $this->config;
150         }
151
152         /**
153          * Returns an array with missing categories/Keys
154          *
155          * @param array $config The array to check
156          *
157          * @return array
158          */
159         public function keyDiff(array $config)
160         {
161                 $return = [];
162
163                 $categories = array_keys($config);
164
165                 foreach ($categories as $category) {
166                         if (is_array($config[$category])) {
167                                 $keys = array_keys($config[$category]);
168
169                                 foreach ($keys as $key) {
170                                         if (!isset($this->config[$category][$key])) {
171                                                 $return[$category][$key] = $config[$category][$key];
172                                         }
173                                 }
174                         }
175                 }
176
177                 return $return;
178         }
179 }