/
Environment.php
444 lines (389 loc) · 11.8 KB
/
Environment.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
namespace Leantime\Core;
use ArrayAccess;
use Dotenv\Dotenv;
use Exception;
use Illuminate\Contracts\Config\Repository as ConfigContract;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Leantime\Config\Config;
use Symfony\Component\Yaml\Yaml;
/**
* environment - class To handle environment variables
*
* @package leantime
* @subpackage core
*/
class Environment implements ArrayAccess, ConfigContract
{
# Config Files ===============================================================================
/**
* @var Dotenv
*/
public Dotenv $dotenv;
/**
* @var ?object
*/
public ?object $yaml;
/**
* @var Config|null
*/
public ?Config $phpConfig;
# Config ======================================================================================
/**
* @var array The Config
*/
public array $config = [];
/**
* @var array list of legacy mappings
* @todo remove this after deprecating configuration.php
*/
private const LEGACY_MAPPINGS = [
'printLogoUrl' => 'LEAN_PRINT_LOGO_URL',
'primarycolor' => 'LEAN_PRIMARY_COLOR',
'secondarycolor' => 'LEAN_SECONDARY_COLOR',
'sessionpassword' => 'LEAN_SESSION_PASSWORD',
'email' => 'LEAN_EMAIL_RETURN',
'useSMTP' => 'LEAN_EMAIL_USE_SMTP',
'smtpHosts' => 'LEAN_EMAIL_SMTP_HOSTS',
'smtpAuth' => 'LEAN_EMAIL_SMTP_AUTH',
'smtpUsername' => 'LEAN_EMAIL_SMTP_USERNAME',
'smtpPassword' => 'LEAN_EMAIL_SMTP_PASSWORD',
'smtpAutoTLS' => 'LEAN_EMAIL_SMTP_AUTO_TLS',
'smtpSecure' => 'LEAN_EMAIL_SMTP_SECURE',
'smtpPort' => 'LEAN_EMAIL_SMTP_PORT',
'smtpSSLNoverify' => 'LEAN_EMAIL_SMTP_SSLNOVERIFY',
'useLdap' => 'LEAN_LDAP_USE_LDAP',
'ldapType' => 'LEAN_LDAP_LDAP_TYPE',
'ldapLtGroupAssignments' => 'LEAN_LDAP_GROUP_ASSIGNMENT',
'ldapDomain' => 'LEAN_LDAP_LDAP_DOMAIN',
'oidcClientId' => 'LEAN_OIDC_CLIEND_ID',
'oidcClientSecret' => 'LEAN_OIDC_CLIEND_SECRET',
'oidcAuthUrl' => 'LEAN_OIDC_AUTH_URL_OVERRIDE',
'oidcTokenUrl' => 'LEAN_OIDC_TOKEN_URL_OVERRIDE',
'oidcJwksUrl' => 'LEAN_OIDC_JWKS_URL_OVERRIDE',
'oidcUserInfoUrl' => 'LEAN_OIDC_USERINFO_URL_OVERRIDE',
'oidcFieldFirstName' => 'LEAN_OIDC_FIELD_FIRSTNAME',
'oidcFieldLastName' => 'LEAN_OIDC_FIELD_LASTNAME',
'redisURL' => 'LEAN_REDIS_URL',
];
/**
* environment constructor.
* @param DefaultConfig $defaultConfiguration
* @throws Exception
*/
public function __construct(DefaultConfig $defaultConfiguration)
{
if (
isset($_SESSION)
&& (! empty($_SESSION['mainconfig']) || ! $_SESSION['mainconfig']['debug'])
) {
$this->config = $_SESSION['mainconfig'];
return $this;
}
/* PHP */
$this->phpConfig = null;
if (file_exists($phpConfigFile = APP_ROOT . "/config/configuration.php")) {
require_once $phpConfigFile;
if (! class_exists(Config::class)) {
throw new Exception("We found a php configuration file but the class cannot be instantiated. Please check the configuration file for namespace and class name. You can use the configuration.sample.php as a template. See https://github.com/leantime/leantime/releases/tag/v2.4-beta-2 for more details.");
}
$this->phpConfig = new Config();
}
/* Dotenv */
$this->dotenv = Dotenv::createImmutable(APP_ROOT . "/config");
$this->dotenv->safeLoad();
/* YAML */
$this->yaml = null;
if (file_exists(APP_ROOT . "/config/config.yaml")) {
$this->yaml = Yaml::parseFile(APP_ROOT . "/config/config.yaml");
}
$defaultConfigurationProperties = get_class_vars($defaultConfiguration::class);
foreach (array_keys($defaultConfigurationProperties) as $propertyName) {
$type = gettype($defaultConfigurationProperties[$propertyName]);
$type = $type == 'NULL' ? 'string' : $type;
$this->config[$propertyName] = $this->environmentHelper(
envVar: self::LEGACY_MAPPINGS[$propertyName] ?? 'LEAN_' . Str::of($propertyName)->snake()->upper()->toString(),
default: $defaultConfigurationProperties[$propertyName],
dataType: $type,
);
}
Events::add_event_listener(
'leantime.core.bootloader.getApplication.session_initialized',
function () {
$_SESSION['mainconfig'] = $this->config;
}
);
}
/**
* getBool - get a boolean value from the environment
*
* @param string $envVar
* @param bool $default
* @return bool
*/
private function getBool(string $envVar, bool $default): bool
{
return $this->environmentHelper($envVar, $default, 'boolean');
}
/**
* getString - get a string value from the environment
*
* @param string $envVar
* @param string $default
* @return string
*/
private function getString(string $envVar, string $default = ''): string
{
return $this->environmentHelper($envVar, $default, 'string');
}
/**
* environmentHelper - helper function to get a value from the environment
*
* @param string $envVar
* @param mixed $default
* @param string $dataType
* @return mixed
*/
private function environmentHelper(string $envVar, mixed $default, string $dataType = "string"): mixed
{
/**
* Basically, here, we are doing the fetch order of
* environment -> .env file -> yaml file -> user default -> leantime default
* This allows us to use any one or a combination of those methods to configure leantime.
*/
$found = $default;
$found = $this->tryGetFromPhp($envVar, $found) ?? $found;
$found = $this->tryGetFromYaml($envVar, $found) ?? $found;
$found = $this->tryGetFromEnvironment($envVar, $found) ?? $found;
// we need to check to see if we need to convert the found data
return match ($dataType) {
"string" => $found,
"boolean" => $found == "true",
"number" => intval($found),
default => $found,
};
}
/**
* @param string $envVar
* @param mixed $currentValue
* @return mixed
*/
private function tryGetFromPhp(string $envVar, mixed $currentValue): mixed
{
if ($this->phpConfig) {
$key = array_search($envVar, self::LEGACY_MAPPINGS) ?: Str::of($envVar)->replace('LEAN_', '')->lower()->camel()->toString();
return $this->phpConfig->$key ?? $currentValue;
}
return null;
}
/**
* tryGetFromEnvironment - try to get a value from the environment
*
* @param string $envVar
* @param mixed $currentValue
* @return mixed
*/
private function tryGetFromEnvironment(string $envVar, mixed $currentValue): mixed
{
return $_ENV[$envVar] ?? $currentValue;
}
/**
* tryGetFromYaml - try to get a value from the yaml file
*
* @param string $envVar
* @param mixed $currentValue
* @return mixed
*/
private function tryGetFromYaml(string $envVar, mixed $currentValue): mixed
{
if ($this->yaml) {
$key = strtolower(preg_replace('/^LEAN_/', '', $envVar));
return $this->yaml[$key] ?? $currentValue;
}
return null;
}
/**
* Determine if the given configuration value exists.
*
* @param string $key
* @return bool
*/
public function has($key): bool
{
return Arr::has($_SESSION['mainconfig'] ?? [], $key) || Arr::has($this->config, $key);
}
/**
* Get the specified configuration value.
*
* @param array|string $key
* @param mixed $default
* @return mixed
*/
public function get($key, $default = null): mixed
{
if (is_array($key)) {
return $this->getMany($key);
}
return Arr::get($_SESSION['mainconfig'] ?? [], $key, Arr::get(
$this->config,
$key,
$default
));
}
/**
* Get many configuration values.
*
* @param array $keys
* @return array
*/
public function getMany(array $keys): array
{
$config = [];
foreach ($keys as $key => $default) {
if (is_numeric($key)) {
[$key, $default] = [$default, null];
}
$config[$key] = $this->get($key, $default);
}
return $config;
}
/**
* Set a given configuration value.
*
* @param array|string $key
* @param mixed $value
* @return void
*/
public function set($key, $value = null): void
{
$keys = is_array($key) ? $key : [$key => $value];
foreach ($keys as $key => $value) {
Arr::set($this->config, $key, $value);
# this basically checks to see if the session is initialized
if (isset($_SESSION['mainconfig'])) {
Arr::set($_SESSION['mainconfig'], $key, $value);
}
}
}
/**
* Prepend a value onto an array configuration value.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function prepend($key, $value): void
{
$array = $this->get($key, []);
array_unshift($array, $value);
$this->set($key, $array);
}
/**
* Push a value onto an array configuration value.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function push($key, $value): void
{
$array = $this->get($key, []);
$array[] = $value;
$this->set($key, $array);
}
/**
* Get all of the configuration items for the application.
*
* @return array
*/
public function all(): array
{
return $this->items;
}
/**
* Determine if the given configuration option exists.
*
* @param string $key
* @return bool
*/
public function offsetExists($key): bool
{
return $this->has($key);
}
/**
* Get a configuration option.
*
* @param string $key
* @return mixed
*/
public function offsetGet($key): mixed
{
return $this->get($key);
}
/**
* Set a configuration option.
*
* @param string $key
* @param mixed $value
* @return void
*/
public function offsetSet($key, mixed $value): void
{
$this->set($key, $value);
}
/**
* Unset a configuration option.
*
* @param string $key
* @return void
*/
public function offsetUnset($key): void
{
$this->set($key, null);
}
/**
* Dynamically access the configuration using object syntax.
*
* @param string $key
* @return mixed
*/
public function __get(string $key): mixed
{
return $this->get($key);
}
/**
* Dynamically set the configuration using object syntax.
*
* @param string $key
* @param mixed $value
*
* @return void
*/
public function __set(string $key, mixed $value): void
{
$this->set($key, $value);
}
/**
* Dynamically check if a configuration option is set using object syntax.
*
* @param string $key
*
* @return bool
*/
public function __isset(string $key): bool
{
return $this->has($key) && $this->get($key) !== null;
}
/**
* Dynamically unset a configuration option using object syntax.
*
* @param string $key
*
* @return void
*/
public function __unset(string $key): void
{
$this->set($key, null);
}
}