1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_confs; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_confs\Interfaces\IConf; |
7
|
|
|
use kalanis\kw_confs\Interfaces\ILoader; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Config |
12
|
|
|
* @package kalanis\kw_confs |
13
|
|
|
* Store config data through system runtime |
14
|
|
|
*/ |
15
|
|
|
class Config |
16
|
|
|
{ |
17
|
|
|
protected static ?ILoader $loader = null; |
18
|
|
|
/** @var array<string, array<string|int, string|int|float|bool|null>> */ |
19
|
|
|
protected static array $configs = []; |
20
|
|
|
|
21
|
6 |
|
public static function init(?ILoader $loader): void |
22
|
|
|
{ |
23
|
6 |
|
static::$loader = $loader; |
24
|
6 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @param string $module |
28
|
|
|
* @param string $conf |
29
|
|
|
* @throws ConfException |
30
|
|
|
*/ |
31
|
5 |
|
public static function load(string $module, string $conf = ''): void |
32
|
|
|
{ |
33
|
5 |
|
if (!static::$loader) { |
34
|
1 |
|
throw new ConfException('Set the loader first!'); |
35
|
|
|
} |
36
|
4 |
|
$data = static::$loader->load($module, $conf); |
37
|
4 |
|
if (!is_null($data)) { |
38
|
3 |
|
static::loadData($module, $data); |
39
|
|
|
} |
40
|
4 |
|
} |
41
|
|
|
|
42
|
1 |
|
public static function loadClass(IConf $conf): void |
43
|
|
|
{ |
44
|
1 |
|
static::loadData($conf->getConfName(), $conf->getSettings()); |
45
|
1 |
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param string $module |
49
|
|
|
* @param array<string|int, string|int|float|bool|null> $confData |
50
|
|
|
*/ |
51
|
4 |
|
protected static function loadData(string $module, array $confData = []): void |
52
|
|
|
{ |
53
|
4 |
|
if (empty(static::$configs[$module])) { |
54
|
3 |
|
static::$configs[$module] = []; |
55
|
|
|
} |
56
|
4 |
|
static::$configs[$module] = array_merge(static::$configs[$module], $confData); |
57
|
4 |
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param string $module |
61
|
|
|
* @param string $key |
62
|
|
|
* @param string|int|float|bool|null $defaultValue |
63
|
|
|
* @return string|int|float|bool|null |
64
|
|
|
*/ |
65
|
5 |
|
public static function get(string $module, string $key, $defaultValue = null) |
66
|
|
|
{ |
67
|
5 |
|
return (isset(static::$configs[$module]) && isset(static::$configs[$module][$key])) |
68
|
4 |
|
? static::$configs[$module][$key] |
69
|
5 |
|
: $defaultValue |
70
|
|
|
; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $module |
75
|
|
|
* @param string $key |
76
|
|
|
* @param string|int|float|bool|null $defaultValue |
77
|
|
|
*/ |
78
|
1 |
|
public static function set(string $module, string $key, $defaultValue = null): void |
79
|
|
|
{ |
80
|
1 |
|
isset(static::$configs[$module]) ? static::$configs[$module][$key] = $defaultValue : null ; |
81
|
1 |
|
} |
82
|
|
|
|
83
|
1 |
|
public static function getLoader(): ?ILoader |
84
|
|
|
{ |
85
|
1 |
|
return static::$loader; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|