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
|
|
|
public const KEY_ADMIN_THEME_NAME = 'admin.theme.name'; |
11
|
|
|
public const KEY_ADMIN_ITEMS_PER_PAGE = 'admin.items.per_page'; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @var array<string, string> |
15
|
|
|
*/ |
16
|
|
|
protected array $config; |
17
|
|
|
|
18
|
|
|
public function __construct(string $configRoot) { |
19
|
|
|
$configFile = $configRoot.DS.'config.ini'; |
20
|
|
|
if (!\is_file($configFile)) { |
21
|
|
|
throw new InvalidArgumentException("Configuration INI file not found [$configFile]"); |
22
|
|
|
} |
23
|
|
|
if (($config = \parse_ini_file($configFile, false, INI_SCANNER_TYPED)) === false) { |
24
|
|
|
throw new InternalException("parse_ini_file() failed [$configFile]"); // @codeCoverageIgnore |
25
|
|
|
} |
26
|
|
|
$this->config = $this->validateAdminConfig($config); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
protected function checkAndGet(string $key): mixed { |
30
|
|
|
if (empty($this->config[$key])) { |
31
|
|
|
return null; |
32
|
|
|
} |
33
|
|
|
return $this->config[$key]; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param array<mixed> $config |
38
|
|
|
* @param string $key |
39
|
|
|
* @param mixed $default |
40
|
|
|
* @return array<mixed> |
41
|
|
|
*/ |
42
|
|
|
protected function checkSetOrDefault(array $config, string $key, mixed $default): array { |
43
|
|
|
if (empty($config[$key]) === false) { |
44
|
|
|
return $config; |
45
|
|
|
} |
46
|
|
|
$config[$key] = $default; |
47
|
|
|
return $config; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Check required options are set and set defaults. |
52
|
|
|
* @param array<string, mixed> $config |
53
|
|
|
* @return array<string, mixed> |
54
|
|
|
* @throws InvalidArgumentException |
55
|
|
|
*/ |
56
|
|
|
protected function validateAdminConfig(array $config): array { |
57
|
|
|
$config = $this->checkSetOrDefault($config, self::KEY_ADMIN_CONTEXT, 'admin'); |
58
|
|
|
$config = $this->checkSetOrDefault($config, self::KEY_ADMIN_THEME_NAME, 'default'); |
59
|
|
|
$config = $this->checkSetOrDefault($config, self::KEY_ADMIN_ITEMS_PER_PAGE, 5); |
60
|
|
|
return $config; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|