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
|
|
|
protected static $plugins = []; |
18
|
|
|
|
19
|
14 |
|
public static function init() |
20
|
|
|
{ |
21
|
14 |
|
self::$env = new Dotenv(File::path()); |
22
|
|
|
|
23
|
|
|
try { |
24
|
14 |
|
self::$env->load(); |
25
|
|
|
} catch (InvalidPathException $e) { |
26
|
|
|
throw InvalidConfiguration::dotEnvNotFound(File::path()); |
27
|
|
|
} |
28
|
|
|
|
29
|
14 |
|
$configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator(); |
30
|
|
|
|
31
|
14 |
|
$loadedConfiguration = self::load($configurationFiles); |
|
|
|
|
32
|
|
|
|
33
|
14 |
|
self::registerPlugins($loadedConfiguration); |
34
|
|
|
|
35
|
14 |
|
self::registerConfiguration($loadedConfiguration); |
36
|
14 |
|
} |
37
|
|
|
|
38
|
6 |
|
public static function get(string $key) |
39
|
|
|
{ |
40
|
6 |
|
return self::$loadedConfiguration[$key] ?? null; |
41
|
|
|
} |
42
|
|
|
|
43
|
8 |
|
public static function all(): array |
44
|
|
|
{ |
45
|
8 |
|
return self::$loadedConfiguration; |
46
|
|
|
} |
47
|
|
|
|
48
|
8 |
|
public static function plugins(): array |
49
|
|
|
{ |
50
|
8 |
|
return self::$plugins; |
51
|
|
|
} |
52
|
|
|
|
53
|
14 |
|
protected static function defaults(): array |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
14 |
|
'rootDirectory' => File::path(), |
57
|
14 |
|
'templateRenderer' => 'twig', |
58
|
|
|
'staticFiles' => [], |
59
|
|
|
'cacheStaticFiles' => false, |
60
|
|
|
]; |
61
|
|
|
} |
62
|
|
|
|
63
|
14 |
|
protected static function load(Iterator $configurationFiles): array |
64
|
|
|
{ |
65
|
14 |
|
$loadedConfiguration = []; |
66
|
|
|
|
67
|
14 |
|
foreach ($configurationFiles as $configurationFile) { |
68
|
14 |
|
$loadedFileConfiguration = require $configurationFile; |
69
|
|
|
|
70
|
14 |
|
if (! is_array($loadedFileConfiguration)) { |
71
|
|
|
continue; |
72
|
|
|
} |
73
|
|
|
|
74
|
14 |
|
$loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration); |
75
|
|
|
} |
76
|
|
|
|
77
|
14 |
|
return $loadedConfiguration; |
78
|
|
|
} |
79
|
|
|
|
80
|
14 |
|
protected static function registerPlugins(array $loadedConfiguration): void |
81
|
|
|
{ |
82
|
14 |
|
self::$plugins = $loadedConfiguration['plugins'] ?? []; |
83
|
14 |
|
} |
84
|
|
|
|
85
|
14 |
|
protected static function registerConfiguration(array $loadedConfiguration): void |
86
|
|
|
{ |
87
|
14 |
|
self::$loadedConfiguration = array_merge( |
88
|
14 |
|
self::defaults(), |
89
|
14 |
|
$loadedConfiguration, |
90
|
14 |
|
Arr::dot($loadedConfiguration) |
91
|
|
|
); |
92
|
14 |
|
} |
93
|
|
|
} |
94
|
|
|
|
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.