|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace alkemann\h2l; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class Environment |
|
7
|
|
|
* |
|
8
|
|
|
* @package alkemann\h2l |
|
9
|
|
|
*/ |
|
10
|
|
|
final class Environment |
|
11
|
|
|
{ |
|
12
|
|
|
public const ALL = "ALL"; |
|
13
|
|
|
public const LOCAL = "LOCAL"; |
|
14
|
|
|
public const DEV = "DEV"; |
|
15
|
|
|
public const TEST = "TEST"; |
|
16
|
|
|
public const PROD = "PROD"; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var array<string, array<string, mixed>> |
|
20
|
|
|
*/ |
|
21
|
|
|
private static array $settings = [ |
|
22
|
|
|
Environment::LOCAL => [ |
|
23
|
|
|
'debug' => true, |
|
24
|
|
|
], |
|
25
|
|
|
Environment::DEV => [ |
|
26
|
|
|
'debug' => true, |
|
27
|
|
|
], |
|
28
|
|
|
Environment::TEST => [ |
|
29
|
|
|
'debug' => false, |
|
30
|
|
|
], |
|
31
|
|
|
Environment::PROD => [ |
|
32
|
|
|
'debug' => false, |
|
33
|
|
|
] |
|
34
|
|
|
]; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var array<string, array<callable>> |
|
38
|
|
|
*/ |
|
39
|
|
|
private static array $middlewares = [ |
|
40
|
|
|
Environment::LOCAL => [], |
|
41
|
|
|
Environment::DEV => [], |
|
42
|
|
|
Environment::TEST => [], |
|
43
|
|
|
Environment::PROD => [] |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Defaults to DEV |
|
48
|
|
|
* @var string |
|
49
|
|
|
*/ |
|
50
|
|
|
private static string $current_env = Environment::DEV; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Changes current environment to provided name |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $env |
|
56
|
|
|
*/ |
|
57
|
|
|
public static function setEnvironment(string $env): void |
|
58
|
|
|
{ |
|
59
|
|
|
self::$current_env = $env; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns the name of the current Environment |
|
64
|
|
|
* |
|
65
|
|
|
* @return string |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function current(): string |
|
68
|
|
|
{ |
|
69
|
|
|
return self::$current_env; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Add a middleware to the environment, you can then register them to Dispatcher later based on the env. |
|
74
|
|
|
* |
|
75
|
|
|
* Middlewares should be closures that matches: `function(Request $request, Chain $chain): ?Response` |
|
76
|
|
|
* |
|
77
|
|
|
* @param callable $mw A callable closure that matches the middleware interface |
|
78
|
|
|
* @param null|string $environment if not specified, configures CURRENT environment only. E::ALL, sets for all envs. |
|
79
|
|
|
*/ |
|
80
|
|
|
public static function addMiddle(callable $mw, ?string $environment = null): void |
|
81
|
|
|
{ |
|
82
|
|
|
$environment = $environment ?? static::current(); |
|
83
|
|
|
if ($environment === Environment::ALL) { |
|
84
|
|
|
foreach (array_keys(self::$settings) as $env) { |
|
85
|
|
|
self::$middlewares[$env][] = $mw; |
|
86
|
|
|
} |
|
87
|
|
|
} else { |
|
88
|
|
|
self::$middlewares[$environment][] = $mw; |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get all middlewares, mostly to be sent to Dispatch::registerMiddleware |
|
94
|
|
|
* |
|
95
|
|
|
* @return array<callable> callables that match the middleware interface |
|
96
|
|
|
*/ |
|
97
|
|
|
public static function middlewares(): array |
|
98
|
|
|
{ |
|
99
|
|
|
return self::$middlewares[static::current()]; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* Get a config value from the current environment, with a default value if not set |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $config_name |
|
106
|
|
|
* @param mixed $default Value to return in named config is not configured |
|
107
|
|
|
* @return mixed|null returns `null` if config is not set or no default is specified |
|
108
|
|
|
*/ |
|
109
|
|
|
public static function get(string $config_name, $default = null) |
|
110
|
|
|
{ |
|
111
|
|
|
$environment = static::current(); |
|
112
|
|
|
if (!array_key_exists($environment, self::$settings) |
|
113
|
|
|
|| !array_key_exists($config_name, self::$settings[$environment]) |
|
114
|
|
|
) { |
|
115
|
|
|
return $default; |
|
116
|
|
|
} |
|
117
|
|
|
return self::$settings[$environment][$config_name]; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Returns an array of all environment variables for selected env (or all) |
|
122
|
|
|
* |
|
123
|
|
|
* @param null|string $environment Name of environment to grab, or Environment::ALL |
|
124
|
|
|
* @return array<mixed> all configurations for selected env in deep array |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function grab(?string $environment = null): array |
|
127
|
|
|
{ |
|
128
|
|
|
if (is_null($environment)) { |
|
129
|
|
|
$environment = static::current(); |
|
130
|
|
|
} |
|
131
|
|
|
if ($environment === Environment::ALL) { |
|
132
|
|
|
return self::$settings; |
|
133
|
|
|
} |
|
134
|
|
|
return self::$settings[$environment] ?? []; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Sets ONE config value to current, specified or all environments |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $config_name |
|
141
|
|
|
* @param mixed $value |
|
142
|
|
|
* @param null|string $environment if not specified, configures CURRENT environment only. E::ALL, sets for all envs. |
|
143
|
|
|
*/ |
|
144
|
|
|
public static function put(string $config_name, $value, ?string $environment = null): void |
|
145
|
|
|
{ |
|
146
|
|
|
if (is_null($environment)) { |
|
147
|
|
|
$environment = static::current(); |
|
148
|
|
|
} |
|
149
|
|
|
if ($environment === Environment::ALL) { |
|
150
|
|
|
foreach (array_keys(self::$settings) as $env) { |
|
151
|
|
|
static::put($config_name, $value, $env); |
|
152
|
|
|
} |
|
153
|
|
|
return; |
|
154
|
|
|
} |
|
155
|
|
|
if (!array_key_exists($environment, self::$settings)) { |
|
156
|
|
|
self::$settings[$environment] = []; |
|
157
|
|
|
} |
|
158
|
|
|
self::$settings[$environment][$config_name] = $value; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Array merges `$config` with current value of the specified or current environments existing configs |
|
163
|
|
|
* |
|
164
|
|
|
* @param array<string, mixed> $configs |
|
165
|
|
|
* @param null|string $environment |
|
166
|
|
|
*/ |
|
167
|
|
|
public static function add(array $configs, ?string $environment = null): void |
|
168
|
|
|
{ |
|
169
|
|
|
if (is_null($environment)) { |
|
170
|
|
|
$environment = static::current(); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
if ($environment === Environment::ALL) { |
|
174
|
|
|
foreach (array_keys(self::$settings) as $env) { |
|
175
|
|
|
static::add($configs, $env); |
|
176
|
|
|
} |
|
177
|
|
|
static::add($configs, Environment::current()); |
|
178
|
|
|
return; |
|
179
|
|
|
} |
|
180
|
|
|
foreach ($configs as $key => $value) { |
|
181
|
|
|
self::$settings[$environment][$key] = $value; |
|
182
|
|
|
} |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Completely replaces the set of configurations that the current or specified environment has. |
|
187
|
|
|
* |
|
188
|
|
|
* @param array<string, mixed> $configs |
|
189
|
|
|
* @param null|string $environment |
|
190
|
|
|
*/ |
|
191
|
|
|
public static function set(array $configs, ?string $environment = null): void |
|
192
|
|
|
{ |
|
193
|
|
|
if (is_null($environment)) { |
|
194
|
|
|
$environment = static::current(); |
|
195
|
|
|
} |
|
196
|
|
|
if ($environment === Environment::ALL) { |
|
197
|
|
|
self::$settings = $configs; |
|
198
|
|
|
} else { |
|
199
|
|
|
self::$settings[$environment] = $configs; |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|