1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace leinonen\Yii2Monolog\CreationStrategies; |
6
|
|
|
|
7
|
|
|
use ReflectionParameter; |
8
|
|
|
use InvalidArgumentException; |
9
|
|
|
|
10
|
|
|
class ReflectionStrategy implements CreationStrategyInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \ReflectionClass |
14
|
|
|
*/ |
15
|
|
|
private $handlerReflectionClass; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Initializes a new ReflectionStrategy. |
19
|
|
|
* |
20
|
|
|
* @param string $class |
21
|
|
|
*/ |
22
|
10 |
|
public function __construct(string $class) |
23
|
|
|
{ |
24
|
10 |
|
$this->handlerReflectionClass = new \ReflectionClass($class); |
25
|
10 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Returns required parameter names as array. |
29
|
|
|
* |
30
|
|
|
* @return string[] |
31
|
|
|
*/ |
32
|
2 |
|
public function getRequiredParameters(): array |
33
|
|
|
{ |
34
|
2 |
|
if ($this->handlerReflectionClass->getConstructor() === null) { |
35
|
1 |
|
return []; |
36
|
|
|
} |
37
|
|
|
|
38
|
1 |
|
$requiredParameters = \array_values( |
39
|
|
|
\array_filter( |
40
|
1 |
|
$this->handlerReflectionClass->getConstructor()->getParameters(), |
41
|
|
|
function (ReflectionParameter $constructorParameter) { |
42
|
1 |
|
return ! $constructorParameter->isOptional(); |
43
|
1 |
|
} |
44
|
|
|
) |
45
|
|
|
); |
46
|
|
|
|
47
|
1 |
|
return \array_map( |
48
|
|
|
function (ReflectionParameter $parameter) { |
49
|
1 |
|
return $parameter->getName(); |
|
|
|
|
50
|
1 |
|
}, |
51
|
|
|
$requiredParameters |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $config |
57
|
|
|
* |
58
|
|
|
* @return array |
59
|
|
|
* |
60
|
|
|
* @throws \yii\di\NotInstantiableException |
61
|
|
|
* @throws \yii\base\InvalidConfigException |
62
|
|
|
* @throws \InvalidArgumentException |
63
|
|
|
*/ |
64
|
8 |
|
public function getConstructorParameters(array $config): array |
65
|
|
|
{ |
66
|
8 |
|
if ($this->handlerReflectionClass->getConstructor() === null) { |
67
|
1 |
|
return []; |
68
|
|
|
} |
69
|
|
|
|
70
|
7 |
|
return array_map( |
71
|
7 |
|
function (ReflectionParameter $constructorParameter) use ($config) { |
72
|
7 |
|
return $this->resolveConstructorParameterValue($constructorParameter, $config); |
73
|
7 |
|
}, |
74
|
7 |
|
$this->handlerReflectionClass->getConstructor()->getParameters() |
75
|
|
|
); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Returns the value for the constructorParameter from given configuration array. |
80
|
|
|
* |
81
|
|
|
* @param ReflectionParameter $constructorParameter |
82
|
|
|
* @param array $config |
83
|
|
|
* |
84
|
|
|
* @return mixed |
85
|
|
|
* |
86
|
|
|
* @throws \yii\di\NotInstantiableException |
87
|
|
|
* @throws \yii\base\InvalidConfigException |
88
|
|
|
* @throws \InvalidArgumentException |
89
|
|
|
*/ |
90
|
7 |
|
private function resolveConstructorParameterValue(ReflectionParameter $constructorParameter, array $config) |
91
|
|
|
{ |
92
|
7 |
|
foreach ($config as $parameterName => $configuredParameterValue) { |
93
|
5 |
|
if ($constructorParameter->getName() === $parameterName) { |
|
|
|
|
94
|
5 |
|
return $configuredParameterValue; |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
if ($constructorParameter->isDefaultValueAvailable()) { |
99
|
4 |
|
return $constructorParameter->getDefaultValue(); |
100
|
|
|
} |
101
|
|
|
|
102
|
3 |
|
if ($constructorParameter->hasType() && ! $constructorParameter->getType()->isBuiltin()) { |
103
|
2 |
|
return \Yii::$container->get($constructorParameter->getClass()->getName()); |
|
|
|
|
104
|
|
|
} |
105
|
|
|
|
106
|
1 |
|
throw new InvalidArgumentException( |
107
|
1 |
|
"Expected to find key: '{$constructorParameter->getName()}' in the given config array but none found." |
|
|
|
|
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|