1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace leinonen\Yii2Monolog\CreationStrategies; |
6
|
|
|
|
7
|
|
|
use Monolog\Formatter\FormatterInterface; |
8
|
|
|
use Monolog\Handler\HandlerInterface; |
9
|
|
|
use ReflectionParameter; |
10
|
|
|
use InvalidArgumentException; |
11
|
|
|
|
12
|
|
|
class ReflectionStrategy implements CreationStrategyInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var \ReflectionClass |
16
|
|
|
*/ |
17
|
|
|
private $handlerReflectionClass; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initializes a new ReflectionStrategy. |
21
|
|
|
* |
22
|
|
|
* @param string $class |
23
|
|
|
*/ |
24
|
12 |
|
public function __construct(string $class) |
25
|
|
|
{ |
26
|
12 |
|
$this->handlerReflectionClass = new \ReflectionClass($class); |
27
|
12 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
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->name; |
50
|
1 |
|
}, |
51
|
|
|
$requiredParameters |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
|
|
*/ |
58
|
8 |
|
public function getConstructorParameters(array $config): array |
59
|
|
|
{ |
60
|
8 |
|
if ($this->handlerReflectionClass->getConstructor() === null) { |
61
|
1 |
|
return []; |
62
|
|
|
} |
63
|
|
|
|
64
|
7 |
|
return array_map( |
65
|
|
|
function (ReflectionParameter $constructorParameter) use ($config) { |
66
|
7 |
|
return $this->resolveConstructorParameterValue($constructorParameter, $config); |
67
|
7 |
|
}, |
68
|
7 |
|
$this->handlerReflectionClass->getConstructor()->getParameters() |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* {@inheritdoc} |
74
|
|
|
*/ |
75
|
|
|
public function getConfigurationCallable(array $config): callable |
76
|
|
|
{ |
77
|
|
|
return $config['configure'] ?? function ($instance) { return $instance; }; |
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
|
|
|
} |
114
|
|
|
|