1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Ray.Di package. |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
6
|
|
|
*/ |
7
|
|
|
namespace Ray\Di; |
8
|
|
|
|
9
|
|
|
use Ray\Aop\MethodInterceptor; |
10
|
|
|
use Ray\Aop\MethodInvocation; |
11
|
|
|
use Ray\Aop\ReflectionMethod; |
12
|
|
|
use Ray\Di\Di\Assisted; |
13
|
|
|
|
14
|
|
|
final class AssistedInterceptor implements MethodInterceptor |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var InjectorInterface |
18
|
|
|
*/ |
19
|
|
|
private $injector; |
20
|
|
|
|
21
|
2 |
|
public function __construct(InjectorInterface $injector) |
22
|
|
|
{ |
23
|
2 |
|
$this->injector = $injector; |
24
|
2 |
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Intercepts any method and injects instances of the missing arguments |
28
|
|
|
* when they are type hinted |
29
|
|
|
*/ |
30
|
2 |
|
public function invoke(MethodInvocation $invocation) |
31
|
|
|
{ |
32
|
2 |
|
$method = $invocation->getMethod(); |
33
|
2 |
|
$assisted = $method->getAnnotation('Ray\Di\Di\Assisted'); |
34
|
|
|
/* @var \Ray\Di\Di\Assisted $assisted */ |
35
|
2 |
|
$parameters = $method->getParameters(); |
36
|
2 |
|
$arguments = $invocation->getArguments()->getArrayCopy(); |
37
|
2 |
|
$cntArgs = count($arguments); |
38
|
2 |
|
if ($assisted instanceof Assisted) { |
39
|
2 |
|
$arguments = $this->injectAssistedParameters($method, $assisted, $parameters, $arguments, $cntArgs); |
40
|
2 |
|
} |
41
|
2 |
|
$invocation->getArguments()->exchangeArray($arguments); |
42
|
|
|
|
43
|
2 |
|
return $invocation->proceed(); |
44
|
|
|
} |
45
|
|
|
|
46
|
2 |
|
private function getName(ReflectionMethod $method, \ReflectionParameter $parameter) |
47
|
|
|
{ |
48
|
2 |
|
$named = $method->getAnnotation('Ray\Di\Di\Named'); |
49
|
2 |
|
if (! $named) { |
50
|
1 |
|
return Name::ANY; |
51
|
|
|
} |
52
|
1 |
|
parse_str($named->value, $names); |
53
|
1 |
|
$paramName = $parameter->getName(); |
54
|
1 |
|
if (isset($names[$paramName])) { |
55
|
1 |
|
return $names[$paramName]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return Name::ANY; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param \ReflectionMethod $method |
63
|
|
|
* @param Assisted $assisted |
64
|
|
|
* @param \ReflectionParameter[] $parameters |
65
|
|
|
* @param array $arguments |
66
|
|
|
* @param int $cntArgs |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
2 |
|
public function injectAssistedParameters(\ReflectionMethod $method, Assisted $assisted, array $parameters, array $arguments, $cntArgs) |
|
|
|
|
71
|
|
|
{ |
72
|
2 |
|
foreach ($parameters as $pos => $parameter) { |
73
|
2 |
|
if (! in_array($parameter->getName(), $assisted->values)) { |
74
|
2 |
|
continue; |
75
|
|
|
} |
76
|
2 |
|
$hint = $parameter->getClass(); |
77
|
2 |
|
$interface = $hint ? $hint->getName() : ''; |
78
|
2 |
|
$name = $this->getName($method, $parameter); |
|
|
|
|
79
|
2 |
|
$arguments[] = $this->injector->getInstance($interface, $name); |
80
|
2 |
|
} |
81
|
|
|
|
82
|
2 |
|
return $arguments; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.