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