19
19
import com .intellij .psi .xml .XmlAttribute ;
20
20
import com .intellij .psi .xml .XmlFile ;
21
21
import com .intellij .psi .xml .XmlTag ;
22
- import com .intellij .util .Processor ;
23
22
import com .intellij .util .containers .ContainerUtil ;
24
23
import com .intellij .util .indexing .FileBasedIndex ;
25
24
import com .intellij .util .indexing .FileBasedIndexImpl ;
60
59
61
60
import java .io .File ;
62
61
import java .util .*;
62
+ import java .util .stream .Collectors ;
63
63
64
64
public class RouteHelper {
65
65
@@ -514,38 +514,27 @@ private static String convertLanguageRouteName(String routeName) {
514
514
* Foo\Bar::methodAction
515
515
*/
516
516
@ Nullable
517
- public static String convertMethodToRouteControllerName (@ NotNull Method method ) {
518
-
517
+ private static String convertMethodToRouteControllerName (@ NotNull Method method ) {
519
518
PhpClass phpClass = method .getContainingClass ();
520
519
if (phpClass == null ) {
521
520
return null ;
522
521
}
523
522
524
- String className = phpClass .getPresentableFQN ();
525
- if (className == null ) {
526
- return null ;
527
- }
528
-
529
- return (className .startsWith ("\\ " ) ? className .substring (1 ) : className ) + "::" + method .getName ();
530
-
523
+ return StringUtils .stripStart (phpClass .getFQN (), "\\ " ) + "::" + method .getName ();
531
524
}
532
525
533
526
/**
534
527
* FooBundle:Bar::method
528
+ * FooBundle:Bar\\Foo::method
535
529
*/
536
530
@ Nullable
537
531
public static String convertMethodToRouteShortcutControllerName (@ NotNull Method method ) {
538
-
539
532
PhpClass phpClass = method .getContainingClass ();
540
533
if (phpClass == null ) {
541
534
return null ;
542
535
}
543
536
544
- String className = phpClass .getPresentableFQN ();
545
- if (className == null ) {
546
- return null ;
547
- }
548
-
537
+ String className = StringUtils .stripStart (phpClass .getFQN (), "\\ " );
549
538
int bundlePos = className .lastIndexOf ("Bundle\\ " );
550
539
if (bundlePos == -1 ) {
551
540
return null ;
@@ -555,16 +544,20 @@ public static String convertMethodToRouteShortcutControllerName(@NotNull Method
555
544
if (symfonyBundle == null ) {
556
545
return null ;
557
546
}
547
+
558
548
String name = method .getName ();
559
549
String methodName = name .substring (0 , name .length () - "Action" .length ());
560
550
561
- String relative = symfonyBundle .getRelative (phpClass .getContainingFile ().getVirtualFile (), true );
562
- if (relative == null ) {
551
+ // try to to find relative class name
552
+ String controllerClass = className .toLowerCase ();
553
+ String bundleClass = StringUtils .stripStart (symfonyBundle .getNamespaceName (), "\\ " ).toLowerCase ();
554
+ if (!controllerClass .startsWith (bundleClass )) {
563
555
return null ;
564
556
}
565
557
566
- if (relative .startsWith ("Controller/" )) {
567
- relative = relative .substring ("Controller/" .length ());
558
+ String relative = StringUtils .stripStart (phpClass .getFQN (), "\\ " ).substring (bundleClass .length ());
559
+ if (relative .startsWith ("Controller\\ " )) {
560
+ relative = relative .substring ("Controller\\ " .length ());
568
561
}
569
562
570
563
if (relative .endsWith ("Controller" )) {
@@ -578,20 +571,14 @@ public static VirtualFile[] getRouteDefinitionInsideFile(Project project, String
578
571
579
572
final List <VirtualFile > virtualFiles = new ArrayList <>();
580
573
581
- FileBasedIndexImpl .getInstance ().getFilesWithKey (RoutesStubIndex .KEY , new HashSet <>(Arrays .asList (routeNames )), new Processor <VirtualFile >() {
582
- @ Override
583
- public boolean process (VirtualFile virtualFile ) {
584
- virtualFiles .add (virtualFile );
585
- return true ;
586
- }
574
+ FileBasedIndexImpl .getInstance ().getFilesWithKey (RoutesStubIndex .KEY , new HashSet <>(Arrays .asList (routeNames )), virtualFile -> {
575
+ virtualFiles .add (virtualFile );
576
+ return true ;
587
577
}, GlobalSearchScope .getScopeRestrictedByFileTypes (GlobalSearchScope .allScope (project ), YAMLFileType .YML , XmlFileType .INSTANCE ));
588
578
589
- FileBasedIndexImpl .getInstance ().getFilesWithKey (AnnotationRoutesStubIndex .KEY , new HashSet <>(Arrays .asList (routeNames )), new Processor <VirtualFile >() {
590
- @ Override
591
- public boolean process (VirtualFile virtualFile ) {
592
- virtualFiles .add (virtualFile );
593
- return true ;
594
- }
579
+ FileBasedIndexImpl .getInstance ().getFilesWithKey (AnnotationRoutesStubIndex .KEY , new HashSet <>(Arrays .asList (routeNames )), virtualFile -> {
580
+ virtualFiles .add (virtualFile );
581
+ return true ;
595
582
}, GlobalSearchScope .getScopeRestrictedByFileTypes (GlobalSearchScope .allScope (project ), PhpFileType .INSTANCE ));
596
583
597
584
return virtualFiles .toArray (new VirtualFile [virtualFiles .size ()]);
@@ -641,7 +628,7 @@ public static Collection<StubIndexedRoute> getYamlRouteDefinitions(@NotNull YAML
641
628
642
629
String controller = getYamlController (yamlKeyValue );
643
630
if (controller != null ) {
644
- route .setController (controller );
631
+ route .setController (normalizeRouteController ( controller ) );
645
632
}
646
633
647
634
indexedRoutes .add (route );
@@ -702,7 +689,7 @@ public static Collection<StubIndexedRoute> getXmlRouteDefinitions(XmlFile psiFil
702
689
if (keyValue != null && "_controller" .equals (keyValue )) {
703
690
String actionName = subTag .getValue ().getTrimmedText ();
704
691
if (StringUtils .isNotBlank (actionName )) {
705
- e .setController (actionName );
692
+ e .setController (normalizeRouteController ( actionName ) );
706
693
}
707
694
}
708
695
}
@@ -779,39 +766,28 @@ public static boolean isServiceController(@NotNull String shortcutName) {
779
766
return !shortcutName .contains ("::" ) && shortcutName .contains (":" ) && !shortcutName .contains ("\\ " ) && shortcutName .split (":" ).length == 2 ;
780
767
}
781
768
782
- @ Nullable
769
+ @ NotNull
783
770
public static List <Route > getRoutesOnControllerAction (@ NotNull Method method ) {
784
-
785
771
Set <String > routeNames = new HashSet <>();
786
772
787
- String methodRouteActionName = RouteHelper .convertMethodToRouteControllerName (method );
788
- if (methodRouteActionName != null ) {
789
- routeNames .add (methodRouteActionName );
790
- }
791
-
792
- String shortcutName = RouteHelper .convertMethodToRouteShortcutControllerName (method );
793
- if (shortcutName != null ) {
794
- routeNames .add (shortcutName );
795
- }
773
+ ContainerUtil .addIfNotNull (routeNames , RouteHelper .convertMethodToRouteControllerName (method ));
774
+ ContainerUtil .addIfNotNull (routeNames , RouteHelper .convertMethodToRouteShortcutControllerName (method ));
796
775
797
776
Map <String , Route > allRoutes = getAllRoutes (method .getProject ());
798
777
List <Route > routes = new ArrayList <>();
799
778
800
779
// resolve indexed routes
801
780
if (routeNames .size () > 0 ) {
802
- for (Map .Entry <String , Route > routeEntry : allRoutes .entrySet ()) {
803
- String controller = routeEntry .getValue ().getController ();
804
- if (controller != null && routeNames .contains (controller )) {
805
- routes .add (routeEntry .getValue ());
806
- }
807
- }
781
+ routes .addAll (allRoutes .values ().stream ()
782
+ .filter (route -> route .getController () != null && routeNames .contains (route .getController ()))
783
+ .collect (Collectors .toList ())
784
+ );
808
785
}
809
786
810
787
// search for services
811
- Collection <Route > methodMatch = ServiceRouteContainer .build (allRoutes ).getMethodMatches (method );
812
- if (methodMatch .size () > 0 ) {
813
- routes .addAll (methodMatch );
814
- }
788
+ routes .addAll (
789
+ ServiceRouteContainer .build (allRoutes ).getMethodMatches (method )
790
+ );
815
791
816
792
return routes ;
817
793
}
@@ -1010,4 +986,8 @@ private static Map<String, Route> getAllRoutesProxy(@NotNull Project project) {
1010
986
return routes ;
1011
987
}
1012
988
1013
- }
989
+ @ NotNull
990
+ private static String normalizeRouteController (@ NotNull String string ) {
991
+ return string .replace ("/" , "\\ " );
992
+ }
993
+ }
0 commit comments