|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.routing.dic; |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 5 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
| 6 | +import fr.adrienbrault.idea.symfony2plugin.routing.Route; |
| 7 | +import fr.adrienbrault.idea.symfony2plugin.routing.RouteHelper; |
| 8 | +import fr.adrienbrault.idea.symfony2plugin.stubs.ContainerCollectionResolver; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil; |
| 10 | +import org.jetbrains.annotations.NotNull; |
| 11 | + |
| 12 | +import java.util.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 16 | + */ |
| 17 | +public class ServiceRouteContainer { |
| 18 | + |
| 19 | + private final Collection<Route> routes; |
| 20 | + private ContainerCollectionResolver.LazyServiceCollector lazyServiceCollector; |
| 21 | + |
| 22 | + private Map<String, PhpClass> serviceCache = new HashMap<String, PhpClass>(); |
| 23 | + |
| 24 | + private ServiceRouteContainer(@NotNull Collection<Route> routes) { |
| 25 | + this.routes = routes; |
| 26 | + } |
| 27 | + |
| 28 | + public Set<String> getServiceNames() { |
| 29 | + |
| 30 | + Set<String> services = new HashSet<String>(); |
| 31 | + |
| 32 | + for (Route route : this.routes) { |
| 33 | + |
| 34 | + String controller = route.getController(); |
| 35 | + if(controller == null) { |
| 36 | + continue; |
| 37 | + } |
| 38 | + |
| 39 | + String[] split = controller.split(":"); |
| 40 | + if(split.length > 1) { |
| 41 | + services.add(split[0]); |
| 42 | + } |
| 43 | + |
| 44 | + } |
| 45 | + |
| 46 | + return services; |
| 47 | + } |
| 48 | + |
| 49 | + @NotNull |
| 50 | + public Collection<Route> getMethodMatches(Method method) { |
| 51 | + |
| 52 | + PhpClass originClass = method.getContainingClass(); |
| 53 | + if(originClass == null) { |
| 54 | + return Collections.emptyList(); |
| 55 | + } |
| 56 | + |
| 57 | + String classFqn = originClass.getPresentableFQN(); |
| 58 | + if(classFqn == null) { |
| 59 | + return Collections.emptyList(); |
| 60 | + } |
| 61 | + |
| 62 | + Collection<Route> routes = new ArrayList<Route>(); |
| 63 | + |
| 64 | + for (Route route : this.routes) { |
| 65 | + |
| 66 | + String serviceRoute = route.getController(); |
| 67 | + if(serviceRoute == null) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + String[] split = serviceRoute.split(":"); |
| 72 | + if(split.length != 2) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + if(!serviceCache.containsKey(split[0])) { |
| 77 | + serviceCache.put(split[0], ServiceUtil.getResolvedClassDefinition(method.getProject(), split[0], getLazyServiceCollector(method.getProject()))); |
| 78 | + } |
| 79 | + |
| 80 | + PhpClass phpClass = serviceCache.get(split[0]); |
| 81 | + if(phpClass != null && classFqn.equals(phpClass.getPresentableFQN())) { |
| 82 | + routes.add(route); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + return routes; |
| 87 | + } |
| 88 | + |
| 89 | + private ContainerCollectionResolver.LazyServiceCollector getLazyServiceCollector(Project project) { |
| 90 | + return this.lazyServiceCollector == null ? this.lazyServiceCollector = new ContainerCollectionResolver.LazyServiceCollector(project) : this.lazyServiceCollector; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Build container which stores all service routes |
| 95 | + * |
| 96 | + * @param routes Unfiltered routes |
| 97 | + */ |
| 98 | + @NotNull |
| 99 | + public static ServiceRouteContainer build(@NotNull Map<String, Route> routes) { |
| 100 | + |
| 101 | + Collection<Route> serviceRoutes = new ArrayList<Route>(); |
| 102 | + |
| 103 | + for (Route route : routes.values()) { |
| 104 | + |
| 105 | + String controller = route.getController(); |
| 106 | + if(controller == null || !RouteHelper.isServiceController(controller)) { |
| 107 | + continue; |
| 108 | + } |
| 109 | + |
| 110 | + String[] split = controller.split(":"); |
| 111 | + if(split.length > 1) { |
| 112 | + serviceRoutes.add(route); |
| 113 | + } |
| 114 | + |
| 115 | + } |
| 116 | + |
| 117 | + return new ServiceRouteContainer(serviceRoutes); |
| 118 | + } |
| 119 | + |
| 120 | +} |
0 commit comments