/
config.php
420 lines (361 loc) · 12.4 KB
/
config.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<?php
/**
* Class for handling configurations.
* The configuration is persisted between requests.
*
* @author staabm
*
* @package redaxo\core
*/
class rex_config
{
/**
* Flag to indicate if the config was initialized.
*
* @var bool
*/
private static $initialized = false;
/**
* path to the cache file.
*
* @var string
*/
private static $cacheFile;
/**
* Flag which indicates if database needs an update, because settings have changed.
*
* @var bool
*/
private static $changed = false;
/**
* data read from database.
*
* @var array<string, array<string, mixed>>
*/
private static $data = [];
/**
* data which is modified during this request.
*
* @var array<string, array<string, mixed>>
*/
private static $changedData = [];
/**
* data which was deleted during this request.
*
* @var array<string, array<string, true>>
*/
private static $deletedData = [];
/**
* Method which saves an arbitary value associated to the given namespace and key.
* If the second parameter is an associative array, all key/value pairs will be saved.
*
* The set-method returns TRUE when an existing value was overridden, otherwise FALSE is returned.
*
* @param string $namespace The namespace e.g. an addon name
* @param string|array<string, mixed> $key The associated key or an associative array of key/value pairs
* @param mixed $value The value to save
*
* @throws InvalidArgumentException
*
* @return bool TRUE when an existing value was overridden, otherwise FALSE
*/
public static function set($namespace, $key, $value = null)
{
self::init();
if (!is_string($namespace)) {
throw new InvalidArgumentException('rex_config: expecting $namespace to be a string, ' . gettype($namespace) . ' given!');
}
if (is_array($key)) {
$existed = false;
foreach ($key as $k => $v) {
$existed = self::set($namespace, $k, $v) || $existed;
}
return $existed;
}
if (!is_string($key)) {
throw new InvalidArgumentException('rex_config: expecting $key to be a string, ' . gettype($key) . ' given!');
}
if (!isset(self::$data[$namespace])) {
self::$data[$namespace] = [];
}
$existed = isset(self::$data[$namespace][$key]);
if (!$existed || self::$data[$namespace][$key] !== $value) {
// keep track of changed data
self::$changedData[$namespace][$key] = $value;
// since it was re-added, do not longer mark as deleted
unset(self::$deletedData[$namespace][$key]);
// re-set the data in the container
self::$data[$namespace][$key] = $value;
self::$changed = true;
}
return $existed;
}
/**
* Method which returns an associated value for the given namespace and key.
* If $key is null, an array of all key/value pairs for the given namespace will be returned.
*
* If no value can be found for the given key/namespace combination $default is returned.
*
* @template T as ?string
* @param string $namespace The namespace e.g. an addon name
* @param T $key The associated key
* @param mixed $default Default return value if no associated-value can be found
* @throws InvalidArgumentException
* @return mixed the value for $key or $default if $key cannot be found in the given $namespace
* @psalm-return (T is string ? mixed|null : array<string, mixed>)
*/
public static function get($namespace, $key = null, $default = null)
{
self::init();
if (!is_string($namespace)) {
throw new InvalidArgumentException('rex_config: expecting $namespace to be a string, ' . gettype($namespace) . ' given!');
}
if (null === $key) {
return self::$data[$namespace] ?? [];
}
if (!is_string($key)) {
throw new InvalidArgumentException('rex_config: expecting $key to be a string, ' . gettype($key) . ' given!');
}
if (isset(self::$data[$namespace][$key])) {
return self::$data[$namespace][$key];
}
return $default;
}
/**
* Returns if the given key is set.
*
* @param string $namespace The namespace e.g. an addon name
* @param string|null $key The associated key
*
* @throws InvalidArgumentException
*
* @return bool TRUE if the key is set, otherwise FALSE
*/
public static function has($namespace, $key = null)
{
self::init();
if (!is_string($namespace)) {
throw new InvalidArgumentException('rex_config: expecting $namespace to be a string, ' . gettype($namespace) . ' given!');
}
if (null === $key) {
return isset(self::$data[$namespace]);
}
if (!is_string($key)) {
throw new InvalidArgumentException('rex_config: expecting $key to be a string, ' . gettype($key) . ' given!');
}
return isset(self::$data[$namespace][$key]);
}
/**
* Removes the setting associated with the given namespace and key.
*
* @param string $namespace The namespace e.g. an addon name
* @param string $key The associated key
*
* @throws InvalidArgumentException
*
* @return bool TRUE if the value was found and removed, otherwise FALSE
*/
public static function remove($namespace, $key)
{
self::init();
if (!is_string($namespace)) {
throw new InvalidArgumentException('rex_config: expecting $namespace to be a string, ' . gettype($namespace) . ' given!');
}
if (!is_string($key)) {
throw new InvalidArgumentException('rex_config: expecting $key to be a string, ' . gettype($key) . ' given!');
}
if (isset(self::$data[$namespace][$key])) {
// keep track of deleted data
self::$deletedData[$namespace][$key] = true;
// since it will be deleted, do not longer mark as changed
unset(self::$changedData[$namespace][$key]);
if (empty(self::$changedData[$namespace])) {
unset(self::$changedData[$namespace]);
}
// delete the data from the container
unset(self::$data[$namespace][$key]);
if (empty(self::$data[$namespace])) {
unset(self::$data[$namespace]);
}
self::$changed = true;
return true;
}
return false;
}
/**
* Removes all settings associated with the given namespace.
*
* @param string $namespace The namespace e.g. an addon name
*
* @throws InvalidArgumentException
*
* @return bool TRUE if the namespace was found and removed, otherwise FALSE
*/
public static function removeNamespace($namespace)
{
self::init();
if (!is_string($namespace)) {
throw new InvalidArgumentException('rex_config: expecting $namespace to be a string, ' . gettype($namespace) . ' given!');
}
if (isset(self::$data[$namespace])) {
foreach (self::$data[$namespace] as $key => $value) {
self::remove($namespace, $key);
}
unset(self::$data[$namespace]);
self::$changed = true;
return true;
}
return false;
}
/**
* Refreshes rex_config by reloading config from db.
* @return void
*/
public static function refresh()
{
if (!self::$initialized) {
self::init();
return;
}
self::loadFromDb();
self::generateCache();
self::$changed = false;
self::$changedData = [];
self::$deletedData = [];
}
/**
* initilizes the rex_config class.
* @return void
*/
protected static function init()
{
if (self::$initialized) {
return;
}
self::$cacheFile = rex_path::coreCache('config.cache');
// take care, so we are able to write a cache file on shutdown
// (check here, since exceptions in shutdown functions are not visible to the user)
$dir = dirname(self::$cacheFile);
rex_dir::create($dir);
if (!is_writable($dir)) {
throw new rex_exception('rex-config: cache dir "' . dirname(self::$cacheFile) . '" is not writable!');
}
// save cache on shutdown
register_shutdown_function([self::class, 'save']);
self::load();
self::$initialized = true;
}
/**
* load the config-data.
* @return void
*/
protected static function load()
{
// check if we can load the config from the filesystem
if (!self::loadFromFile()) {
// if not possible, fallback to load config from the db
self::loadFromDb();
// afterwards persist loaded data into file-cache
self::generateCache();
}
}
/**
* load the config-data from a file-cache.
*
* @return bool Returns TRUE, if the data was successfully loaded from the file-cache, otherwise FALSE
*/
private static function loadFromFile()
{
// delete cache-file, will be regenerated on next request
if (is_file(self::$cacheFile)) {
self::$data = rex_file::getCache(self::$cacheFile);
return true;
}
return false;
}
/**
* load the config-data from database.
* @return void
*/
private static function loadFromDb()
{
$sql = rex_sql::factory();
$sql->setQuery('SELECT * FROM ' . rex::getTablePrefix() . 'config');
self::$data = [];
foreach ($sql as $cfg) {
self::$data[$cfg->getValue('namespace')][$cfg->getValue('key')] = json_decode($cfg->getValue('value'), true);
}
}
/**
* save config to file-cache.
* @return void
*/
private static function generateCache()
{
if (rex_file::putCache(self::$cacheFile, self::$data) <= 0) {
throw new rex_exception('rex-config: unable to write cache file ' . self::$cacheFile);
}
}
/**
* persists the config-data and truncates the file-cache.
* @return void
*/
public static function save()
{
// save cache only if changes happened
if (!self::$changed) {
return;
}
// after all no data needs to be deleted or update, so skip save
if (empty(self::$deletedData) && empty(self::$changedData)) {
return;
}
// delete cache-file; will be regenerated on next request
rex_file::delete(self::$cacheFile);
// save all data to the db
self::saveToDb();
self::$changed = false;
self::$changedData = [];
self::$deletedData = [];
}
/**
* save the config-data into the db.
* @return void
*/
private static function saveToDb()
{
$sql = rex_sql::factory();
// $sql->setDebug();
// remove all deleted data
if (self::$deletedData) {
$sql->setTable(rex::getTable('config'));
$where = [];
$params = [];
foreach (self::$deletedData as $namespace => $nsData) {
if (0 === count($nsData)) {
continue;
}
$params[] = $namespace;
$where[] = 'namespace = ? AND `key` IN ('.$sql->in(array_keys($nsData)).')';
}
if (count($where) > 0) {
$sql->setWhere(implode("\n OR ", $where), $params);
$sql->delete();
}
}
// update all changed data
if (self::$changedData) {
$sql->setTable(rex::getTable('config'));
foreach (self::$changedData as $namespace => $nsData) {
foreach ($nsData as $key => $value) {
$sql->addRecord(static function (rex_sql $record) use ($namespace, $key, $value) {
$record->setValue('namespace', $namespace);
$record->setValue('key', $key);
$record->setValue('value', json_encode($value));
});
}
}
$sql->insertOrUpdate();
}
}
}