@@ -59,10 +59,19 @@ public void visitFile(@NotNull PhpClass phpClass) {
59
59
60
60
PhpAttributesList childOfType = PsiTreeUtil .getChildOfType (method , PhpAttributesList .class );
61
61
if (childOfType != null ) {
62
- visitPhpAttributesList (childOfType );
62
+ visitPhpAttributesList (childOfType , method , phpClass , false );
63
+ }
64
+ }
65
+
66
+ Method invoke = phpClass .findOwnMethodByName ("__invoke" );
67
+ if (invoke != null ) {
68
+ PhpAttributesList childOfType = PsiTreeUtil .getChildOfType (phpClass , PhpAttributesList .class );
69
+ if (childOfType != null ) {
70
+ visitPhpAttributesList (childOfType , invoke , phpClass , true );
63
71
}
64
72
}
65
73
}
74
+
66
75
private void visitPhpDocTag (@ NotNull PhpDocTag phpDocTag ) {
67
76
68
77
// "@var" and user non-related tags don't need an action
@@ -132,32 +141,33 @@ private void visitPhpDocTag(@NotNull PhpDocTag phpDocTag) {
132
141
}
133
142
}
134
143
135
- private void visitPhpAttributesList (@ NotNull PhpAttributesList phpAttributesList ) {
144
+ private void visitPhpAttributesList (@ NotNull PhpAttributesList phpAttributesList , @ NotNull Method method , @ NotNull PhpClass phpClass , boolean classLevel ) {
136
145
PsiElement parent = phpAttributesList .getParent ();
137
146
138
147
// prefix on class scope
139
148
String routeNamePrefix = "" ;
140
149
String routePathPrefix = "" ;
141
- if (parent instanceof Method ) {
142
- PhpClass containingClass = ((Method ) parent ).getContainingClass ();
143
- if (containingClass != null ) {
144
- for (PhpAttribute attribute : containingClass .getAttributes ()) {
145
- String fqn = attribute .getFQN ();
146
- if (fqn == null || !RouteHelper .isRouteClassAnnotation (fqn )) {
147
- continue ;
148
- }
149
150
150
- String nameAttribute = PhpPsiAttributesUtil .getAttributeValueByNameAsString (attribute , 1 , "name" );
151
- if (nameAttribute != null ) {
152
- routeNamePrefix = nameAttribute ;
153
- }
151
+ for (PhpAttribute attribute : phpClass .getAttributes ()) {
152
+ String fqn = attribute .getFQN ();
153
+ if (fqn == null || !RouteHelper .isRouteClassAnnotation (fqn )) {
154
+ continue ;
155
+ }
154
156
155
- String pathAttribute = PhpPsiAttributesUtil .getAttributeValueByNameAsStringWithDefaultParameterFallback (attribute , "path" );;
156
- if (pathAttribute != null ) {
157
- routePathPrefix = pathAttribute ;
158
- }
159
- }
157
+ String nameAttribute = PhpPsiAttributesUtil .getAttributeValueByNameAsString (attribute , 1 , "name" );
158
+ if (nameAttribute != null ) {
159
+ routeNamePrefix = nameAttribute ;
160
160
}
161
+
162
+ String pathAttribute = PhpPsiAttributesUtil .getAttributeValueByNameAsStringWithDefaultParameterFallback (attribute , "path" );;
163
+ if (pathAttribute != null ) {
164
+ routePathPrefix = pathAttribute ;
165
+ }
166
+ }
167
+
168
+ if (classLevel ) {
169
+ routePathPrefix = "" ;
170
+ routeNamePrefix = "" ;
161
171
}
162
172
163
173
for (PhpAttribute attribute : phpAttributesList .getAttributes ()) {
@@ -168,13 +178,11 @@ private void visitPhpAttributesList(@NotNull PhpAttributesList phpAttributesList
168
178
169
179
String nameAttribute = PhpPsiAttributesUtil .getAttributeValueByNameAsString (attribute , 1 , "name" );
170
180
171
- String routeName = null ;
181
+ String routeName ;
172
182
if (nameAttribute != null ) {
173
183
routeName = nameAttribute ;
174
184
} else {
175
- if (parent instanceof Method ) {
176
- routeName = AnnotationBackportUtil .getRouteByMethod ((Method ) parent );
177
- }
185
+ routeName = AnnotationBackportUtil .getRouteByMethod (method );
178
186
}
179
187
180
188
if (routeName == null ) {
@@ -183,8 +191,10 @@ private void visitPhpAttributesList(@NotNull PhpAttributesList phpAttributesList
183
191
184
192
StubIndexedRoute route = new StubIndexedRoute (routeNamePrefix + routeName );
185
193
186
- if (parent instanceof Method ) {
187
- route .setController (getController ((Method ) parent ));
194
+ if (classLevel ) {
195
+ route .setController (getController (phpClass ));
196
+ } else {
197
+ route .setController (getController (method ));
188
198
}
189
199
190
200
// find path "#[Route('/attributesWithoutName')]" or "#[Route(path: '/attributesWithoutName')]"
@@ -304,6 +314,18 @@ private String getController(@NotNull Method method) {
304
314
);
305
315
}
306
316
317
+ private String getController (@ NotNull PhpClass phpClass ) {
318
+ if (phpClass .findOwnMethodByName ("__invoke" ) == null ) {
319
+ return null ;
320
+ }
321
+
322
+ return String .format (
323
+ "%s::%s" ,
324
+ StringUtils .stripStart (phpClass .getFQN (), "\\ " ),
325
+ "__invoke"
326
+ );
327
+ }
328
+
307
329
@ Nullable
308
330
private String getClassRoutePattern (@ NotNull PhpDocTag phpDocTag ) {
309
331
PhpClass phpClass = PsiTreeUtil .getParentOfType (phpDocTag , PhpClass .class );
0 commit comments