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