]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/Cache.php
Introduce Config\Cache Source indicators (File, DB, Server Env)
[friendica.git] / src / Core / Config / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core\Config;
23
24 use ParagonIE\HiddenString\HiddenString;
25
26 /**
27  * The Friendica config cache for the application
28  * Initial, all *.config.php files are loaded into this cache with the
29  * ConfigFileLoader ( @see ConfigFileLoader )
30  */
31 class Cache
32 {
33         /** @var int Indicates that the cache entry is set by file - Low Priority */
34         const SOURCE_FILE = 0;
35         /** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
36         const SOURCE_DB = 1;
37         /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
38         const SOURCE_ENV = 3;
39
40         /** @var int Default value for a config source */
41         const SOURCE_DEFAULT = self::SOURCE_FILE;
42
43         /**
44          * @var array
45          */
46         private $config;
47
48         /**
49          * @var int[][]
50          */
51         private $source = [];
52
53         /**
54          * @var bool
55          */
56         private $hidePasswordOutput;
57
58         /**
59          * @param array $config             A initial config array
60          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
61          * @param int   $source             Sets a source of the initial config values
62          */
63         public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
64         {
65                 $this->hidePasswordOutput = $hidePasswordOutput;
66                 $this->load($config, $source);
67         }
68
69         /**
70          * Tries to load the specified configuration array into the config array.
71          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
72          *
73          * @param array $config
74          * @param int   $source Indicates the source of the config entry
75          */
76         public function load(array $config, int $source = self::SOURCE_DEFAULT)
77         {
78                 $categories = array_keys($config);
79
80                 foreach ($categories as $category) {
81                         if (is_array($config[$category])) {
82                                 $keys = array_keys($config[$category]);
83
84                                 foreach ($keys as $key) {
85                                         $value = $config[$category][$key];
86                                         if (isset($value)) {
87                                                 $this->set($category, $key, $value, $source);
88                                         }
89                                 }
90                         }
91                 }
92         }
93
94         /**
95          * Gets a value from the config cache.
96          *
97          * @param string $cat Config category
98          * @param string $key Config key
99          *
100          * @return null|mixed Returns the value of the Config entry or null if not set
101          */
102         public function get(string $cat, string $key = null)
103         {
104                 if (isset($this->config[$cat][$key])) {
105                         return $this->config[$cat][$key];
106                 } else if (!isset($key) && isset($this->config[$cat])) {
107                         return $this->config[$cat];
108                 } else {
109                         return null;
110                 }
111         }
112
113         /**
114          * Sets a value in the config cache. Accepts raw output from the config table
115          *
116          * @param string $cat    Config category
117          * @param string $key    Config key
118          * @param mixed  $value  Value to set
119          * @param int    $source The source of the current config key
120          *
121          * @return bool True, if the value is set
122          */
123         public function set(string $cat, string $key, $value, $source = self::SOURCE_DEFAULT)
124         {
125                 if (!isset($this->config[$cat])) {
126                         $this->config[$cat] = [];
127                         $this->source[$cat] = [];
128                 }
129
130                 if (isset($this->source[$cat][$key]) &&
131                         $source < $this->source[$cat][$key]) {
132                         return false;
133                 }
134
135                 if ($this->hidePasswordOutput &&
136                         $key == 'password' &&
137                         is_string($value)) {
138                         $this->config[$cat][$key] = new HiddenString((string)$value);
139                 } else {
140                         $this->config[$cat][$key] = $value;
141                 }
142
143                 $this->source[$cat][$key] = $source;
144
145                 return true;
146         }
147
148         /**
149          * Deletes a value from the config cache.
150          *
151          * @param string $cat Config category
152          * @param string $key Config key
153          *
154          * @return bool true, if deleted
155          */
156         public function delete(string $cat, string $key)
157         {
158                 if (isset($this->config[$cat][$key])) {
159                         unset($this->config[$cat][$key]);
160                         unset($this->source[$cat][$key]);
161                         if (count($this->config[$cat]) == 0) {
162                                 unset($this->config[$cat]);
163                                 unset($this->source[$cat]);
164                         }
165                         return true;
166                 } else {
167                         return false;
168                 }
169         }
170
171         /**
172          * Returns the whole configuration
173          *
174          * @return array The configuration
175          */
176         public function getAll()
177         {
178                 return $this->config;
179         }
180
181         /**
182          * Returns an array with missing categories/Keys
183          *
184          * @param array $config The array to check
185          *
186          * @return array
187          */
188         public function keyDiff(array $config)
189         {
190                 $return = [];
191
192                 $categories = array_keys($config);
193
194                 foreach ($categories as $category) {
195                         if (is_array($config[$category])) {
196                                 $keys = array_keys($config[$category]);
197
198                                 foreach ($keys as $key) {
199                                         if (!isset($this->config[$category][$key])) {
200                                                 $return[$category][$key] = $config[$category][$key];
201                                         }
202                                 }
203                         }
204                 }
205
206                 return $return;
207         }
208 }