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
|
|
|
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 = 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
|
30 |
|
public static function getInstance(): self |
31
|
|
|
{ |
32
|
30 |
|
if (self::$instance === null) { |
33
|
3 |
|
self::$instance = new self(); |
34
|
|
|
} |
35
|
|
|
|
36
|
30 |
|
return self::$instance; |
|
|
|
|
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @internal for testing purposes |
41
|
|
|
*/ |
42
|
3 |
|
public static function resetInstance(): void |
43
|
|
|
{ |
44
|
3 |
|
self::$instance = null; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param null|mixed $default |
49
|
|
|
* |
50
|
|
|
* @throws ConfigException |
51
|
|
|
* |
52
|
|
|
* @return mixed |
53
|
|
|
*/ |
54
|
13 |
|
public function get(string $key, $default = self::DEFAULT_CONFIG_VALUE) |
55
|
|
|
{ |
56
|
13 |
|
if (empty($this->config)) { |
57
|
3 |
|
$this->init(); |
58
|
|
|
} |
59
|
|
|
|
60
|
13 |
|
if ($default !== self::DEFAULT_CONFIG_VALUE && !$this->hasValue($key)) { |
61
|
2 |
|
return $default; |
62
|
|
|
} |
63
|
|
|
|
64
|
11 |
|
if (!$this->hasValue($key)) { |
65
|
1 |
|
throw ConfigException::keyNotFound($key, self::class); |
66
|
|
|
} |
67
|
|
|
|
68
|
10 |
|
return $this->config[$key]; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Force loading all config values in memory. |
73
|
|
|
* |
74
|
|
|
* @throws ConfigException |
75
|
|
|
*/ |
76
|
29 |
|
public function init(): void |
77
|
|
|
{ |
78
|
29 |
|
$this->configFactory = null; |
79
|
29 |
|
$this->config = $this->loadAllConfigValues(); |
80
|
|
|
} |
81
|
|
|
|
82
|
26 |
|
public function setAppRootDir(string $dir): self |
83
|
|
|
{ |
84
|
26 |
|
$this->appRootDir = $dir; |
85
|
|
|
|
86
|
26 |
|
if (empty($this->appRootDir)) { |
87
|
|
|
$this->appRootDir = getcwd() ?: ''; // @codeCoverageIgnore |
88
|
|
|
} |
89
|
|
|
|
90
|
26 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
29 |
|
public function getAppRootDir(): string |
94
|
|
|
{ |
95
|
29 |
|
return $this->appRootDir ?? getcwd() ?: ''; |
96
|
|
|
} |
97
|
|
|
|
98
|
26 |
|
public function setSetup(SetupGacelaInterface $setup): self |
99
|
|
|
{ |
100
|
26 |
|
$this->setup = $setup; |
101
|
|
|
|
102
|
26 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @internal |
107
|
|
|
*/ |
108
|
30 |
|
public function getFactory(): ConfigFactory |
109
|
|
|
{ |
110
|
30 |
|
if ($this->configFactory === null) { |
111
|
29 |
|
$this->configFactory = new ConfigFactory( |
112
|
29 |
|
$this->getAppRootDir(), |
113
|
29 |
|
$this->setup ?? new SetupGacela() |
114
|
|
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
30 |
|
return $this->configFactory; |
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
120
|
13 |
|
private function hasValue(string $key): bool |
121
|
|
|
{ |
122
|
13 |
|
return isset($this->config[$key]); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return array<string,mixed> |
127
|
|
|
*/ |
128
|
29 |
|
private function loadAllConfigValues(): array |
129
|
|
|
{ |
130
|
29 |
|
return $this->getFactory() |
131
|
29 |
|
->createConfigLoader() |
132
|
29 |
|
->loadAll(); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|