]> git.mxchange.org Git - friendica.git/blob - src/Core/Config/ValueObject/Cache.php
Merge pull request #11015 from MrPetovan/task/10979-frio-time-tooltip
[friendica.git] / src / Core / Config / ValueObject / Cache.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\ConfigFileLoader;
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  * ConfigFileLoader ( @see ConfigFileLoader )
31  */
32 class Cache
33 {
34         /** @var int Indicates that the cache entry is set by file - Low Priority */
35         const SOURCE_FILE = 0;
36         /** @var int Indicates that the cache entry is set by the DB config table - Middle Priority */
37         const SOURCE_DB = 1;
38         /** @var int Indicates that the cache entry is set by a server environment variable - High Priority */
39         const SOURCE_ENV = 3;
40         /** @var int Indicates that the cache entry is fixed and must not be changed */
41         const SOURCE_FIX = 4;
42
43         /** @var int Default value for a config source */
44         const SOURCE_DEFAULT = self::SOURCE_FILE;
45
46         /**
47          * @var array
48          */
49         private $config = [];
50
51         /**
52          * @var int[][]
53          */
54         private $source = [];
55
56         /**
57          * @var bool
58          */
59         private $hidePasswordOutput;
60
61         /**
62          * @param array $config             A initial config array
63          * @param bool  $hidePasswordOutput True, if cache variables should take extra care of password values
64          * @param int   $source             Sets a source of the initial config values
65          */
66         public function __construct(array $config = [], bool $hidePasswordOutput = true, $source = self::SOURCE_DEFAULT)
67         {
68                 $this->hidePasswordOutput = $hidePasswordOutput;
69                 $this->load($config, $source);
70         }
71
72         /**
73          * Tries to load the specified configuration array into the config array.
74          * Doesn't overwrite previously set values by default to prevent default config files to supersede DB Config.
75          *
76          * @param array $config
77          * @param int   $source Indicates the source of the config entry
78          */
79         public function load(array $config, int $source = self::SOURCE_DEFAULT)
80         {
81                 $categories = array_keys($config);
82
83                 foreach ($categories as $category) {
84                         if (is_array($config[$category])) {
85                                 $keys = array_keys($config[$category]);
86
87                                 foreach ($keys as $key) {
88                                         $value = $config[$category][$key];
89                                         if (isset($value)) {
90                                                 $this->set($category, $key, $value, $source);
91                                         }
92                                 }
93                         }
94                 }
95         }
96
97         /**
98          * Gets a value from the config cache.
99          *
100          * @param string      $cat Config category
101          * @param string|null $key Config key
102          *
103          * @return null|mixed Returns the value of the Config entry or null if not set
104          */
105         public function get(string $cat, ?string $key = null)
106         {
107                 if (isset($this->config[$cat][$key])) {
108                         return $this->config[$cat][$key];
109                 } elseif (!isset($key) && isset($this->config[$cat])) {
110                         return $this->config[$cat];
111                 } else {
112                         return null;
113                 }
114         }
115
116         /**
117          * Sets a value in the config cache. Accepts raw output from the config table
118          *
119          * @param string $cat    Config category
120          * @param string $key    Config key
121          * @param mixed  $value  Value to set
122          * @param int    $source The source of the current config key
123          *
124          * @return bool True, if the value is set
125          */
126         public function set(string $cat, string $key, $value, int $source = self::SOURCE_DEFAULT): bool
127         {
128                 if (!isset($this->config[$cat])) {
129                         $this->config[$cat] = [];
130                         $this->source[$cat] = [];
131                 }
132
133                 if (isset($this->source[$cat][$key]) &&
134                         $source < $this->source[$cat][$key]) {
135                         return false;
136                 }
137
138                 if ($this->hidePasswordOutput &&
139                         $key == 'password' &&
140                         is_string($value)) {
141                         $this->config[$cat][$key] = new HiddenString((string)$value);
142                 } else {
143                         $this->config[$cat][$key] = $value;
144                 }
145
146                 $this->source[$cat][$key] = $source;
147
148                 return true;
149         }
150
151         /**
152          * Deletes a value from the config cache.
153          *
154          * @param string $cat Config category
155          * @param string $key Config key
156          *
157          * @return bool true, if deleted
158          */
159         public function delete(string $cat, string $key): bool
160         {
161                 if (isset($this->config[$cat][$key])) {
162                         unset($this->config[$cat][$key]);
163                         unset($this->source[$cat][$key]);
164                         if (count($this->config[$cat]) == 0) {
165                                 unset($this->config[$cat]);
166                                 unset($this->source[$cat]);
167                         }
168                         return true;
169                 } else {
170                         return false;
171                 }
172         }
173
174         /**
175          * Returns the whole configuration
176          *
177          * @return string[][] The configuration
178          */
179         public function getAll(): array
180         {
181                 return $this->config;
182         }
183
184         /**
185          * Returns an array with missing categories/Keys
186          *
187          * @param string[][] $config The array to check
188          *
189          * @return string[][]
190          */
191         public function keyDiff(array $config): array
192         {
193                 $return = [];
194
195                 $categories = array_keys($config);
196
197                 foreach ($categories as $category) {
198                         if (is_array($config[$category])) {
199                                 $keys = array_keys($config[$category]);
200
201                                 foreach ($keys as $key) {
202                                         if (!isset($this->config[$category][$key])) {
203                                                 $return[$category][$key] = $config[$category][$key];
204                                         }
205                                 }
206                         }
207                 }
208
209                 return $return;
210         }
211 }