1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Gacela\Framework\Config; |
6
|
|
|
|
7
|
|
|
use Gacela\Framework\Bootstrap\SetupGacela; |
8
|
|
|
use Gacela\Framework\Bootstrap\SetupGacelaInterface; |
9
|
|
|
use Gacela\Framework\Exception\ConfigException; |
10
|
|
|
|
11
|
|
|
use function array_key_exists; |
12
|
|
|
|
13
|
|
|
final class Config implements ConfigInterface |
14
|
|
|
{ |
15
|
|
|
private static ?self $instance = null; |
16
|
|
|
|
17
|
|
|
private ?string $appRootDir = null; |
18
|
|
|
|
19
|
|
|
/** @var array<string,mixed> */ |
20
|
|
|
private array $config = []; |
21
|
|
|
|
22
|
|
|
private ?SetupGacelaInterface $setup = null; |
23
|
|
|
|
24
|
|
|
private ?ConfigFactory $configFactory = null; |
25
|
|
|
|
26
|
|
|
private function __construct() |
27
|
|
|
{ |
28
|
|
|
} |
29
|
|
|
|
30
|
51 |
|
public static function getInstance(): self |
31
|
|
|
{ |
32
|
51 |
|
if (self::$instance === null) { |
33
|
11 |
|
self::$instance = new self(); |
34
|
|
|
} |
35
|
|
|
|
36
|
51 |
|
return self::$instance; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @internal |
41
|
|
|
*/ |
42
|
11 |
|
public static function resetInstance(): void |
43
|
|
|
{ |
44
|
11 |
|
self::$instance = null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param null|mixed $default |
49
|
|
|
* |
50
|
|
|
* @throws ConfigException |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
24 |
|
public function get(string $key, $default = self::DEFAULT_CONFIG_VALUE) |
55
|
|
|
{ |
56
|
24 |
|
if (empty($this->config)) { |
57
|
3 |
|
$this->init(); |
58
|
|
|
} |
59
|
|
|
|
60
|
24 |
|
if ($default !== self::DEFAULT_CONFIG_VALUE && !$this->hasKey($key)) { |
61
|
2 |
|
return $default; |
62
|
|
|
} |
63
|
|
|
|
64
|
22 |
|
if (!$this->hasKey($key)) { |
65
|
1 |
|
throw ConfigException::keyNotFound($key, self::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
21 |
|
return $this->config[$key]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Force loading all config values in memory. |
73
|
|
|
* |
74
|
|
|
* @throws ConfigException |
75
|
|
|
*/ |
76
|
51 |
|
public function init(): void |
77
|
|
|
{ |
78
|
51 |
|
$this->configFactory = null; |
79
|
51 |
|
$this->config = $this->loadAllConfigValues(); |
80
|
51 |
|
$this->config = array_merge($this->config, $this->getSetupGacela()->getConfigKeyValues()); |
81
|
|
|
} |
82
|
|
|
|
83
|
51 |
|
public function setAppRootDir(string $dir): self |
84
|
|
|
{ |
85
|
51 |
|
$this->appRootDir = $dir; |
86
|
|
|
|
87
|
51 |
|
if (empty($this->appRootDir)) { |
88
|
|
|
$this->appRootDir = getcwd() ?: ''; // @codeCoverageIgnore |
89
|
|
|
} |
90
|
|
|
|
91
|
51 |
|
return $this; |
92
|
|
|
} |
93
|
|
|
|
94
|
51 |
|
public function getAppRootDir(): string |
95
|
|
|
{ |
96
|
51 |
|
return $this->appRootDir ?? getcwd() ?: ''; |
97
|
|
|
} |
98
|
|
|
|
99
|
4 |
|
public function getProfilerDir(): string |
100
|
|
|
{ |
101
|
4 |
|
return $this->getAppRootDir() . '/' . $this->getSetupGacela()->getProfilerDir(); |
102
|
|
|
} |
103
|
|
|
|
104
|
51 |
|
public function setSetup(SetupGacelaInterface $setup): self |
105
|
|
|
{ |
106
|
51 |
|
$this->setup = $setup; |
107
|
|
|
|
108
|
51 |
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @internal |
113
|
|
|
*/ |
114
|
51 |
|
public function getFactory(): ConfigFactory |
115
|
|
|
{ |
116
|
51 |
|
if ($this->configFactory === null) { |
117
|
51 |
|
$this->configFactory = new ConfigFactory( |
118
|
51 |
|
$this->getAppRootDir(), |
119
|
51 |
|
$this->getSetupGacela() |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
51 |
|
return $this->configFactory; |
|
|
|
|
124
|
|
|
} |
125
|
|
|
|
126
|
51 |
|
public function getSetupGacela(): SetupGacelaInterface |
127
|
|
|
{ |
128
|
51 |
|
if ($this->setup === null) { |
129
|
|
|
$this->setup = new SetupGacela(); |
130
|
|
|
} |
131
|
|
|
|
132
|
51 |
|
return $this->setup; |
|
|
|
|
133
|
|
|
} |
134
|
|
|
|
135
|
42 |
|
public function hasKey(string $key): bool |
136
|
|
|
{ |
137
|
42 |
|
return array_key_exists($key, $this->config); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return array<string,mixed> |
142
|
|
|
*/ |
143
|
51 |
|
private function loadAllConfigValues(): array |
144
|
|
|
{ |
145
|
51 |
|
return $this->getFactory() |
146
|
51 |
|
->createConfigLoader() |
147
|
51 |
|
->loadAll(); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
|