1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Ray\Di; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Annotations\Reader; |
8
|
|
|
use Ray\Di\Di\Qualifier; |
9
|
|
|
|
10
|
|
|
final class InjectionPoint implements InjectionPointInterface, \Serializable |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var \ReflectionParameter |
14
|
|
|
*/ |
15
|
|
|
private $parameter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var Reader |
19
|
|
|
*/ |
20
|
|
|
private $reader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
private $pClass; |
26
|
42 |
|
|
27
|
|
|
/** |
28
|
42 |
|
* @var string |
29
|
42 |
|
*/ |
30
|
42 |
|
private $pFunction; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
2 |
|
private $pName; |
36
|
|
|
|
37
|
2 |
|
public function __construct(\ReflectionParameter $parameter, Reader $reader) |
38
|
|
|
{ |
39
|
|
|
$this->parameter = $parameter; |
40
|
|
|
$this->pFunction = $parameter->getDeclaringFunction()->name; |
41
|
|
|
$class = $parameter->getDeclaringClass(); |
42
|
|
|
$this->pClass = $class instanceof \ReflectionClass ? $class->name : ''; |
43
|
3 |
|
$this->pName = $parameter->name; |
44
|
|
|
$this->reader = $reader; |
45
|
3 |
|
} |
46
|
3 |
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritdoc} |
49
|
3 |
|
*/ |
50
|
|
|
public function getParameter() : \ReflectionParameter |
51
|
3 |
|
{ |
52
|
|
|
return $this->parameter ?? new \ReflectionParameter([$this->pClass, $this->pFunction], $this->pName); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* {@inheritdoc} |
57
|
1 |
|
*/ |
58
|
|
|
public function getMethod() : \ReflectionMethod |
59
|
1 |
|
{ |
60
|
1 |
|
$class = $this->parameter->getDeclaringClass(); |
61
|
1 |
|
if (! $class instanceof \ReflectionClass) { |
62
|
|
|
throw new \LogicException($this->parameter->getName()); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
$method = $this->parameter->getDeclaringFunction()->getShortName(); |
65
|
|
|
|
66
|
|
|
return new \ReflectionMethod($class->name, $method); |
67
|
|
|
} |
68
|
|
|
|
69
|
2 |
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
2 |
|
*/ |
72
|
2 |
|
public function getClass() : \ReflectionClass |
73
|
2 |
|
{ |
74
|
2 |
|
$class = $this->parameter->getDeclaringClass(); |
75
|
2 |
|
if ($class instanceof \ReflectionClass) { |
76
|
2 |
|
return $class; |
77
|
|
|
} |
78
|
2 |
|
|
79
|
2 |
|
throw new \LogicException($this->parameter->getName()); |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
2 |
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getQualifiers() : array |
86
|
|
|
{ |
87
|
|
|
$qualifiers = []; |
88
|
|
|
$annotations = $this->reader->getMethodAnnotations($this->getMethod()); |
89
|
|
|
foreach ($annotations as $annotation) { |
90
|
|
|
$qualifier = $this->reader->getClassAnnotation( |
91
|
|
|
new \ReflectionClass($annotation), |
92
|
|
|
Qualifier::class |
93
|
|
|
); |
94
|
|
|
if ($qualifier instanceof Qualifier) { |
95
|
|
|
$qualifiers[] = $annotation; |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
return $qualifiers; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function serialize() : string |
103
|
|
|
{ |
104
|
|
|
return serialize([$this->reader, $this->pClass, $this->pFunction, $this->pName]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function unserialize($serialized) |
108
|
|
|
{ |
109
|
|
|
[$this->reader, $this->pClass, $this->pFunction, $this->pName] = unserialize($serialized); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|