|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
namespace AbterPhp\Framework\Config; |
|
7
|
|
|
|
|
8
|
|
|
use Opulence\Framework\Configuration\Config as OpulenceConfig; |
|
9
|
|
|
use RuntimeException; |
|
10
|
|
|
|
|
11
|
|
|
class Config extends OpulenceConfig |
|
12
|
|
|
{ |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Gets a setting, but throws an exception if it's empty |
|
16
|
|
|
* |
|
17
|
|
|
* @param string $category The category of setting to get |
|
18
|
|
|
* @param string $setting The name of the setting to get |
|
19
|
|
|
* @param mixed $default The default value if one does not exist |
|
20
|
|
|
* |
|
21
|
|
|
* @return string|int|float|bool The value of the setting |
|
22
|
|
|
* @throws \RuntimeException |
|
23
|
|
|
*/ |
|
24
|
|
|
public static function mustGet(string $category, string $setting, $default = null) |
|
25
|
|
|
{ |
|
26
|
|
|
$value = parent::get($category, $setting, $default); |
|
27
|
|
|
|
|
28
|
|
|
if ($value === null) { |
|
29
|
|
|
throw new RuntimeException(sprintf("missing config: %s:%s", $category, $setting)); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
if (!is_scalar($value)) { |
|
33
|
|
|
throw new RuntimeException( |
|
34
|
|
|
sprintf("invalid config: %s:%s, type: %s", $category, $setting, gettype($value)) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
return $value; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @param string $category |
|
43
|
|
|
* @param string $setting |
|
44
|
|
|
* @param string $default |
|
45
|
|
|
* |
|
46
|
|
|
* @return string |
|
47
|
|
|
*/ |
|
48
|
|
|
public static function mustGetString(string $category, string $setting, string $default = ""): string |
|
49
|
|
|
{ |
|
50
|
|
|
return (string)static::mustGet($category, $setting, $default); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $category |
|
55
|
|
|
* @param string $setting |
|
56
|
|
|
* @param bool $default |
|
57
|
|
|
* |
|
58
|
|
|
* @return bool |
|
59
|
|
|
*/ |
|
60
|
|
|
public static function mustGetBool(string $category, string $setting, bool $default = false): bool |
|
61
|
|
|
{ |
|
62
|
|
|
$value = static::mustGet($category, $setting, $default); |
|
63
|
|
|
|
|
64
|
|
|
if (is_bool($value)) { |
|
65
|
|
|
return $value; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if (is_string($value) && in_array(strtolower($value), ['false', 'true'], true)) { |
|
69
|
|
|
switch (strtolower($value)) { |
|
70
|
|
|
case 'false': |
|
71
|
|
|
return false; |
|
72
|
|
|
case 'true': |
|
73
|
|
|
return true; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
throw new RuntimeException( |
|
77
|
|
|
sprintf("non-bool config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
|
78
|
|
|
); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
if (in_array($value, [0, 1, '0', '1'], true)) { |
|
82
|
|
|
return (bool)$value; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
throw new RuntimeException( |
|
86
|
|
|
sprintf("non-bool config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
|
87
|
|
|
); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $category |
|
92
|
|
|
* @param string $setting |
|
93
|
|
|
* @param int $default |
|
94
|
|
|
* |
|
95
|
|
|
* @return int |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function mustGetInt(string $category, string $setting, int $default = 0): int |
|
98
|
|
|
{ |
|
99
|
|
|
$value = static::mustGet($category, $setting, $default); |
|
100
|
|
|
|
|
101
|
|
|
if (!is_numeric($value)) { |
|
102
|
|
|
throw new RuntimeException( |
|
103
|
|
|
sprintf("non-int config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
|
104
|
|
|
); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
return (int)$value; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param string $category |
|
112
|
|
|
* @param string $setting |
|
113
|
|
|
* @param float $default |
|
114
|
|
|
* |
|
115
|
|
|
* @return float |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function mustGetFloat(string $category, string $setting, float $default = 0.0): float |
|
118
|
|
|
{ |
|
119
|
|
|
$value = static::mustGet($category, $setting, $default); |
|
120
|
|
|
|
|
121
|
|
|
if (!is_numeric($value)) { |
|
122
|
|
|
throw new RuntimeException( |
|
123
|
|
|
sprintf("non-float config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
|
124
|
|
|
); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return (float)$value; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* Gets a setting, but throws an exception if it's empty |
|
132
|
|
|
* |
|
133
|
|
|
* @param string $category The category of setting to get |
|
134
|
|
|
* @param string $setting The name of the setting to get |
|
135
|
|
|
* @param mixed $default The default value if one does not exist |
|
136
|
|
|
* |
|
137
|
|
|
* @return string|int|float|bool The value of the setting |
|
138
|
|
|
*/ |
|
139
|
|
|
public static function reset(string $category, string $setting, $default = null) |
|
140
|
|
|
{ |
|
141
|
|
|
$value = parent::get($category, $setting, $default); |
|
142
|
|
|
|
|
143
|
|
|
if ($value === null) { |
|
144
|
|
|
throw new RuntimeException(sprintf("missing config: %s:%s", $category, $setting)); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
if (!is_scalar($value)) { |
|
148
|
|
|
throw new RuntimeException( |
|
149
|
|
|
sprintf("invalid config: %s:%s, type: %s", $category, $setting, gettype($value)) |
|
150
|
|
|
); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
return $value; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|