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
|
15 |
|
public static function init(): void |
20
|
|
|
{ |
21
|
15 |
|
self::$env = new Dotenv(File::path()); |
22
|
|
|
|
23
|
|
|
try { |
24
|
15 |
|
self::$env->load(); |
25
|
|
|
} catch (InvalidPathException $e) { |
26
|
|
|
throw InvalidConfiguration::dotEnvNotFound(File::path()); |
27
|
|
|
} |
28
|
|
|
|
29
|
15 |
|
$loadedConfiguration = []; |
30
|
|
|
|
31
|
15 |
View Code Duplication |
if (is_dir(File::path('config'))) { |
|
|
|
|
32
|
15 |
|
$configurationFiles = Finder::create()->files()->in(File::path('config'))->name('*.php')->getIterator(); |
33
|
|
|
|
34
|
15 |
|
$loadedConfiguration = array_merge($loadedConfiguration, self::load($configurationFiles)); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
15 |
View Code Duplication |
if (file_exists(File::path('src/config.php'))) { |
|
|
|
|
38
|
|
|
$sourceConfigurationFile = Finder::create()->files()->in(File::path('src'))->name('config.php')->getIterator(); |
39
|
|
|
|
40
|
|
|
$loadedConfiguration = array_merge($loadedConfiguration, self::load($sourceConfigurationFile)); |
|
|
|
|
41
|
|
|
} |
42
|
|
|
|
43
|
15 |
|
self::registerPlugins($loadedConfiguration); |
44
|
|
|
|
45
|
15 |
|
self::registerConfiguration($loadedConfiguration); |
46
|
15 |
|
} |
47
|
|
|
|
48
|
7 |
|
public static function get(string $key) |
49
|
|
|
{ |
50
|
7 |
|
return self::$loadedConfiguration[$key] ?? null; |
51
|
|
|
} |
52
|
|
|
|
53
|
9 |
|
public static function all(): array |
54
|
|
|
{ |
55
|
9 |
|
return self::$loadedConfiguration; |
56
|
|
|
} |
57
|
|
|
|
58
|
9 |
|
public static function plugins(): array |
59
|
|
|
{ |
60
|
9 |
|
return self::$plugins; |
61
|
|
|
} |
62
|
|
|
|
63
|
15 |
|
protected static function defaults(): array |
64
|
|
|
{ |
65
|
|
|
return [ |
66
|
15 |
|
'rootDirectory' => File::path(), |
67
|
15 |
|
'resourcesPath' => File::path('resources'), |
68
|
15 |
|
'templateRenderer' => 'twig', |
69
|
|
|
'staticFiles' => [], |
70
|
|
|
'cacheStaticFiles' => false, |
71
|
|
|
'cacheImages' => true, |
72
|
15 |
|
'siteUrl' => '', |
73
|
|
|
'errorPages' => [], |
74
|
|
|
'minify' => false, |
75
|
|
|
]; |
76
|
|
|
} |
77
|
|
|
|
78
|
15 |
|
protected static function load(Iterator $configurationFiles): array |
79
|
|
|
{ |
80
|
15 |
|
$loadedConfiguration = []; |
81
|
|
|
|
82
|
15 |
|
foreach ($configurationFiles as $configurationFile) { |
83
|
15 |
|
$loadedFileConfiguration = require $configurationFile; |
84
|
|
|
|
85
|
15 |
|
if (! \is_array($loadedFileConfiguration)) { |
86
|
|
|
continue; |
87
|
|
|
} |
88
|
|
|
|
89
|
15 |
|
$loadedConfiguration = array_merge($loadedConfiguration, $loadedFileConfiguration); |
90
|
|
|
} |
91
|
|
|
|
92
|
15 |
|
return $loadedConfiguration; |
93
|
|
|
} |
94
|
|
|
|
95
|
15 |
|
protected static function registerPlugins(array $loadedConfiguration): void |
96
|
|
|
{ |
97
|
15 |
|
self::$plugins = $loadedConfiguration['plugins'] ?? []; |
98
|
15 |
|
} |
99
|
|
|
|
100
|
15 |
|
protected static function registerConfiguration(array $loadedConfiguration): void |
101
|
|
|
{ |
102
|
15 |
|
self::$loadedConfiguration = array_merge( |
103
|
15 |
|
self::defaults(), |
104
|
15 |
|
$loadedConfiguration, |
105
|
15 |
|
Arr::dot($loadedConfiguration) |
106
|
|
|
); |
107
|
15 |
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.