1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\Di; |
6
|
|
|
|
7
|
|
|
use Ray\Aop\MethodInterceptor; |
8
|
|
|
use Ray\Aop\MethodInvocation; |
9
|
|
|
use Ray\Di\Di\Inject; |
10
|
|
|
use Ray\Di\Di\Named; |
11
|
|
|
use ReflectionAttribute; |
12
|
|
|
use ReflectionNamedType; |
13
|
|
|
use ReflectionParameter; |
14
|
|
|
|
15
|
|
|
use function assert; |
16
|
|
|
use function call_user_func_array; |
17
|
|
|
use function is_callable; |
18
|
|
|
|
19
|
|
|
final class ParamInjectInterceptor implements MethodInterceptor |
20
|
|
|
{ |
21
|
|
|
/** @var InjectorInterface */ |
22
|
|
|
private $injector; |
23
|
|
|
|
24
|
|
|
/** @var MethodInvocationProvider */ |
25
|
|
|
private $methodInvocationProvider; |
26
|
|
|
|
27
|
|
|
public function __construct(InjectorInterface $injector, MethodInvocationProvider $methodInvocationProvider) |
28
|
|
|
{ |
29
|
|
|
$this->injector = $injector; |
30
|
|
|
$this->methodInvocationProvider = $methodInvocationProvider; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function invoke(MethodInvocation $invocation) |
37
|
|
|
{ |
38
|
|
|
$this->methodInvocationProvider->set($invocation); |
39
|
|
|
$params = $invocation->getMethod()->getParameters(); |
40
|
|
|
$namedArguments = $invocation->getNamedArguments()->getArrayCopy(); |
41
|
|
|
foreach ($params as $param) { |
42
|
|
|
/** @var list<ReflectionAttribute> $attributes */ |
|
|
|
|
43
|
|
|
$attributes = $param->getAttributes(Inject::class); |
44
|
|
|
if (isset($attributes[0])) { |
45
|
|
|
/** @psalm-suppress MixedAssignment */ |
46
|
|
|
$namedArguments[$param->getName()] = $this->getDependency($param); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$callable = [$invocation->getThis(), $invocation->getMethod()->getName()]; |
|
|
|
|
51
|
|
|
assert(is_callable($callable)); |
52
|
|
|
|
53
|
|
|
return call_user_func_array($callable, $namedArguments); // @phpstan-ignore-line PHP8 named arguments |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @return mixed |
58
|
|
|
*/ |
59
|
|
|
private function getDependency(ReflectionParameter $param) |
60
|
|
|
{ |
61
|
|
|
$named = $this->getName($param); |
62
|
|
|
$type = $param->getType(); |
63
|
|
|
assert($type instanceof ReflectionNamedType || $type === null); |
|
|
|
|
64
|
|
|
$interface = $type ? $type->getName() : ''; |
65
|
|
|
|
66
|
|
|
return $this->injector->getInstance($interface, $named); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
private function getName(ReflectionParameter $param): string |
70
|
|
|
{ |
71
|
|
|
/** @var list<ReflectionAttribute> $attributes */ |
|
|
|
|
72
|
|
|
$attributes = $param->getAttributes(Named::class); |
73
|
|
|
if (isset($attributes[0])) { |
74
|
|
|
$named = $attributes[0]->newInstance(); |
75
|
|
|
assert($named instanceof Named); |
76
|
|
|
|
77
|
|
|
return $named->value; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return ''; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.