1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\Config\ConfigInit; |
8
|
|
|
use Gacela\Framework\Config\ConfigReader\EnvConfigReader; |
9
|
|
|
use Gacela\Framework\Config\ConfigReader\PhpConfigReader; |
10
|
|
|
use Gacela\Framework\Config\ConfigReaderInterface; |
11
|
|
|
use Gacela\Framework\Config\GacelaJsonConfigFactory; |
12
|
|
|
use Gacela\Framework\Config\GacelaJsonConfigFactoryInterface; |
13
|
|
|
use Gacela\Framework\Config\PathFinder; |
14
|
|
|
use Gacela\Framework\Config\PathFinderInterface; |
15
|
|
|
use Gacela\Framework\Exception\ConfigException; |
16
|
|
|
|
17
|
|
|
final class Config |
18
|
|
|
{ |
19
|
|
|
private const GACELA_CONFIG_FILENAME = 'gacela.json'; |
20
|
|
|
|
21
|
|
|
private static string $applicationRootDir = ''; |
22
|
|
|
|
23
|
|
|
private static ?self $instance = null; |
24
|
|
|
|
25
|
|
|
private array $config = []; |
26
|
|
|
|
27
|
|
|
/** @var array<string, ConfigReaderInterface> */ |
28
|
|
|
private array $configReaders; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array<string, ConfigReaderInterface> $configReaders |
32
|
|
|
*/ |
33
|
4 |
|
private function __construct(array $configReaders) |
34
|
|
|
{ |
35
|
4 |
|
$this->configReaders = $configReaders; |
36
|
4 |
|
} |
37
|
|
|
|
38
|
7 |
|
public static function getInstance(): self |
39
|
|
|
{ |
40
|
7 |
|
if (self::$instance === null) { |
41
|
4 |
|
self::$instance = new self([ |
42
|
4 |
|
'php' => new PhpConfigReader(), |
43
|
4 |
|
'env' => new EnvConfigReader(), |
44
|
|
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
7 |
|
return self::$instance; |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
public static function resetInstance(): void |
51
|
|
|
{ |
52
|
3 |
|
self::$instance = null; |
53
|
3 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array<string, ConfigReaderInterface> $configReaders |
57
|
|
|
*/ |
58
|
1 |
|
public function setConfigReaders(array $configReaders = []): void |
59
|
|
|
{ |
60
|
1 |
|
$this->configReaders = $configReaders; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
4 |
|
public static function setApplicationRootDir(string $dir): void |
64
|
|
|
{ |
65
|
4 |
|
self::$applicationRootDir = $dir; |
66
|
4 |
|
} |
67
|
|
|
|
68
|
7 |
|
public static function getApplicationRootDir(): string |
69
|
|
|
{ |
70
|
7 |
|
if (empty(self::$applicationRootDir)) { |
71
|
1 |
|
self::$applicationRootDir = getcwd() ?: ''; |
72
|
|
|
} |
73
|
|
|
|
74
|
7 |
|
return self::$applicationRootDir; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param null|mixed $default |
79
|
|
|
* |
80
|
|
|
* @throws ConfigException |
81
|
|
|
* |
82
|
|
|
* @return mixed |
83
|
|
|
*/ |
84
|
7 |
|
public function get(string $key, $default = null) |
85
|
|
|
{ |
86
|
7 |
|
if (empty($this->config)) { |
87
|
2 |
|
$this->init(); |
88
|
|
|
} |
89
|
|
|
|
90
|
7 |
|
if ($default !== null && !$this->hasValue($key)) { |
91
|
1 |
|
return $default; |
92
|
|
|
} |
93
|
|
|
|
94
|
6 |
|
if (!$this->hasValue($key)) { |
95
|
1 |
|
throw ConfigException::keyNotFound($key, self::class); |
96
|
|
|
} |
97
|
|
|
|
98
|
5 |
|
return $this->config[$key]; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @throws ConfigException |
103
|
|
|
*/ |
104
|
7 |
|
public function init(): void |
105
|
|
|
{ |
106
|
7 |
|
$this->config = (new ConfigInit( |
107
|
7 |
|
self::getApplicationRootDir(), |
108
|
7 |
|
$this->createGacelaJsonConfigCreator(), |
109
|
7 |
|
$this->createPathFinder(), |
110
|
7 |
|
$this->configReaders |
111
|
7 |
|
))->readAll(); |
112
|
7 |
|
} |
113
|
|
|
|
114
|
7 |
|
private function createGacelaJsonConfigCreator(): GacelaJsonConfigFactoryInterface |
115
|
|
|
{ |
116
|
7 |
|
return new GacelaJsonConfigFactory( |
117
|
7 |
|
self::$applicationRootDir, |
118
|
7 |
|
self::GACELA_CONFIG_FILENAME |
119
|
|
|
); |
120
|
|
|
} |
121
|
|
|
|
122
|
7 |
|
private function createPathFinder(): PathFinderInterface |
123
|
|
|
{ |
124
|
7 |
|
return new PathFinder(); |
125
|
|
|
} |
126
|
|
|
|
127
|
7 |
|
private function hasValue(string $key): bool |
128
|
|
|
{ |
129
|
7 |
|
return isset($this->config[$key]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|