|
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
|
4 |
|
public static function getInstance(): self |
|
28
|
|
|
{ |
|
29
|
4 |
|
if (self::$instance === null) { |
|
30
|
1 |
|
self::$instance = new self(); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
4 |
|
return self::$instance; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
4 |
|
public static function setApplicationRootDir(string $dir): void |
|
37
|
|
|
{ |
|
38
|
4 |
|
self::$applicationRootDir = $dir; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
public static function getApplicationRootDir(): string |
|
42
|
|
|
{ |
|
43
|
4 |
|
if (empty(self::$applicationRootDir)) { |
|
44
|
|
|
self::$applicationRootDir = getcwd() ?: ''; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
4 |
|
return self::$applicationRootDir; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param null|mixed $default |
|
52
|
|
|
* |
|
53
|
|
|
* @throws ConfigException |
|
54
|
|
|
* |
|
55
|
|
|
* @return mixed |
|
56
|
|
|
*/ |
|
57
|
4 |
|
public function get(string $key, $default = null) |
|
58
|
|
|
{ |
|
59
|
4 |
|
if (empty($this->config)) { |
|
60
|
|
|
$this->init(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
4 |
|
if ($default !== null && !$this->hasValue($key)) { |
|
64
|
|
|
return $default; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
4 |
|
if (!$this->hasValue($key)) { |
|
68
|
|
|
throw ConfigException::keyNotFound($key, self::class); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
4 |
|
return $this->config[$key]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @throws ConfigException |
|
76
|
|
|
*/ |
|
77
|
4 |
|
public function init(): void |
|
78
|
|
|
{ |
|
79
|
4 |
|
$this->config = (new ConfigInit( |
|
80
|
4 |
|
self::getApplicationRootDir(), |
|
81
|
4 |
|
$this->createGacelaJsonConfigCreator(), |
|
82
|
4 |
|
$this->createPathFinder(), |
|
83
|
4 |
|
$this->createConfigReaders() |
|
84
|
4 |
|
))->readAll(); |
|
85
|
4 |
|
} |
|
86
|
|
|
|
|
87
|
4 |
|
private function createGacelaJsonConfigCreator(): GacelaJsonConfigFactoryInterface |
|
88
|
|
|
{ |
|
89
|
4 |
|
return new GacelaJsonConfigFactory( |
|
90
|
4 |
|
self::$applicationRootDir, |
|
91
|
4 |
|
self::GACELA_CONFIG_FILENAME |
|
92
|
|
|
); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
4 |
|
private function createPathFinder(): PathFinderInterface |
|
96
|
|
|
{ |
|
97
|
4 |
|
return new PathFinder(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return array<string, ConfigReaderInterface> |
|
102
|
|
|
*/ |
|
103
|
4 |
|
private function createConfigReaders(): array |
|
104
|
|
|
{ |
|
105
|
|
|
return [ |
|
106
|
4 |
|
'php' => new PhpConfigReader(), |
|
107
|
4 |
|
'env' => new EnvConfigReader(), |
|
108
|
|
|
]; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
4 |
|
private function hasValue(string $key): bool |
|
112
|
|
|
{ |
|
113
|
4 |
|
return isset($this->config[$key]); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
|