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