]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ValueObject/Cache.php
Remove unnecessary classes
[friendica.git] / src / Core / Config / ValueObject / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, the Friendica project
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\ValueObject;
23
24 use Friendica\Core\Config\Util\ConfigFileManager;
25 use ParagonIE\HiddenString\HiddenString;
26
27 /**
28  * The Friendica config cache for the application
29  * Initial, all *.config.php files are loaded into this cache with the
30  * ConfigFileManager ( @see ConfigFileManager )
31  */
32 class Cache
33 {
34         /** @var int Indicates that the cache entry is a default value - Lowest Priority */
35         const SOURCE_STATIC = 0;
36         /** @var int Indicates that the cache entry is set by file - Low Priority */
37         const SOURCE_FILE = 1;
38         /** @var int Indicates that the cache entry is manually set by the application (per admin page/console) - Middle Priority */
39         const SOURCE_DATA = 2;
40         /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
41         const SOURCE_ENV = 3;
42         /** @var int Indicates that the cache entry is fixed and must not be changed */
43         const SOURCE_FIX = 5;
44
45         /** @var int Default value for a config source */
46         const SOURCE_DEFAULT = self::SOURCE_FILE;
47
48         /**
49          * @var array
50          */
51         private $config = [];
52
53         /**
54          * @var int[][]
55          */
56         private $source = [];
57
58         /**
59          * @var bool
60          */
61         private $hidePasswordOutput;
62
63         /**
64          * @param array $config             A initial config array
65          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
66          * @param int   $source             Sets a source of the initial config values
67          */
68         public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
69         {
70                 $this->hidePasswordOutput = $hidePasswordOutput;
71                 $this->load($config, $source);
72         }
73
74         /**
75          * Tries to load the specified configuration array into the config array.
76          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
77          *
78          * @param array $config
79          * @param int   $source Indicates the source of the config entry
80          */
81         public function load(array $config, int $source = self::SOURCE_DEFAULT)
82         {
83                 $categories = array_keys($config);
84
85                 foreach ($categories as $category) {
86                         if (is_array($config[$category])) {
87                                 $keys = array_keys($config[$category]);
88
89                                 foreach ($keys as $key) {
90                                         $value = $config[$category][$key];
91                                         if (isset($value)) {
92                                                 $this->set($category, $key, $value, $source);
93                                         }
94                                 }
95                         }
96                 }
97         }
98
99         /**
100          * Gets a value from the config cache.
101          *
102          * @param string      $cat Config category
103          * @param string|null $key Config key
104          *
105          * @return null|mixed Returns the value of the Config entry or null if not set
106          */
107         public function get(string $cat, ?string $key = null)
108         {
109                 if (isset($this->config[$cat][$key])) {
110                         return $this->config[$cat][$key];
111                 } elseif (!isset($key) && isset($this->config[$cat])) {
112                         return $this->config[$cat];
113                 } else {
114                         return null;
115                 }
116         }
117
118         /**
119          * Returns the source value of the current, cached config value
120          *
121          * @param string $cat Config category
122          * @param string $key Config key
123          *
124          * @return int
125          */
126         public function getSource(string $cat, string $key): int
127         {
128                 return $this->source[$cat][$key] ?? -1;
129         }
130
131         /**
132          * Returns the whole config array based on the given source type
133          *
134          * @param int $source Indicates the source of the config entry
135          *
136          * @return array The config array part of the given source
137          */
138         public function getDataBySource(int $source): array
139         {
140                 $data = [];
141
142                 $categories = array_keys($this->source);
143
144                 foreach ($categories as $category) {
145                         if (is_array($this->source[$category])) {
146                                 $keys = array_keys($this->source[$category]);
147
148                                 foreach ($keys as $key) {
149                                         if ($this->source[$category][$key] === $source) {
150                                                 $data[$category][$key] = $this->config[$category][$key];
151                                         }
152                                 }
153                         }
154                 }
155
156                 return $data;
157         }
158
159         /**
160          * Sets a value in the config cache. Accepts raw output from the config table
161          *
162          * @param string $cat    Config category
163          * @param string $key    Config key
164          * @param mixed  $value  Value to set
165          * @param int    $source The source of the current config key
166          *
167          * @return bool True, if the value is set
168          */
169         public function set(string $cat, string $key, $value, int $source = self::SOURCE_DEFAULT): bool
170         {
171                 if (!isset($this->config[$cat])) {
172                         $this->config[$cat] = [];
173                         $this->source[$cat] = [];
174                 }
175
176                 if (isset($this->source[$cat][$key]) &&
177                         $source < $this->source[$cat][$key]) {
178                         return false;
179                 }
180
181                 if ($this->hidePasswordOutput &&
182                         $key == 'password' &&
183                         is_string($value)) {
184                         $this->config[$cat][$key] = new HiddenString((string)$value);
185                 } else {
186                         $this->config[$cat][$key] = $value;
187                 }
188
189                 $this->source[$cat][$key] = $source;
190
191                 return true;
192         }
193
194         /**
195          * Deletes a value from the config cache.
196          *
197          * @param string $cat Config category
198          * @param string $key Config key
199          *
200          * @return bool true, if deleted
201          */
202         public function delete(string $cat, string $key): bool
203         {
204                 if (isset($this->config[$cat][$key])) {
205                         unset($this->config[$cat][$key]);
206                         unset($this->source[$cat][$key]);
207                         if (count($this->config[$cat]) == 0) {
208                                 unset($this->config[$cat]);
209                                 unset($this->source[$cat]);
210                         }
211                         return true;
212                 } else {
213                         return false;
214                 }
215         }
216
217         /**
218          * Returns the whole configuration
219          *
220          * @return string[][] The configuration
221          */
222         public function getAll(): array
223         {
224                 return $this->config;
225         }
226
227         /**
228          * Returns an array with missing categories/Keys
229          *
230          * @param string[][] $config The array to check
231          *
232          * @return string[][]
233          */
234         public function keyDiff(array $config): array
235         {
236                 $return = [];
237
238                 $categories = array_keys($config);
239
240                 foreach ($categories as $category) {
241                         if (is_array($config[$category])) {
242                                 $keys = array_keys($config[$category]);
243
244                                 foreach ($keys as $key) {
245                                         if (!isset($this->config[$category][$key])) {
246                                                 $return[$category][$key] = $config[$category][$key];
247                                         }
248                                 }
249                         }
250                 }
251
252                 return $return;
253         }
254 }