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