@@ -31,17 +31,17 @@ class AttributeBasedAutowiring implements DefinitionSource, Autowiring
31
31
{
32
32
// Annotations configuration flags:
33
33
// enable on implicit definitions
34
- const IMPLICIT = 1 ;
34
+ public const IMPLICIT = 1 ;
35
35
// enable on all autowire definitions (which are written in DI config) by default
36
- const EXPLICIT = 2 ;
36
+ public const EXPLICIT = 2 ;
37
37
// read @Injectable annotations for classes
38
- const INJECTABLE = 4 ;
38
+ public const INJECTABLE = 4 ;
39
39
// read @Inject annotations for properties
40
- const PROPERTIES = 8 ;
40
+ public const PROPERTIES = 8 ;
41
41
// read @Inject annotations for methods' parameters
42
- const METHODS = 16 ;
42
+ public const METHODS = 16 ;
43
43
// all options enabled
44
- const ALL = 31 ;
44
+ public const ALL = 31 ;
45
45
46
46
public function __construct (private int $ flags = 0 )
47
47
{
@@ -57,17 +57,17 @@ public function autowire(string $name, ObjectDefinition $definition = null) : Ob
57
57
}
58
58
59
59
$ definition = $ definition ?: new ObjectDefinition ($ name );
60
- $ useAnnotations = $ definition instanceof AutowireDefinition
61
- ? ($ definition ->isUsingAnnotations () ?? ($ this ->flags & self ::EXPLICIT ))
62
- : ($ this ->flags & self ::IMPLICIT );
60
+ $ useAttributes = $ definition instanceof AutowireDefinition
61
+ ? ($ definition ->isUsingAttributes () ?? ( bool ) ($ this ->flags & self ::EXPLICIT ))
62
+ : (bool ) ( $ this ->flags & self ::IMPLICIT );
63
63
64
64
$ class = null ;
65
- if ($ useAnnotations && $ this ->flags >= self ::INJECTABLE ) {
65
+ if ($ useAttributes && $ this ->flags >= self ::INJECTABLE ) {
66
66
$ class = new ReflectionClass ($ className );
67
67
68
- $ this ->readInjectableAttribute ($ class , $ definition );
68
+ $ this ->readInjectableAttribute ($ class , $ definition );
69
69
if ($ this ->flags & self ::INJECTABLE ) {
70
- $ this ->readInjectableAnnotation ($ class , $ definition );
70
+ $ this ->readInjectableAttribute ($ class , $ definition );
71
71
}
72
72
73
73
// Browse the class properties looking for annotated properties
@@ -83,8 +83,8 @@ public function autowire(string $name, ObjectDefinition $definition = null) : Ob
83
83
84
84
// constructor parameters should always be read, even if annotations are disabled (completely or i.a. for methods)
85
85
// so that it behaves at least as ReflectionBasedAutowiring
86
- if (!$ useAnnotations || !($ this ->flags & self ::METHODS )) {
87
- $ class = $ class ?? new ReflectionClass ($ className );
86
+ if (!$ useAttributes || !($ this ->flags & self ::METHODS )) {
87
+ $ class ??= new ReflectionClass ($ className );
88
88
$ this ->readConstructor ($ class , $ definition );
89
89
}
90
90
@@ -109,7 +109,7 @@ public function getDefinitions() : array
109
109
}
110
110
111
111
/**
112
- * Browse the class properties looking for annotated properties.
112
+ * Browse the class properties looking for properties with attributes .
113
113
*/
114
114
private function readProperties (ReflectionClass $ class , ObjectDefinition $ definition ) : void
115
115
{
@@ -182,7 +182,7 @@ private function readProperty(ReflectionProperty $property, ObjectDefinition $de
182
182
}
183
183
184
184
/**
185
- * Browse the object's methods looking for annotated methods.
185
+ * Browse the object's methods looking for methods with attributes .
186
186
*/
187
187
private function readMethods (ReflectionClass $ class , ObjectDefinition $ objectDefinition ) : void
188
188
{
@@ -214,17 +214,17 @@ private function getMethodInjection(ReflectionMethod $method) : ?MethodInjection
214
214
if ($ attribute ) {
215
215
/** @var Inject $inject */
216
216
$ inject = $ attribute ->newInstance ();
217
- $ annotationParameters = $ inject ->getParameters ();
217
+ $ attributeParameters = $ inject ->getParameters ();
218
218
} elseif ($ method ->isConstructor ()) {
219
219
// #[Inject] on constructor is implicit, we continue
220
- $ annotationParameters = [];
220
+ $ attributeParameters = [];
221
221
} else {
222
222
return null ;
223
223
}
224
224
225
225
$ parameters = [];
226
226
foreach ($ method ->getParameters () as $ index => $ parameter ) {
227
- $ entryName = $ this ->getMethodParameter ($ index , $ parameter , $ annotationParameters );
227
+ $ entryName = $ this ->getMethodParameter ($ index , $ parameter , $ attributeParameters );
228
228
229
229
if ($ entryName !== null ) {
230
230
$ parameters [$ index ] = new Reference ($ entryName );
@@ -241,7 +241,7 @@ private function getMethodInjection(ReflectionMethod $method) : ?MethodInjection
241
241
/**
242
242
* @return string|null Entry name or null if not found.
243
243
*/
244
- private function getMethodParameter (int $ parameterIndex , ReflectionParameter $ parameter , array $ annotationParameters ) : ?string
244
+ private function getMethodParameter (int $ parameterIndex , ReflectionParameter $ parameter , array $ attributeParameters ) : ?string
245
245
{
246
246
// Let's check if this parameter has an #[Inject] attribute
247
247
$ attribute = $ parameter ->getAttributes (Inject::class)[0 ] ?? null ;
@@ -253,11 +253,11 @@ private function getMethodParameter(int $parameterIndex, ReflectionParameter $pa
253
253
}
254
254
255
255
// #[Inject] has definition for this parameter (by index, or by name)
256
- if (isset ($ annotationParameters [$ parameterIndex ])) {
257
- return $ annotationParameters [$ parameterIndex ];
256
+ if (isset ($ attributeParameters [$ parameterIndex ])) {
257
+ return $ attributeParameters [$ parameterIndex ];
258
258
}
259
- if (isset ($ annotationParameters [$ parameter ->getName ()])) {
260
- return $ annotationParameters [$ parameter ->getName ()];
259
+ if (isset ($ attributeParameters [$ parameter ->getName ()])) {
260
+ return $ attributeParameters [$ parameter ->getName ()];
261
261
}
262
262
263
263
// Skip optional parameters if not explicitly defined
0 commit comments