|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Uxmp\Core\Component\Config; |
|
6
|
|
|
|
|
7
|
|
|
use Configula\ConfigValues; |
|
8
|
|
|
use Psr\Log\LogLevel; |
|
9
|
|
|
use Uxmp\Core\Component\Cache\CacheDescriptorEnum; |
|
10
|
|
|
use Uxmp\Core\Component\Cache\CacheTypeEnum; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Provides typed access to config values |
|
14
|
|
|
*/ |
|
15
|
|
|
final class ConfigProvider implements ConfigProviderInterface |
|
16
|
|
|
{ |
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private const PROTO_HTTP = 'http'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
private const PROTO_HTTPS = 'https'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
private const HTTP_PORT = 80; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private const HTTPS_PORT = 443; |
|
36
|
|
|
|
|
37
|
|
|
// Default values (ten days) |
|
38
|
|
|
/** |
|
39
|
|
|
* @var int |
|
40
|
|
|
*/ |
|
41
|
|
|
private const DEFAULT_TOKEN_LIFETIME = 1_086_400; |
|
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param ConfigValues<string, mixed> $configValues |
|
45
|
|
|
*/ |
|
46
|
20 |
|
public function __construct( |
|
47
|
|
|
private readonly ConfigValues $configValues, |
|
48
|
|
|
) { |
|
49
|
20 |
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* Path to the log file storage folder |
|
53
|
|
|
*/ |
|
54
|
1 |
|
public function getLogFilePath(): string |
|
55
|
|
|
{ |
|
56
|
1 |
|
return $this->configValues->get('logging.path', ''); |
|
|
|
|
|
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* JWT secret which must be explicitly set by the user in the config |
|
61
|
|
|
*/ |
|
62
|
1 |
|
public function getJwtSecret(): string |
|
63
|
|
|
{ |
|
64
|
1 |
|
return $this->configValues->get('security.jwt_secret', ''); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Name of the http cookie needed by the streaming routes |
|
69
|
|
|
*/ |
|
70
|
1 |
|
public function getCookieName(): string |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->configValues->get('security.token_name', 'nekot'); |
|
|
|
|
|
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* Max age of the jwt token |
|
77
|
|
|
*/ |
|
78
|
1 |
|
public function getTokenLifetime(): int |
|
79
|
|
|
{ |
|
80
|
1 |
|
return (int) $this->configValues->get( |
|
81
|
1 |
|
'security.token_lifetime', |
|
82
|
1 |
|
self::DEFAULT_TOKEN_LIFETIME |
|
83
|
1 |
|
); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Log level as defined by PSR |
|
88
|
|
|
* |
|
89
|
|
|
* @phpstan-return LogLevel::* |
|
90
|
|
|
*/ |
|
91
|
1 |
|
public function getLogLevel(): string |
|
92
|
|
|
{ |
|
93
|
1 |
|
return $this->configValues->get('logging.level', LogLevel::ERROR); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Name of the CORS origin (e.g. if backend/frontend hostnames differ) |
|
98
|
|
|
*/ |
|
99
|
1 |
|
public function getCorsOrigin(): string |
|
100
|
|
|
{ |
|
101
|
1 |
|
return $this->configValues->get('http.cors_origin', ''); |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Base path to the public folder |
|
106
|
|
|
*/ |
|
107
|
4 |
|
public function getApiBasePath(): string |
|
108
|
|
|
{ |
|
109
|
4 |
|
return $this->configValues->get('http.api_base_path', ''); |
|
|
|
|
|
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Path to the asset storage folder |
|
114
|
|
|
*/ |
|
115
|
1 |
|
public function getAssetPath(): string |
|
116
|
|
|
{ |
|
117
|
1 |
|
return $this->configValues->get('assets.path', ''); |
|
|
|
|
|
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Debug mode setting |
|
122
|
|
|
*/ |
|
123
|
1 |
|
public function getDebugMode(): bool |
|
124
|
|
|
{ |
|
125
|
1 |
|
return (bool) $this->configValues->get('debug.enabled', false); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* DSN for the database connection |
|
130
|
|
|
*/ |
|
131
|
1 |
|
public function getDatabaseDsn(): string |
|
132
|
|
|
{ |
|
133
|
1 |
|
return $this->configValues->get('database.dsn', ''); |
|
|
|
|
|
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Database password |
|
138
|
|
|
*/ |
|
139
|
1 |
|
public function getDatabasePassword(): string |
|
140
|
|
|
{ |
|
141
|
1 |
|
return $this->configValues->get('database.password', ''); |
|
|
|
|
|
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
/** |
|
145
|
|
|
* @return array{ |
|
|
|
|
|
|
146
|
|
|
* orm_query: CacheTypeEnum, |
|
147
|
|
|
* orm_result: CacheTypeEnum, |
|
148
|
|
|
* orm_metadata: CacheTypeEnum |
|
149
|
|
|
* } |
|
150
|
|
|
*/ |
|
151
|
2 |
|
public function getCacheConfig(): array |
|
152
|
|
|
{ |
|
153
|
|
|
/** |
|
154
|
|
|
* @var array{ |
|
155
|
|
|
* orm_query: string, |
|
156
|
|
|
* orm_result: string, |
|
157
|
|
|
* orm_metadata: string |
|
158
|
|
|
* } $cacheConfig |
|
159
|
|
|
*/ |
|
160
|
2 |
|
$cacheConfig = (array) $this->configValues->get('cache') + [ |
|
161
|
2 |
|
CacheDescriptorEnum::ORM_QUERY->value => '', |
|
162
|
2 |
|
CacheDescriptorEnum::ORM_RESULT->value => '', |
|
163
|
2 |
|
CacheDescriptorEnum::ORM_METADATA->value => '', |
|
164
|
2 |
|
]; |
|
165
|
|
|
|
|
166
|
2 |
|
return array_map( |
|
167
|
2 |
|
static fn (string $cacheAdapter): CacheTypeEnum => CacheTypeEnum::tryFrom($cacheAdapter) ?? CacheTypeEnum::ARRAY, |
|
168
|
2 |
|
$cacheConfig |
|
169
|
2 |
|
); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Returns the redis connection uri |
|
174
|
|
|
*/ |
|
175
|
1 |
|
public function getRedisUri(): string |
|
176
|
|
|
{ |
|
177
|
1 |
|
return (string) $this->configValues->get('services.redis_uri', ''); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* The base url for the usage in absolute urls |
|
182
|
|
|
*/ |
|
183
|
3 |
|
public function getBaseUrl(): string |
|
184
|
|
|
{ |
|
185
|
3 |
|
$hostname = $this->configValues->get('http.hostname', ''); |
|
186
|
3 |
|
$port = (int) $this->configValues->get('http.port', 0); |
|
187
|
3 |
|
$ssl = (bool) $this->configValues->get('http.ssl', true); |
|
188
|
|
|
|
|
189
|
3 |
|
$protocol = ($ssl) |
|
190
|
2 |
|
? self::PROTO_HTTPS |
|
191
|
1 |
|
: self::PROTO_HTTP; |
|
192
|
|
|
|
|
193
|
3 |
|
$port_string = ''; |
|
194
|
|
|
if ( |
|
195
|
3 |
|
$port !== 0 && |
|
196
|
3 |
|
!in_array($port, [self::HTTP_PORT, self::HTTPS_PORT], true) |
|
197
|
|
|
) { |
|
198
|
1 |
|
$port_string = sprintf(':%d', $port); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
3 |
|
return sprintf( |
|
202
|
3 |
|
'%s://%s%s%s', |
|
203
|
3 |
|
$protocol, |
|
204
|
3 |
|
$hostname, |
|
|
|
|
|
|
205
|
3 |
|
$port_string, |
|
206
|
3 |
|
$this->getApiBasePath() |
|
207
|
3 |
|
); |
|
208
|
|
|
} |
|
209
|
|
|
|
|
210
|
|
|
/** |
|
211
|
|
|
* Max age of files in the clients' asset cache |
|
212
|
|
|
*/ |
|
213
|
1 |
|
public function getClientCacheMaxAge(): int |
|
214
|
|
|
{ |
|
215
|
1 |
|
return 86_400 * 100; |
|
|
|
|
|
|
216
|
|
|
} |
|
217
|
|
|
|
|
218
|
|
|
/** |
|
219
|
|
|
* Destination directory for the orm proxy generation |
|
220
|
|
|
*/ |
|
221
|
1 |
|
public function getOrmProxyPath(): string |
|
222
|
|
|
{ |
|
223
|
1 |
|
return $this->configValues->get('database.proxy_path', ''); |
|
|
|
|
|
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @return array{ |
|
|
|
|
|
|
228
|
|
|
* transcoder: string, |
|
229
|
|
|
* codec: string, |
|
230
|
|
|
* bitrate: int, |
|
231
|
|
|
* allowed_bitrates: array<int>, |
|
232
|
|
|
* allowed_codecs: array<string>, |
|
233
|
|
|
* } |
|
234
|
|
|
*/ |
|
235
|
1 |
|
public function getTranscodingConfig(): array |
|
236
|
|
|
{ |
|
237
|
1 |
|
$config = $this->configValues->get('transcoding'); |
|
238
|
|
|
|
|
239
|
1 |
|
return [ |
|
240
|
1 |
|
'transcoder' => (string) $config['transcoder'], |
|
241
|
1 |
|
'codec' => (string) $config['codec'], |
|
242
|
1 |
|
'bitrate' => (int) $config['bitrate'], |
|
243
|
1 |
|
'allowed_bitrates' => array_map('intval', explode(',', (string) $config['allowed_bitrates'])), |
|
244
|
1 |
|
'allowed_codecs' => explode(',', (string) $config['allowed_codecs']), |
|
245
|
1 |
|
]; |
|
246
|
|
|
} |
|
247
|
|
|
} |
|
248
|
|
|
|