|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of the Stack package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Andrzej Kostrzewa <[email protected]> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Stack\DI\Resolver; |
|
12
|
|
|
|
|
13
|
|
|
use Stack\DI\Exception; |
|
14
|
|
|
use Stack\DI\Injection\LazyInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Class ParameterResolver |
|
18
|
|
|
* |
|
19
|
|
|
* @author Andrzej Kostrzewa <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ParameterResolver |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* Resolve class constructor params |
|
25
|
|
|
* |
|
26
|
|
|
* @param $class |
|
27
|
|
|
* @param $params |
|
28
|
|
|
* @param array $mergeParams |
|
29
|
|
|
* |
|
30
|
|
|
* @throws Exception\MissingParam |
|
31
|
|
|
* |
|
32
|
|
|
* @return mixed |
|
33
|
|
|
*/ |
|
34
|
|
|
public function resolve($class, $params, $mergeParams = []) |
|
35
|
|
|
{ |
|
36
|
|
|
if (empty($mergeParams)) { |
|
37
|
|
|
$this->mergeParamsEmpty($class, $params); |
|
38
|
|
|
|
|
39
|
|
|
return $params; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
$this->mergeParams($class, $params, $mergeParams); |
|
43
|
|
|
|
|
44
|
|
|
return $params; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* Merges the params with overrides; also invokes Lazy values. |
|
49
|
|
|
* |
|
50
|
|
|
* @param string $class The params are on this class. |
|
51
|
|
|
* @param array $params The constructor parameters. |
|
52
|
|
|
* @param array $mergeParams An array of override parameters. |
|
53
|
|
|
* |
|
54
|
|
|
* @throws Exception\MissingParam |
|
55
|
|
|
* |
|
56
|
|
|
* @return string[]|null |
|
57
|
|
|
*/ |
|
58
|
|
|
protected function mergeParams($class, &$params, array $mergeParams = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$positionOfParam = 0; |
|
61
|
|
|
foreach ($params as $key => $value) { |
|
62
|
|
|
if (array_key_exists($positionOfParam, $mergeParams)) { |
|
63
|
|
|
$value = $mergeParams[$positionOfParam]; |
|
64
|
|
|
} elseif (array_key_exists($key, $mergeParams)) { |
|
65
|
|
|
$value = $mergeParams[$key]; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
if ($value instanceof UnresolvedParam) { |
|
69
|
|
|
throw Exception::missingParam($class, $value->getName()); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($value instanceof LazyInterface) { |
|
73
|
|
|
$value = $value(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$params[$key] = $value; |
|
77
|
|
|
|
|
78
|
|
|
$positionOfParam++; |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Load the Lazy values in params when the mergeParams are empty. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $class The params are on this class. |
|
86
|
|
|
* @param array $params The constructor parameters. |
|
87
|
|
|
* |
|
88
|
|
|
* @throws Exception\MissingParam |
|
89
|
|
|
* |
|
90
|
|
|
* @return null |
|
91
|
|
|
*/ |
|
92
|
|
|
protected function mergeParamsEmpty($class, &$params) |
|
93
|
|
|
{ |
|
94
|
|
|
foreach ($params as $key => $value) { |
|
95
|
|
|
if ($value instanceof UnresolvedParam) { |
|
96
|
|
|
throw Exception::missingParam($class, $value->getName()); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
if ($value instanceof LazyInterface) { |
|
100
|
|
|
$params[$key] = $value(); |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|