|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Valkyrja Framework package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Melech Mizrachi <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Valkyrja\Application\Config; |
|
15
|
|
|
|
|
16
|
|
|
use Valkyrja\Api\Config as Api; |
|
17
|
|
|
use Valkyrja\Application\Config as App; |
|
18
|
|
|
use Valkyrja\Asset\Config as Asset; |
|
19
|
|
|
use Valkyrja\Auth\Config as Auth; |
|
20
|
|
|
use Valkyrja\Broadcast\Config as Broadcast; |
|
21
|
|
|
use Valkyrja\Cache\Config as Cache; |
|
22
|
|
|
use Valkyrja\Cli\Config as Cli; |
|
23
|
|
|
use Valkyrja\Config\Config; |
|
24
|
|
|
use Valkyrja\Config\Exception\InvalidArgumentException; |
|
25
|
|
|
use Valkyrja\Config\Exception\RuntimeException; |
|
26
|
|
|
use Valkyrja\Container\Config as Container; |
|
27
|
|
|
use Valkyrja\Crypt\Config as Crypt; |
|
28
|
|
|
use Valkyrja\Event\Config as Event; |
|
29
|
|
|
use Valkyrja\Filesystem\Config as Filesystem; |
|
30
|
|
|
use Valkyrja\Http\Client\Config as Client; |
|
31
|
|
|
use Valkyrja\Http\Config as Http; |
|
32
|
|
|
use Valkyrja\Jwt\Config as Jwt; |
|
33
|
|
|
use Valkyrja\Log\Config as Log; |
|
34
|
|
|
use Valkyrja\Mail\Config as Mail; |
|
35
|
|
|
use Valkyrja\Notification\Config as Notification; |
|
36
|
|
|
use Valkyrja\Orm\Config as Orm; |
|
37
|
|
|
use Valkyrja\Session\Config as Session; |
|
38
|
|
|
use Valkyrja\Sms\Config as Sms; |
|
39
|
|
|
use Valkyrja\View\Config as View; |
|
40
|
|
|
|
|
41
|
|
|
use function unserialize; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Class Valkyrja. |
|
45
|
|
|
* |
|
46
|
|
|
* @author Melech Mizrachi |
|
47
|
|
|
* |
|
48
|
|
|
* @property Api $api |
|
49
|
|
|
* @property App $app |
|
50
|
|
|
* @property Asset $asset |
|
51
|
|
|
* @property Auth $auth |
|
52
|
|
|
* @property Broadcast $broadcast |
|
53
|
|
|
* @property Cache $cache |
|
54
|
|
|
* @property Cli $cli |
|
55
|
|
|
* @property Client $client |
|
56
|
|
|
* @property Container $container |
|
57
|
|
|
* @property Crypt $crypt |
|
58
|
|
|
* @property Event $event |
|
59
|
|
|
* @property Filesystem $filesystem |
|
60
|
|
|
* @property Http $http |
|
61
|
|
|
* @property Jwt $jwt |
|
62
|
|
|
* @property Log $log |
|
63
|
|
|
* @property Mail $mail |
|
64
|
|
|
* @property Notification $notification |
|
65
|
|
|
* @property Orm $orm |
|
66
|
|
|
* @property Session $session |
|
67
|
|
|
* @property Sms $sms |
|
68
|
|
|
* @property View $view |
|
69
|
|
|
*/ |
|
70
|
|
|
class ValkyrjaConfig |
|
71
|
|
|
{ |
|
72
|
|
|
/** |
|
73
|
|
|
* An array of config classes. |
|
74
|
|
|
* |
|
75
|
|
|
* @var array<string, Config> |
|
76
|
|
|
*/ |
|
77
|
|
|
protected array $map = []; |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param array<string, string>|null $cached The cached config |
|
81
|
|
|
* @param class-string|null $env The env class |
|
|
|
|
|
|
82
|
|
|
*/ |
|
83
|
|
|
public function __construct( |
|
84
|
|
|
protected array|null $cached = null, |
|
85
|
|
|
protected string|null $env = null |
|
86
|
|
|
) { |
|
87
|
|
|
if ($env === null && $cached === null) { |
|
88
|
|
|
throw new InvalidArgumentException('One of env or cached is required'); |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Get a config from a serialized string version of itself. |
|
94
|
|
|
* |
|
95
|
|
|
* @param non-empty-string $cached The cached config |
|
|
|
|
|
|
96
|
|
|
*/ |
|
97
|
|
|
public static function fromSerializedString(string $cached): static |
|
98
|
|
|
{ |
|
99
|
|
|
$config = unserialize($cached, ['allowed_classes' => true]); |
|
100
|
|
|
|
|
101
|
|
|
if (! $config instanceof static) { |
|
102
|
|
|
throw new RuntimeException('Invalid cached config provided'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** @psalm-suppress MixedReturnStatement It's a static object, not sure why Psalm is confused */ |
|
106
|
|
|
return $config; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Get a property. |
|
111
|
|
|
* |
|
112
|
|
|
* @param non-empty-string $name The name of the property |
|
|
|
|
|
|
113
|
|
|
*/ |
|
114
|
|
|
public function __get(string $name): Config|null |
|
115
|
|
|
{ |
|
116
|
|
|
if (! isset($this->map[$name]) && $this->cached !== null) { |
|
117
|
|
|
$cache = $this->cached[$name]; |
|
118
|
|
|
|
|
119
|
|
|
// Allow all classes, and filter for only Config classes down below since allowed_classes cannot be |
|
120
|
|
|
// a class that others extend off of, and we don't want to limit what a cached config class could be |
|
121
|
|
|
$config = unserialize($cache, ['allowed_classes' => true]); |
|
122
|
|
|
|
|
123
|
|
|
if (! $config instanceof Config) { |
|
124
|
|
|
throw new RuntimeException("Invalid cache provided for $name"); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
$this->map[$name] = $config; |
|
128
|
|
|
|
|
129
|
|
|
return $config; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
return $this->map[$name] ?? null; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* Set a property. |
|
137
|
|
|
* |
|
138
|
|
|
* @param non-empty-string $name The name of the config to add |
|
|
|
|
|
|
139
|
|
|
*/ |
|
140
|
|
|
public function __set(string $name, Config $value): void |
|
141
|
|
|
{ |
|
142
|
|
|
$this->map[$name] = $value; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* Determine if a property isset. |
|
147
|
|
|
* |
|
148
|
|
|
* @param non-empty-string $name The name of the config to check for |
|
|
|
|
|
|
149
|
|
|
*/ |
|
150
|
|
|
public function __isset(string $name): bool |
|
151
|
|
|
{ |
|
152
|
|
|
return isset($this->map[$name]); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* Cache the config. |
|
157
|
|
|
*/ |
|
158
|
|
|
public function cache(): void |
|
159
|
|
|
{ |
|
160
|
|
|
$cache = array_map('serialize', $this->map); |
|
161
|
|
|
|
|
162
|
|
|
$this->cached = $cache; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Get a cached version of this cache. |
|
167
|
|
|
*/ |
|
168
|
|
|
public function getCached(): static |
|
169
|
|
|
{ |
|
170
|
|
|
$this->cache(); |
|
171
|
|
|
|
|
172
|
|
|
$static = new static(cached: $this->cached); |
|
173
|
|
|
|
|
174
|
|
|
$this->cached = null; |
|
175
|
|
|
|
|
176
|
|
|
return $static; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Get the config as a serialized string. |
|
181
|
|
|
*/ |
|
182
|
|
|
public function asSerializedString(): string |
|
183
|
|
|
{ |
|
184
|
|
|
return serialize($this->getCached()); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
/** |
|
188
|
|
|
* Set config properties from env after setup. |
|
189
|
|
|
* |
|
190
|
|
|
* @param class-string $env The env class |
|
|
|
|
|
|
191
|
|
|
*/ |
|
192
|
|
|
public function setConfigFromEnv(string $env): void |
|
|
|
|
|
|
193
|
|
|
{ |
|
194
|
|
|
} |
|
195
|
|
|
} |
|
196
|
|
|
|