1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Pageon; |
4
|
|
|
|
5
|
|
|
use Dotenv\Dotenv; |
6
|
|
|
use Dotenv\Exception\InvalidPathException; |
7
|
|
|
use Illuminate\Support\Arr; |
8
|
|
|
use Iterator; |
9
|
|
|
use Stitcher\Exception\InvalidConfiguration; |
10
|
|
|
use Stitcher\File; |
11
|
|
|
use Symfony\Component\Finder\Finder; |
12
|
|
|
|
13
|
|
|
class Config |
14
|
|
|
{ |
15
|
|
|
protected static $env; |
16
|
|
|
protected static $loadedConfiguration = []; |
17
|
|
|
|
18
|
7 |
|
public static function init() |
19
|
|
|
{ |
20
|
7 |
|
self::$env = new Dotenv(File::path()); |
21
|
|
|
|
22
|
|
|
try { |
23
|
7 |
|
self::$env->load(); |
24
|
|
|
} catch (InvalidPathException $e) { |
25
|
|
|
throw InvalidConfiguration::dotEnvNotFound(File::path()); |
26
|
|
|
} |
27
|
|
|
|
28
|
7 |
|
$configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator(); |
29
|
|
|
|
30
|
7 |
|
self::loadDefaults(); |
31
|
|
|
|
32
|
7 |
|
$loadedConfiguration = self::load($configurationFiles); |
|
|
|
|
33
|
|
|
|
34
|
7 |
|
self::$loadedConfiguration = array_merge(self::$loadedConfiguration, Arr::dot($loadedConfiguration)); |
35
|
7 |
|
} |
36
|
|
|
|
37
|
4 |
|
public static function get(string $key) |
38
|
|
|
{ |
39
|
4 |
|
return self::$loadedConfiguration[$key] ?? null; |
40
|
|
|
} |
41
|
|
|
|
42
|
2 |
|
public static function all(): array |
43
|
|
|
{ |
44
|
2 |
|
return self::$loadedConfiguration; |
45
|
|
|
} |
46
|
|
|
|
47
|
7 |
|
protected static function loadDefaults(): void |
48
|
|
|
{ |
49
|
7 |
|
self::$loadedConfiguration['rootDirectory'] = File::path(); |
50
|
7 |
|
self::$loadedConfiguration['templateRenderer'] = 'twig'; |
51
|
7 |
|
} |
52
|
|
|
|
53
|
7 |
|
protected static function load(Iterator $configurationFiles): array |
54
|
|
|
{ |
55
|
7 |
|
$loadedConfiguration = []; |
56
|
|
|
|
57
|
7 |
|
foreach ($configurationFiles as $configurationFile) { |
58
|
7 |
|
$loadedFileConfiguration = require $configurationFile; |
59
|
|
|
|
60
|
7 |
|
if (!is_array($loadedFileConfiguration)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
$loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration); |
65
|
|
|
} |
66
|
|
|
|
67
|
7 |
|
return $loadedConfiguration; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.