|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of Spiral Framework package. |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
declare(strict_types=1); |
|
11
|
|
|
|
|
12
|
|
|
namespace Spiral\Attributes\Internal\Key; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* A generator that returns a unique key associated with the |
|
16
|
|
|
* passed reflection object. |
|
17
|
|
|
* |
|
18
|
|
|
* @internal NameKeyGenerator is an internal library class, please do not use it in your code. |
|
19
|
|
|
* @psalm-internal Spiral\Attributes |
|
20
|
|
|
*/ |
|
21
|
|
|
final class NameKeyGenerator implements KeyGeneratorInterface |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private const TPL_ANONYMOUS_CLASS = '{anonymous@class %s:%d:%d}'; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private const TPL_ANONYMOUS_FN = '{anonymous@function %s:%d:%d}'; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var string |
|
35
|
|
|
*/ |
|
36
|
|
|
private const TPL_METHOD = '%s::%s()'; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var string |
|
40
|
|
|
*/ |
|
41
|
|
|
private const TPL_CONSTANT = '%s::%s'; |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var string |
|
45
|
|
|
*/ |
|
46
|
|
|
private const TPL_PROPERTY = '%s::$%s'; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
private const TPL_PARAMETER = '%s($%s)'; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritDoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
public function forClass(\ReflectionClass $class): string |
|
57
|
|
|
{ |
|
58
|
|
|
if ($class->isAnonymous()) { |
|
59
|
|
|
return \vsprintf(self::TPL_ANONYMOUS_CLASS, [ |
|
60
|
|
|
$class->getFileName(), |
|
61
|
|
|
$class->getStartLine(), |
|
62
|
|
|
$class->getEndLine(), |
|
63
|
|
|
]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
return $class->getName(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritDoc} |
|
71
|
|
|
*/ |
|
72
|
|
|
public function forConstant(\ReflectionClassConstant $const): string |
|
73
|
|
|
{ |
|
74
|
|
|
return \vsprintf(self::TPL_CONSTANT, [ |
|
75
|
|
|
$this->forClass($const->getDeclaringClass()), |
|
76
|
|
|
$const->getName(), |
|
77
|
|
|
]); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritDoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
public function forProperty(\ReflectionProperty $prop): string |
|
84
|
|
|
{ |
|
85
|
|
|
return \vsprintf(self::TPL_PROPERTY, [ |
|
86
|
|
|
$this->forClass($prop->getDeclaringClass()), |
|
87
|
|
|
$prop->getName(), |
|
88
|
|
|
]); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritDoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function forParameter(\ReflectionParameter $param): string |
|
95
|
|
|
{ |
|
96
|
|
|
return \vsprintf(self::TPL_PARAMETER, [ |
|
97
|
|
|
$this->forFunction($param->getDeclaringFunction()), |
|
98
|
|
|
$param->getName(), |
|
99
|
|
|
]); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritDoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
public function forFunction(\ReflectionFunctionAbstract $fn): string |
|
106
|
|
|
{ |
|
107
|
|
|
if ($fn instanceof \ReflectionMethod) { |
|
108
|
|
|
return \vsprintf(self::TPL_METHOD, [ |
|
109
|
|
|
$this->forClass($fn->getDeclaringClass()), |
|
110
|
|
|
$fn->getName(), |
|
111
|
|
|
]); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ($fn->isClosure()) { |
|
115
|
|
|
return \vsprintf(self::TPL_ANONYMOUS_FN, [ |
|
116
|
|
|
$fn->getFileName(), |
|
117
|
|
|
$fn->getStartLine(), |
|
118
|
|
|
$fn->getEndLine(), |
|
119
|
|
|
]); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
return $fn->getName(); |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|