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 (in_array($value, [0, 1, '0', '1', ''], true)) { |
69
|
|
|
return (bool)$value; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
if (is_string($value)) { |
73
|
|
|
$l = strtolower($value); |
74
|
|
|
|
75
|
|
|
if ($l === 'false') { |
76
|
|
|
return false; |
77
|
|
|
} elseif ($l === 'true') { |
78
|
|
|
return true; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
throw new RuntimeException( |
83
|
|
|
sprintf("non-bool config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
84
|
|
|
); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $category |
89
|
|
|
* @param string $setting |
90
|
|
|
* @param int $default |
91
|
|
|
* |
92
|
|
|
* @return int |
93
|
|
|
*/ |
94
|
|
|
public static function mustGetInt(string $category, string $setting, int $default = 0): int |
95
|
|
|
{ |
96
|
|
|
$value = static::mustGet($category, $setting, $default); |
97
|
|
|
|
98
|
|
|
if (!is_numeric($value)) { |
99
|
|
|
throw new RuntimeException( |
100
|
|
|
sprintf("non-int config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
101
|
|
|
); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return (int)$value; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @param string $category |
109
|
|
|
* @param string $setting |
110
|
|
|
* @param float $default |
111
|
|
|
* |
112
|
|
|
* @return float |
113
|
|
|
*/ |
114
|
|
|
public static function mustGetFloat(string $category, string $setting, float $default = 0.0): float |
115
|
|
|
{ |
116
|
|
|
$value = static::mustGet($category, $setting, $default); |
117
|
|
|
|
118
|
|
|
if (!is_numeric($value)) { |
119
|
|
|
throw new RuntimeException( |
120
|
|
|
sprintf("non-float config: %s:%s, type: %s, value: %s", $category, $setting, gettype($value), $value) |
121
|
|
|
); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return (float)$value; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Gets a setting, but throws an exception if it's empty |
129
|
|
|
* |
130
|
|
|
* @param string $category The category of setting to get |
131
|
|
|
* @param string $setting The name of the setting to get |
132
|
|
|
* @param mixed $default The default value if one does not exist |
133
|
|
|
* |
134
|
|
|
* @return string|int|float|bool The value of the setting |
135
|
|
|
*/ |
136
|
|
|
public static function reset(string $category, string $setting, $default = null) |
137
|
|
|
{ |
138
|
|
|
$value = parent::get($category, $setting, $default); |
139
|
|
|
|
140
|
|
|
if ($value === null) { |
141
|
|
|
throw new RuntimeException(sprintf("missing config: %s:%s", $category, $setting)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if (!is_scalar($value)) { |
145
|
|
|
throw new RuntimeException( |
146
|
|
|
sprintf("invalid config: %s:%s, type: %s", $category, $setting, gettype($value)) |
147
|
|
|
); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $value; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|