|
1 | 1 | package fr.adrienbrault.idea.symfony2plugin.action;
|
2 | 2 |
|
3 | 3 |
|
| 4 | +import com.intellij.codeInspection.ProblemsHolder; |
4 | 5 | import com.intellij.ide.IdeView;
|
5 | 6 | import com.intellij.ide.highlighter.XmlFileType;
|
6 | 7 | import com.intellij.openapi.actionSystem.AnActionEvent;
|
|
15 | 16 | import com.intellij.psi.PsiFile;
|
16 | 17 | import com.intellij.psi.PsiFileFactory;
|
17 | 18 | import com.intellij.psi.codeStyle.CodeStyleManager;
|
| 19 | +import com.intellij.psi.util.PsiTreeUtil; |
| 20 | +import com.intellij.psi.xml.XmlAttribute; |
| 21 | +import com.intellij.psi.xml.XmlTag; |
| 22 | +import com.jetbrains.php.lang.psi.elements.Method; |
| 23 | +import com.jetbrains.php.lang.psi.elements.Parameter; |
| 24 | +import com.jetbrains.php.lang.psi.elements.PhpClass; |
18 | 25 | import fr.adrienbrault.idea.symfony2plugin.Symfony2Icons;
|
| 26 | +import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil; |
| 27 | +import fr.adrienbrault.idea.symfony2plugin.action.ui.SymfonyCreateService; |
| 28 | +import fr.adrienbrault.idea.symfony2plugin.dic.ContainerService; |
| 29 | +import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil; |
19 | 30 | import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
|
| 31 | +import fr.adrienbrault.idea.symfony2plugin.util.dict.ServiceUtil; |
20 | 32 | import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
|
21 | 33 | import org.apache.commons.lang.StringUtils;
|
| 34 | +import org.jetbrains.annotations.NotNull; |
| 35 | +import org.jetbrains.annotations.Nullable; |
22 | 36 | import org.jetbrains.yaml.YAMLFileType;
|
23 | 37 |
|
24 | 38 | import java.io.IOException;
|
| 39 | +import java.util.*; |
25 | 40 |
|
26 | 41 | public class ServiceActionUtil {
|
27 | 42 |
|
@@ -98,4 +113,112 @@ public void run() {
|
98 | 113 |
|
99 | 114 | }
|
100 | 115 |
|
| 116 | + @NotNull |
| 117 | + public static Set<String> getPossibleServices(Project project, String type, Map<String, ContainerService> serviceClasses) { |
| 118 | + |
| 119 | + Set<String> possibleServices = new LinkedHashSet<String>(); |
| 120 | + List<ContainerService> matchedContainer = new ArrayList<ContainerService>(); |
| 121 | + |
| 122 | + PhpClass typeClass = PhpElementsUtil.getClassInterface(project, type); |
| 123 | + if(typeClass != null) { |
| 124 | + for(Map.Entry<String, ContainerService> entry: serviceClasses.entrySet()) { |
| 125 | + if(entry.getValue().getClassName() != null) { |
| 126 | + PhpClass serviceClass = PhpElementsUtil.getClassInterface(project, entry.getValue().getClassName()); |
| 127 | + if(serviceClass != null) { |
| 128 | + if(new Symfony2InterfacesUtil().isInstanceOf(serviceClass, typeClass)) { |
| 129 | + matchedContainer.add(entry.getValue()); |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + if(matchedContainer.size() > 0) { |
| 138 | + |
| 139 | + // weak service have lower priority |
| 140 | + Collections.sort(matchedContainer, new SymfonyCreateService.ContainerServicePriorityWeakComparator()); |
| 141 | + |
| 142 | + // lower priority of services like "doctrine.orm.default_entity_manager" |
| 143 | + Collections.sort(matchedContainer, new SymfonyCreateService.ContainerServicePriorityNameComparator()); |
| 144 | + |
| 145 | + for(ContainerService containerService: matchedContainer) { |
| 146 | + possibleServices.add(containerService.getName()); |
| 147 | + } |
| 148 | + |
| 149 | + } |
| 150 | + |
| 151 | + return possibleServices; |
| 152 | + } |
| 153 | + |
| 154 | + @NotNull |
| 155 | + public static Collection<XmlTag> getXmlContainerServiceDefinition(PsiFile psiFile) { |
| 156 | + |
| 157 | + Collection<XmlTag> xmlTags = new ArrayList<XmlTag>(); |
| 158 | + |
| 159 | + for(XmlTag xmlTag: PsiTreeUtil.getChildrenOfTypeAsList(psiFile.getFirstChild(), XmlTag.class)) { |
| 160 | + if(xmlTag.getName().equals("container")) { |
| 161 | + for(XmlTag servicesTag: xmlTag.getSubTags()) { |
| 162 | + if(servicesTag.getName().equals("services")) { |
| 163 | + for(XmlTag parameterTag: servicesTag.getSubTags()) { |
| 164 | + if(parameterTag.getName().equals("service")) { |
| 165 | + xmlTags.add(parameterTag); |
| 166 | + } |
| 167 | + } |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + return xmlTags; |
| 174 | + } |
| 175 | + |
| 176 | + @Nullable |
| 177 | + public static List<String> getXmlMissingArgumentTypes(XmlTag xmlTag) { |
| 178 | + |
| 179 | + XmlAttribute classAttribute = xmlTag.getAttribute("class"); |
| 180 | + if(classAttribute == null) { |
| 181 | + return null; |
| 182 | + } |
| 183 | + |
| 184 | + String value = classAttribute.getValue(); |
| 185 | + if(StringUtils.isBlank(value)) { |
| 186 | + return null; |
| 187 | + } |
| 188 | + |
| 189 | + // @TODO: cache defs |
| 190 | + PhpClass resolvedClassDefinition = ServiceUtil.getResolvedClassDefinition(xmlTag.getProject(), value); |
| 191 | + if(resolvedClassDefinition == null) { |
| 192 | + return null; |
| 193 | + } |
| 194 | + |
| 195 | + Method constructor = resolvedClassDefinition.getConstructor(); |
| 196 | + if(constructor == null) { |
| 197 | + return null; |
| 198 | + } |
| 199 | + |
| 200 | + int serviceArguments = 0; |
| 201 | + |
| 202 | + for (XmlTag tag : xmlTag.getSubTags()) { |
| 203 | + if("argument".equals(tag.getName())) { |
| 204 | + serviceArguments++; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + Parameter[] parameters = constructor.getParameters(); |
| 209 | + if(parameters.length <= serviceArguments) { |
| 210 | + return null; |
| 211 | + } |
| 212 | + |
| 213 | + final List<String> args = new ArrayList<String>(); |
| 214 | + |
| 215 | + for (int i = serviceArguments; i < parameters.length; i++) { |
| 216 | + Parameter parameter = parameters[i]; |
| 217 | + String s = parameter.getDeclaredType().toString(); |
| 218 | + args.add(s); |
| 219 | + } |
| 220 | + |
| 221 | + return args; |
| 222 | + } |
| 223 | + |
101 | 224 | }
|
0 commit comments