1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the daikon-cqrs/config project. |
4
|
|
|
* |
5
|
|
|
* For the full copyright and license information, please view the LICENSE |
6
|
|
|
* file that was distributed with this source code. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
declare(strict_types=1); |
10
|
|
|
|
11
|
|
|
namespace Daikon\Config; |
12
|
|
|
|
13
|
|
|
final class ConfigProviderParams implements ConfigProviderParamsInterface |
14
|
|
|
{ |
15
|
|
|
private $params; |
16
|
|
|
|
17
|
13 |
|
public function __construct(array $params) |
18
|
|
|
{ |
19
|
13 |
|
$this->params = $this->verifyParams($params); |
20
|
7 |
|
} |
21
|
|
|
|
22
|
10 |
|
public function hasScope(string $scope): bool |
23
|
|
|
{ |
24
|
10 |
|
return isset($this->params[$scope]); |
25
|
|
|
} |
26
|
|
|
|
27
|
7 |
|
public function getLoader(string $scope): ConfigLoaderInterface |
28
|
|
|
{ |
29
|
7 |
|
$this->assertScopeExists($scope); |
30
|
7 |
|
$loader = $this->params[$scope]['loader']; |
31
|
7 |
|
if (!is_object($loader)) { |
32
|
6 |
|
$this->params[$scope]['loader'] = new $loader; |
33
|
|
|
} |
34
|
7 |
|
return $this->params[$scope]['loader']; |
35
|
|
|
} |
36
|
|
|
|
37
|
7 |
|
public function getLocations(string $scope): array |
38
|
|
|
{ |
39
|
7 |
|
$this->assertScopeExists($scope); |
40
|
7 |
|
return $this->params[$scope]['locations']; |
41
|
|
|
} |
42
|
|
|
|
43
|
7 |
|
public function getSources(string $scope): array |
44
|
|
|
{ |
45
|
7 |
|
$this->assertScopeExists($scope); |
46
|
7 |
|
return $this->params[$scope]['sources']; |
47
|
|
|
} |
48
|
|
|
|
49
|
13 |
|
private function verifyParams(array $params): array |
50
|
|
|
{ |
51
|
13 |
|
if (empty($params)) { |
52
|
1 |
|
throw new \Exception('Given params may not be empty.'); |
53
|
|
|
} |
54
|
12 |
|
foreach ($params as $scope => $scopeParams) { |
55
|
12 |
|
if (isset($scopeParams['locations'])) { |
56
|
4 |
|
$this->checkLocations($scope, $scopeParams); |
57
|
|
|
} else { |
58
|
9 |
|
$params[$scope]['locations'] = []; |
59
|
|
|
} |
60
|
11 |
|
$this->checkSources($scope, $scopeParams); |
61
|
9 |
|
$this->checkLoader($scope, $scopeParams); |
62
|
|
|
} |
63
|
7 |
|
return $params; |
64
|
|
|
} |
65
|
|
|
|
66
|
4 |
|
private function checkLocations(string $scope, array $scopeParams) |
67
|
|
|
{ |
68
|
4 |
|
if (!is_array($scopeParams['locations'])) { |
69
|
1 |
|
throw new \Exception(sprintf('The "locations" param within scope: "%s" must be an array', $scope)); |
70
|
|
|
} |
71
|
3 |
|
} |
72
|
|
|
|
73
|
11 |
|
private function checkSources(string $scope, array $params) |
74
|
|
|
{ |
75
|
11 |
|
if (!isset($params['sources'])) { |
76
|
1 |
|
throw new \Exception(sprintf('Missing required key "sources" within scope: "%s"', $scope)); |
77
|
|
|
} |
78
|
10 |
|
if (!is_array($params['sources'])) { |
79
|
1 |
|
throw new \Exception(sprintf('The "sources" param within scope: "%s" must be an array', $scope)); |
80
|
|
|
} |
81
|
9 |
|
} |
82
|
|
|
|
83
|
9 |
|
private function checkLoader(string $scope, array $params) |
84
|
|
|
{ |
85
|
9 |
|
if (!isset($params['loader'])) { |
86
|
1 |
|
throw new \Exception(sprintf('Missing required key "loader" within scope: "%s"', $scope)); |
87
|
|
|
} |
88
|
8 |
|
if ($params['loader'] instanceof ConfigLoaderInterface) { |
89
|
2 |
|
return; |
90
|
|
|
} |
91
|
7 |
|
if (!is_string($params['loader'])) { |
92
|
|
|
throw new \Exception(sprintf('The "loader" param within scope: "%s" must be a string(fqcn)', $scope)); |
93
|
|
|
} |
94
|
7 |
|
if (!class_exists($params['loader'])) { |
95
|
1 |
|
throw new \Exception( |
96
|
1 |
|
sprintf('Configured loader: "%s" for scope: "%s" can not be found.', $params['loader'], $scope) |
97
|
|
|
); |
98
|
|
|
} |
99
|
6 |
|
$implementedInterfaces = class_implements($params['loader']); |
100
|
6 |
|
if (!in_array(ConfigLoaderInterface::class, $implementedInterfaces)) { |
101
|
|
|
throw new \Exception(sprintf( |
102
|
|
|
'Configured loader: "%s" for scope: "%s" does not implement required interface: %s', |
103
|
|
|
$params['loader'], |
104
|
|
|
$scope, |
105
|
|
|
ConfigLoaderInterface::class |
106
|
|
|
)); |
107
|
|
|
} |
108
|
6 |
|
} |
109
|
|
|
|
110
|
9 |
|
private function assertScopeExists(string $scope) |
111
|
|
|
{ |
112
|
9 |
|
if (!$this->hasScope($scope)) { |
113
|
|
|
throw new \Exception(sprintf('Given scope: "%s" has not been registered.', $scope)); |
114
|
|
|
} |
115
|
9 |
|
} |
116
|
|
|
} |
117
|
|
|
|