|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Di; |
|
6
|
|
|
|
|
7
|
|
|
use phpDocumentor\Reflection\Types\ClassString; |
|
8
|
|
|
use Ray\Aop\ReflectionMethod; |
|
9
|
|
|
use Ray\Di\Di\Named; |
|
10
|
|
|
use Reflection; |
|
11
|
|
|
use ReflectionAttribute; |
|
12
|
|
|
use ReflectionParameter; |
|
13
|
|
|
|
|
14
|
|
|
use function assert; |
|
15
|
|
|
use function class_exists; |
|
16
|
|
|
use function explode; |
|
17
|
|
|
use function is_string; |
|
18
|
|
|
use function preg_match; |
|
19
|
|
|
use function substr; |
|
20
|
|
|
use function trim; |
|
21
|
|
|
|
|
22
|
|
|
final class Name |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* 'Unnamed' name |
|
26
|
|
|
*/ |
|
27
|
|
|
public const ANY = ''; |
|
28
|
|
|
|
|
29
|
|
|
/** @var string */ |
|
30
|
|
|
private $name = ''; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Named database |
|
34
|
|
|
* |
|
35
|
|
|
* format: array<varName, NamedName> |
|
36
|
|
|
* |
|
37
|
|
|
* @var array<string, string> |
|
38
|
|
|
*/ |
|
39
|
|
|
private $names = []; |
|
40
|
|
|
|
|
41
|
|
|
public function __construct(?string $name = null) |
|
42
|
|
|
{ |
|
43
|
|
|
if ($name !== null) { |
|
44
|
|
|
$this->setName($name); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Create instance from PHP8 attributes |
|
50
|
|
|
* |
|
51
|
|
|
* @psalm-suppress MixedAssignment |
|
52
|
|
|
* @psalm-suppress UndefinedMethod |
|
53
|
|
|
* @psalm-suppress MixedMethodCall |
|
54
|
|
|
* @psalm-suppress MixedArrayAccess |
|
55
|
|
|
* |
|
56
|
|
|
* psalm does not know ReflectionAttribute?? PHPStan produces no type error here. |
|
57
|
|
|
*/ |
|
58
|
|
|
public function createFromAttributes(\ReflectionMethod $method): ?self |
|
59
|
|
|
{ |
|
60
|
|
|
$params = $method->getParameters(); |
|
61
|
|
|
foreach ($params as $param) { |
|
62
|
|
|
$attribue = $param->getAttributes(Named::class); |
|
63
|
|
|
if ($attribue) { |
|
64
|
|
|
$name = $attribue[0]->newInstance(); |
|
65
|
|
|
assert($name instanceof Named); |
|
66
|
|
|
$this->names[$param->getName()] = $name->value; |
|
|
|
|
|
|
67
|
|
|
} |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
if ($this->names) { |
|
|
|
|
|
|
71
|
|
|
return clone $this; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
return null; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
public function __invoke(ReflectionParameter $parameter): string |
|
78
|
|
|
{ |
|
79
|
|
|
// single variable named binding |
|
80
|
|
|
if ($this->name) { |
|
81
|
|
|
return $this->name; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// multiple variable named binding |
|
85
|
|
|
if (isset($this->names[$parameter->name])) { |
|
86
|
|
|
return $this->names[$parameter->name]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// ANY match |
|
90
|
|
|
if (isset($this->names[self::ANY])) { |
|
91
|
|
|
return $this->names[self::ANY]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
// not matched |
|
95
|
|
|
return self::ANY; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
private function setName(string $name): void |
|
99
|
|
|
{ |
|
100
|
|
|
// annotation |
|
101
|
|
|
if (class_exists($name, false)) { |
|
102
|
|
|
$this->name = $name; |
|
103
|
|
|
|
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// single name |
|
108
|
|
|
// @Named(name) |
|
109
|
|
|
if ($name === self::ANY || preg_match('/^\w+$/', $name)) { |
|
110
|
|
|
$this->name = $name; |
|
111
|
|
|
|
|
112
|
|
|
return; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
// name list |
|
116
|
|
|
// @Named(varName1=name1, varName2=name2)] |
|
117
|
|
|
$this->names = $this->parseName($name); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @return array<string, string> |
|
|
|
|
|
|
122
|
|
|
*/ |
|
123
|
|
|
private function parseName(string $name): array |
|
124
|
|
|
{ |
|
125
|
|
|
$names = []; |
|
126
|
|
|
$keyValues = explode(',', $name); |
|
127
|
|
|
foreach ($keyValues as $keyValue) { |
|
128
|
|
|
$exploded = explode('=', $keyValue); |
|
129
|
|
|
if (isset($exploded[1])) { |
|
130
|
|
|
[$key, $value] = $exploded; |
|
|
|
|
|
|
131
|
|
|
assert(is_string($key)); |
|
132
|
|
|
if (isset($key[0]) && $key[0] === '$') { |
|
133
|
|
|
$key = substr($key, 1); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$names[trim($key)] = trim($value); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
return $names; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|