|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Compiler; |
|
6
|
|
|
|
|
7
|
|
|
use LogicException; |
|
8
|
|
|
use Ray\Aop\ReflectionMethod; |
|
9
|
|
|
use Ray\Di\InjectionPointInterface; |
|
10
|
|
|
use ReflectionClass; |
|
11
|
|
|
use ReflectionParameter; |
|
12
|
|
|
use RuntimeException; |
|
13
|
|
|
|
|
14
|
|
|
use function assert; |
|
15
|
|
|
use function file_exists; |
|
16
|
|
|
use function file_get_contents; |
|
17
|
|
|
use function is_bool; |
|
18
|
|
|
use function sprintf; |
|
19
|
|
|
use function str_replace; |
|
20
|
|
|
use function unserialize; |
|
21
|
|
|
|
|
22
|
|
|
final class InjectionPoint implements InjectionPointInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** @var ReflectionParameter */ |
|
25
|
|
|
private $parameter; |
|
26
|
|
|
|
|
27
|
|
|
/** @var string */ |
|
28
|
|
|
private $scriptDir; |
|
29
|
|
|
|
|
30
|
|
|
public function __construct(ReflectionParameter $parameter, string $scriptDir) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->parameter = $parameter; |
|
33
|
|
|
$this->scriptDir = $scriptDir; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* {@inheritdoc} |
|
38
|
|
|
*/ |
|
39
|
|
|
public function getParameter(): ReflectionParameter |
|
40
|
|
|
{ |
|
41
|
|
|
return $this->parameter; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* {@inheritdoc} |
|
46
|
|
|
*/ |
|
47
|
|
|
public function getMethod(): \ReflectionMethod |
|
48
|
|
|
{ |
|
49
|
|
|
$reflectionMethod = $this->parameter->getDeclaringFunction(); |
|
50
|
|
|
assert($reflectionMethod instanceof ReflectionMethod); |
|
51
|
|
|
|
|
52
|
|
|
return $reflectionMethod; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* {@inheritdoc} |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getClass(): ReflectionClass |
|
59
|
|
|
{ |
|
60
|
|
|
$class = $this->parameter->getDeclaringClass(); |
|
61
|
|
|
if (! $class instanceof ReflectionClass) { |
|
62
|
|
|
throw new LogicException(); // @codeCoverageIgnore |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
return $class; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* {@inheritdoc} |
|
70
|
|
|
* |
|
71
|
|
|
* @return array<(object|null)> |
|
72
|
|
|
* |
|
73
|
|
|
* @psalm-suppress ImplementedReturnTypeMismatch |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getQualifiers(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return [$this->getQualifier()]; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
* |
|
83
|
|
|
* @return object|null |
|
84
|
|
|
*/ |
|
85
|
|
|
public function getQualifier() |
|
86
|
|
|
{ |
|
87
|
|
|
$class = $this->parameter->getDeclaringClass(); |
|
88
|
|
|
if (! $class instanceof ReflectionClass) { |
|
89
|
|
|
throw new LogicException(); // @codeCoverageIgnore |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
$qualifierFile = sprintf( |
|
93
|
|
|
ScriptInjector::QUALIFIER, |
|
94
|
|
|
$this->scriptDir, |
|
95
|
|
|
str_replace('\\', '_', $class->name), |
|
96
|
|
|
$this->parameter->getDeclaringFunction()->name, |
|
97
|
|
|
$this->parameter->name |
|
98
|
|
|
); |
|
99
|
|
|
if (! file_exists($qualifierFile)) { |
|
100
|
|
|
return null; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
$qualifier = file_get_contents($qualifierFile); |
|
104
|
|
|
if (is_bool($qualifier)) { |
|
105
|
|
|
throw new RuntimeException(); // @codeCoverageIgnore |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
return unserialize($qualifier, ['allowed_classes' => true]); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|