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