1
|
|
|
<?php |
2
|
|
|
namespace html_go\model; |
3
|
|
|
|
4
|
|
|
use InvalidArgumentException; |
5
|
|
|
use html_go\exceptions\InternalException; |
6
|
|
|
|
7
|
|
|
abstract class AdminConfig |
8
|
|
|
{ |
9
|
|
|
public const KEY_ADMIN_CONTEXT = 'admin.context'; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* @var array<string, string> |
13
|
|
|
*/ |
14
|
|
|
protected array $config; |
15
|
|
|
|
16
|
|
|
public function __construct(string $configRoot) { |
17
|
|
|
$configFile = $configRoot.DS.'config.ini'; |
18
|
|
|
if (!\is_file($configFile)) { |
19
|
|
|
throw new InvalidArgumentException("Configuration INI file not found [$configFile]"); |
20
|
|
|
} |
21
|
|
|
if (($config = \parse_ini_file($configFile, false, INI_SCANNER_TYPED)) === false) { |
22
|
|
|
throw new InternalException("parse_ini_file() failed [$configFile]"); // @codeCoverageIgnore |
23
|
|
|
} |
24
|
|
|
$this->config = $this->validateAdminConfig($config); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function checkAndGet(string $key): mixed { |
28
|
|
|
if (isset($this->config[$key]) === false) { |
29
|
|
|
return null; |
30
|
|
|
} |
31
|
|
|
return $this->config[$key]; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param array<mixed> $config |
36
|
|
|
* @param string $key |
37
|
|
|
* @param mixed $default |
38
|
|
|
* @return array<mixed> |
39
|
|
|
*/ |
40
|
|
|
protected function checkSetOrDefault(array $config, string $key, mixed $default): array { |
41
|
|
|
if (isset($config[$key])) { |
42
|
|
|
return $config; |
43
|
|
|
} |
44
|
|
|
$config[$key] = $default; |
45
|
|
|
return $config; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Check required options are set and set defaults. |
50
|
|
|
* @param array<string, mixed> $config |
51
|
|
|
* @return array<string, mixed> |
52
|
|
|
* @throws InvalidArgumentException |
53
|
|
|
*/ |
54
|
|
|
protected function validateAdminConfig(array $config): array { |
55
|
|
|
$config = $this->checkSetOrDefault($config, self::KEY_ADMIN_CONTEXT, 'admin'); |
56
|
|
|
return $config; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|