1 | <?php |
||
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) |
|
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) |
|
112 | } |
||
113 |