@@ -247,14 +247,6 @@ public static ControllerClassOnShortcutReturn getControllerClassOnShortcut(@NotN
247247 return null ;
248248 }
249249
250- private static <E > ArrayList <E > makeCollection (Iterable <E > iter ) {
251- ArrayList <E > list = new ArrayList <>();
252- for (E item : iter ) {
253- list .add (item );
254- }
255- return list ;
256- }
257-
258250 private static String getPath (Project project , String path ) {
259251 if (!FileUtil .isAbsolute (path )) { // Project relative path
260252 path = project .getBasePath () + "/" + path ;
@@ -344,7 +336,6 @@ public static Map<String, Route> getRoutesInsideUrlGeneratorFile(@NotNull Projec
344336 return getRoutesInsideUrlGeneratorFile (psiFile );
345337 }
346338
347-
348339 /**
349340 * Temporary or remote files dont support "isInstanceOf", check for string implementation first
350341 */
@@ -368,9 +359,30 @@ private static boolean isRouteClass(@NotNull PhpClass phpClass) {
368359
369360 @ NotNull
370361 public static Map <String , Route > getRoutesInsideUrlGeneratorFile (@ NotNull PsiFile psiFile ) {
371-
372362 Map <String , Route > routes = new HashMap <>();
373363
364+ // Symfony >= 4
365+ // extract the routes on a return statement
366+ // return [['route'] => [...]]
367+ for (PhpReturn phpReturn : PsiTreeUtil .findChildrenOfType (psiFile , PhpReturn .class )) {
368+ PsiElement argument = phpReturn .getArgument ();
369+ if (!(argument instanceof ArrayCreationExpression )) {
370+ continue ;
371+ }
372+
373+ // get only the inside arrays
374+ // [[..], [..]] => [..], [..]
375+ for (Map .Entry <String , PsiElement > routeArray : PhpElementsUtil .getArrayKeyValueMapWithValueAsPsiElement ((ArrayCreationExpression ) argument ).entrySet ()) {
376+ List <ArrayCreationExpression > routeArrayOptions = new ArrayList <>();
377+ for (PhpPsiElement routeOption : PsiTreeUtil .getChildrenOfTypeAsList (routeArray .getValue (), PhpPsiElement .class )) {
378+ routeArrayOptions .add (PsiTreeUtil .getChildOfType (routeOption , ArrayCreationExpression .class ));
379+ }
380+
381+ routes .put (routeArray .getKey (), convertRouteConfigForReturnArray (routeArray .getKey (), routeArrayOptions ));
382+ }
383+ }
384+
385+ // Symfony < 4
374386 // heavy stuff here, to get nested routing array :)
375387 // list($variables, $defaults, $requirements, $tokens, $hostTokens)
376388 Collection <PhpClass > phpClasses = PsiTreeUtil .findChildrenOfType (psiFile , PhpClass .class );
@@ -454,9 +466,60 @@ private static void collectRoutesOnArrayCreation(@NotNull Map<String, Route> rou
454466 }
455467 }
456468
469+ /**
470+ * Used in Symfony > 4 where routes are wrapped into a return array
471+ */
472+ @ NotNull
473+ private static Route convertRouteConfigForReturnArray (@ NotNull String routeName , @ NotNull List <ArrayCreationExpression > hashElementCollection ) {
474+ Set <String > variables = new HashSet <>();
475+ if (hashElementCollection .size () >= 1 && hashElementCollection .get (0 ) != null ) {
476+ ArrayCreationExpression value = hashElementCollection .get (0 );
477+ if (value != null ) {
478+ variables .addAll (PhpElementsUtil .getArrayValuesAsString (value ));
479+ }
480+ }
481+
482+ Map <String , String > defaults = new HashMap <>();
483+ if (hashElementCollection .size () >= 2 && hashElementCollection .get (1 ) != null ) {
484+ ArrayCreationExpression value = hashElementCollection .get (1 );
485+ if (value != null ) {
486+ defaults = PhpElementsUtil .getArrayKeyValueMap (value );
487+ }
488+ }
489+
490+ Map <String , String >requirements = new HashMap <>();
491+ if (hashElementCollection .size () >= 3 && hashElementCollection .get (2 ) != null ) {
492+ ArrayCreationExpression value = hashElementCollection .get (2 );
493+ if (value != null ) {
494+ requirements = PhpElementsUtil .getArrayKeyValueMap (value );
495+ }
496+ }
497+
498+ List <Collection <String >> tokens = new ArrayList <>();
499+ if (hashElementCollection .size () >= 4 && hashElementCollection .get (3 ) != null ) {
500+ ArrayCreationExpression tokenArray = hashElementCollection .get (3 );
501+ if (tokenArray != null ) {
502+ for (ArrayHashElement tokenArrayConfig : tokenArray .getHashElements ()) {
503+ if (tokenArrayConfig .getValue () instanceof ArrayCreationExpression ) {
504+ Map <String , String > arrayKeyValueMap = PhpElementsUtil .getArrayKeyValueMap ((ArrayCreationExpression ) tokenArrayConfig .getValue ());
505+ tokens .add (arrayKeyValueMap .values ());
506+ }
507+ }
508+ }
509+
510+ }
511+
512+ // hostTokens = 4 need them?
513+ return new Route (routeName , variables , defaults , requirements , tokens );
514+ }
515+
516+ /**
517+ * Used in Symfony < 4 where routes are wrapped into a class
518+ */
457519 @ NotNull
458520 private static Route convertRouteConfig (@ NotNull String routeName , @ NotNull ArrayCreationExpression hashValue ) {
459- List <ArrayHashElement > hashElementCollection = makeCollection (hashValue .getHashElements ());
521+ List <ArrayHashElement > hashElementCollection = new ArrayList <>();
522+ hashValue .getHashElements ().forEach (hashElementCollection ::add );
460523
461524 Set <String > variables = new HashSet <>();
462525 if (hashElementCollection .size () >= 1 && hashElementCollection .get (0 ).getValue () instanceof ArrayCreationExpression ) {
0 commit comments