|
1
|
|
|
<?php declare(strict_types=1); |
|
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
|
|
|
namespace Daikon\Config; |
|
10
|
|
|
|
|
11
|
|
|
use Daikon\Interop\Assert; |
|
12
|
|
|
use Daikon\Interop\Assertion; |
|
13
|
|
|
|
|
14
|
|
|
final class ConfigProviderParams implements ConfigProviderParamsInterface |
|
15
|
|
|
{ |
|
16
|
|
|
private array $params; |
|
17
|
|
|
|
|
18
|
13 |
|
public function __construct(array $params) |
|
19
|
|
|
{ |
|
20
|
13 |
|
$this->params = $this->verifyParams($params); |
|
21
|
7 |
|
} |
|
22
|
|
|
|
|
23
|
11 |
|
public function hasScope(string $scope): bool |
|
24
|
|
|
{ |
|
25
|
11 |
|
return isset($this->params[$scope]); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
8 |
|
public function getLoader(string $scope): ConfigLoaderInterface |
|
29
|
|
|
{ |
|
30
|
8 |
|
$this->assertScopeExists($scope); |
|
31
|
8 |
|
$loader = $this->params[$scope]['loader']; |
|
32
|
8 |
|
if (!is_object($loader)) { |
|
33
|
7 |
|
$this->params[$scope]['loader'] = new $loader; |
|
34
|
|
|
} |
|
35
|
8 |
|
return $this->params[$scope]['loader']; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
8 |
|
public function getLocations(string $scope): array |
|
39
|
|
|
{ |
|
40
|
8 |
|
$this->assertScopeExists($scope); |
|
41
|
8 |
|
return $this->params[$scope]['locations']; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
8 |
|
public function getSources(string $scope): array |
|
45
|
|
|
{ |
|
46
|
8 |
|
$this->assertScopeExists($scope); |
|
47
|
8 |
|
return $this->params[$scope]['sources']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
13 |
|
private function verifyParams(array $params): array |
|
51
|
|
|
{ |
|
52
|
13 |
|
Assertion::notEmpty($params, 'Given params may not be empty.'); |
|
53
|
12 |
|
foreach ($params as $scope => $scopeParams) { |
|
54
|
12 |
|
if (isset($scopeParams['locations'])) { |
|
55
|
4 |
|
$this->checkLocations($scope, $scopeParams); |
|
56
|
|
|
} else { |
|
57
|
9 |
|
$params[$scope]['locations'] = []; |
|
58
|
|
|
} |
|
59
|
11 |
|
$this->checkSources($scope, $scopeParams); |
|
60
|
9 |
|
$this->checkLoader($scope, $scopeParams); |
|
61
|
|
|
} |
|
62
|
7 |
|
return $params; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
private function checkLocations(string $scope, array $params): void |
|
66
|
|
|
{ |
|
67
|
4 |
|
Assertion::isArray($params['locations'], "The 'locations' param within scope '$scope' must be an array."); |
|
68
|
3 |
|
} |
|
69
|
|
|
|
|
70
|
11 |
|
private function checkSources(string $scope, array $params): void |
|
71
|
|
|
{ |
|
72
|
11 |
|
Assertion::keyIsset($params, 'sources', "Missing required key 'sources' within scope '$scope'."); |
|
73
|
10 |
|
Assertion::isArray($params['sources'], "The 'sources' param within scope '$scope' must be an array."); |
|
74
|
9 |
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
private function checkLoader(string $scope, array $params): void |
|
77
|
|
|
{ |
|
78
|
9 |
|
Assertion::keyIsset($params, 'loader', "Missing required key 'loader' within scope '$scope'."); |
|
79
|
8 |
|
if ($params['loader'] instanceof ConfigLoaderInterface) { |
|
80
|
2 |
|
return; |
|
81
|
|
|
} |
|
82
|
7 |
|
Assert::that($params['loader']) |
|
83
|
7 |
|
->string("The 'loader' param within scope '$scope' must be a fqcn string.") |
|
84
|
7 |
|
->classExists("Configured loader '{$params['loader']}' for scope '$scope' cannot be found.") |
|
85
|
6 |
|
->implementsInterface( |
|
86
|
6 |
|
ConfigLoaderInterface::class, |
|
87
|
|
|
sprintf( |
|
88
|
6 |
|
"Configured loader '%s' for scope '%s' does not implement required interface '%s'.", |
|
89
|
6 |
|
$params['loader'], |
|
90
|
|
|
$scope, |
|
91
|
6 |
|
ConfigLoaderInterface::class |
|
92
|
|
|
) |
|
93
|
|
|
); |
|
94
|
6 |
|
} |
|
95
|
|
|
|
|
96
|
10 |
|
private function assertScopeExists(string $scope): void |
|
97
|
|
|
{ |
|
98
|
10 |
|
Assertion::true($this->hasScope($scope), "Given scope '$scope' has not been registered."); |
|
99
|
10 |
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|