1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace kalanis\kw_confs\Loaders; |
4
|
|
|
|
5
|
|
|
|
6
|
|
|
use kalanis\kw_confs\Interfaces\ILoader; |
7
|
|
|
use kalanis\kw_paths\Interfaces\IPaths; |
8
|
|
|
use kalanis\kw_paths\Path; |
9
|
|
|
use kalanis\kw_routed_paths\RoutedPath; |
10
|
|
|
|
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class PhpLoader |
14
|
|
|
* @package kalanis\kw_confs |
15
|
|
|
* Load config data from defined source |
16
|
|
|
* Contains personalized autoloader for configs! |
17
|
|
|
*/ |
18
|
|
|
class PhpLoader implements ILoader |
19
|
|
|
{ |
20
|
|
|
/** @var string[] */ |
21
|
|
|
protected array $pathMasks = [ |
22
|
|
|
'%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%8$s%9$s', # user dir, user module with conf name |
23
|
|
|
'%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%7$s%9$s', # user dir, user module |
24
|
|
|
'%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%1$s%7$s%9$s', # user dir, all user confs |
25
|
|
|
'%2$s%1$s%3$s%1$s%4$s%1$s%5$s%1$s%6$s%9$s', # user dir, conf named by module |
26
|
|
|
'%2$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%8$s%9$s', # all modules, main conf with name |
27
|
|
|
'%2$s%1$s%5$s%1$s%6$s%1$s%7$s%1$s%7$s%9$s', # all modules, default main conf |
28
|
|
|
'%2$s%1$s%5$s%1$s%6$s%1$s%7$s%9$s', # all modules, conf in root |
29
|
|
|
]; |
30
|
|
|
|
31
|
|
|
protected Path $pathLib; |
32
|
|
|
protected RoutedPath $routedLib; |
33
|
|
|
|
34
|
2 |
|
public function __construct(Path $pathLib, RoutedPath $routedLib) |
35
|
|
|
{ |
36
|
2 |
|
$this->pathLib = $pathLib; |
37
|
2 |
|
$this->routedLib = $routedLib; |
38
|
2 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param string $module |
42
|
|
|
* @param string $conf |
43
|
|
|
* @return array<string|int, string|int|float|bool|null> |
44
|
|
|
*/ |
45
|
2 |
|
public function load(string $module, string $conf = ''): ?array |
46
|
|
|
{ |
47
|
2 |
|
$path = $this->contentPath($module, $conf); |
48
|
2 |
|
return (!empty($path)) ? $this->includedConf($path) : null; |
49
|
|
|
} |
50
|
|
|
|
51
|
2 |
|
protected function contentPath(string $module, string $conf = ''): ?string |
52
|
|
|
{ |
53
|
2 |
|
$basicLookupDir = $this->pathLib->getDocumentRoot() . $this->pathLib->getPathToSystemRoot(); |
54
|
2 |
|
foreach ($this->pathMasks as $pathMask) { |
55
|
2 |
|
$unmasked = sprintf( $pathMask, |
56
|
2 |
|
DIRECTORY_SEPARATOR, $basicLookupDir, |
57
|
2 |
|
IPaths::DIR_USER, $this->routedLib->getUser(), |
58
|
2 |
|
IPaths::DIR_MODULE, $module, |
59
|
2 |
|
IPaths::DIR_CONF, $conf, IPaths::EXT |
60
|
|
|
); |
61
|
2 |
|
$path = realpath($unmasked); |
62
|
2 |
|
if ($path && is_file($path)) { |
63
|
1 |
|
return $path; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
} |
67
|
1 |
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $path |
72
|
|
|
* @return array<string|int, string|int|float|bool|null> |
73
|
|
|
*/ |
74
|
1 |
|
protected function includedConf(string $path): array |
75
|
|
|
{ |
76
|
1 |
|
$config = []; |
77
|
1 |
|
include_once ($path); |
78
|
1 |
|
return (array) $config; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|