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\InjectInterface; |
10
|
|
|
use Ray\Di\Di\Named; |
11
|
|
|
|
12
|
|
|
use function assert; |
13
|
|
|
|
14
|
|
|
use const PHP_VERSION_ID; |
15
|
|
|
|
16
|
|
|
final class AnnotatedClassMethods |
17
|
|
|
{ |
18
|
|
|
/** @var NameKeyVarString */ |
19
|
|
|
private $nameKeyVarString; |
20
|
|
|
|
21
|
|
|
public function __construct() |
22
|
|
|
{ |
23
|
|
|
$this->nameKeyVarString = new NameKeyVarString(); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @phpstan-param ReflectionClass<object> $class |
28
|
|
|
*/ |
29
|
|
|
public function getConstructorName(ReflectionClass $class): Name |
30
|
|
|
{ |
31
|
|
|
$constructor = $class->getConstructor(); |
32
|
|
|
if (! $constructor) { |
|
|
|
|
33
|
|
|
return new Name(Name::ANY); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if (PHP_VERSION_ID >= 80000) { |
37
|
|
|
$name = Name::withAttributes(new \ReflectionMethod($class->getName(), '__construct')); |
38
|
|
|
if ($name) { |
39
|
|
|
return $name; |
40
|
|
|
} |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
assert($constructor instanceof ReflectionMethod); |
44
|
|
|
$named = $constructor->getAnnotation(Named::class); |
45
|
|
|
if ($named instanceof Named) { |
46
|
|
|
return new Name($named->value); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$name = ($this->nameKeyVarString)(new ReflectionMethod($class->getName(), $constructor->getName())); |
50
|
|
|
if ($name !== null) { |
51
|
|
|
return new Name($name); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return new Name(Name::ANY); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getSetterMethod(ReflectionMethod $method): ?SetterMethod |
58
|
|
|
{ |
59
|
|
|
$inject = $method->getAnnotation(InjectInterface::class); |
60
|
|
|
if (! $inject instanceof InjectInterface) { |
61
|
|
|
return null; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$name = $this->getName($method); |
65
|
|
|
$setterMethod = new SetterMethod($method, $name); |
66
|
|
|
if ($inject->isOptional()) { |
67
|
|
|
$setterMethod->setOptional(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
return $setterMethod; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
private function getName(ReflectionMethod $method): Name |
74
|
|
|
{ |
75
|
|
|
if (PHP_VERSION_ID >= 80000) { |
76
|
|
|
$name = Name::withAttributes($method); |
77
|
|
|
if ($name) { |
78
|
|
|
return $name; |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$nameValue = ($this->nameKeyVarString)($method); |
83
|
|
|
|
84
|
|
|
return new Name($nameValue); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|