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