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