|
| 1 | +package fr.adrienbrault.idea.symfony2plugin; |
| 2 | + |
| 3 | +import com.intellij.openapi.project.Project; |
| 4 | +import com.intellij.psi.PsiElement; |
| 5 | +import com.jetbrains.php.lang.psi.elements.impl.MethodImpl; |
| 6 | +import com.jetbrains.php.lang.psi.elements.impl.MethodReferenceImpl; |
| 7 | +import com.jetbrains.php.lang.psi.elements.impl.StringLiteralExpressionImpl; |
| 8 | +import com.jetbrains.php.lang.psi.resolve.types.PhpType; |
| 9 | +import com.jetbrains.php.lang.psi.resolve.types.PhpTypeProvider; |
| 10 | +import org.jetbrains.annotations.Nullable; |
| 11 | +import org.w3c.dom.*; |
| 12 | +import org.xml.sax.SAXException; |
| 13 | + |
| 14 | +import javax.xml.parsers.*; |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.util.HashMap; |
| 18 | +import java.util.Map; |
| 19 | + |
| 20 | +/** |
| 21 | + * @author Adrien Brault <adrien.brault@gmail.com> |
| 22 | + */ |
| 23 | +public class SymfonyContainerTypeProvider implements PhpTypeProvider { |
| 24 | + |
| 25 | + @Nullable |
| 26 | + @Override |
| 27 | + public PhpType getType(PsiElement e) { |
| 28 | + if (!isContainerGetCall(e)) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + |
| 32 | + String serviceId = getServiceId((MethodReferenceImpl) e); |
| 33 | + if (null == serviceId) { |
| 34 | + return null; |
| 35 | + } |
| 36 | + |
| 37 | + Map<String, String> serviceMap = getServicesMap(e.getProject()); |
| 38 | + String serviceClass = serviceMap.get(serviceId); |
| 39 | + |
| 40 | + if (null == serviceClass) { |
| 41 | + return null; |
| 42 | + } |
| 43 | + |
| 44 | + return new PhpType().add(serviceClass); |
| 45 | + } |
| 46 | + |
| 47 | + private boolean isContainerGetCall(PsiElement e) { |
| 48 | + if (!(e instanceof MethodReferenceImpl)) { |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + MethodReferenceImpl methodRefImpl = (MethodReferenceImpl) e; |
| 53 | + if (null == e.getReference()) { |
| 54 | + return false; |
| 55 | + } |
| 56 | + |
| 57 | + PsiElement resolvedReference = methodRefImpl.getReference().resolve(); |
| 58 | + if (!(resolvedReference instanceof MethodImpl)) { |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + MethodImpl methodImpl = (MethodImpl) resolvedReference; |
| 63 | + String methodFQN = methodImpl.getFQN(); // Something like "\Symfony\Bundle\FrameworkBundle\Controller\Controller.get" |
| 64 | + if (null == methodFQN) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + if (methodFQN.equals("\\Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller.get") |
| 69 | + || methodFQN.equals("\\Symfony\\Component\\DependencyInjection\\ContainerInterface.get")) { |
| 70 | + return true; |
| 71 | + } |
| 72 | + |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + private String getServiceId(MethodReferenceImpl e) { |
| 77 | + String serviceId = null; |
| 78 | + |
| 79 | + PsiElement[] parameters = e.getParameters(); |
| 80 | + if (parameters.length > 0 && parameters[0] instanceof StringLiteralExpressionImpl) { |
| 81 | + serviceId = parameters[0].getText(); // quoted string |
| 82 | + serviceId = serviceId.substring(1, serviceId.length() - 1); |
| 83 | + } |
| 84 | + |
| 85 | + return serviceId; |
| 86 | + } |
| 87 | + |
| 88 | + private Map<String, String> cachedServiceMap; |
| 89 | + private long cachedServiceMapLastModified; |
| 90 | + |
| 91 | + private Map<String, String>getServicesMap(Project project) { |
| 92 | + Map<String, String> map = new HashMap<String, String>(); |
| 93 | + |
| 94 | + String defaultServiceMapFilePath = project.getBasePath() + "/app/cache/dev/appDevDebugProjectContainer.xml"; |
| 95 | + File xmlFile = new File(defaultServiceMapFilePath); |
| 96 | + if (!xmlFile.exists()) { |
| 97 | + return map; |
| 98 | + } |
| 99 | + |
| 100 | + long xmlFileLastModified = xmlFile.lastModified(); |
| 101 | + if (xmlFileLastModified == cachedServiceMapLastModified) { |
| 102 | + return cachedServiceMap; |
| 103 | + } |
| 104 | + |
| 105 | + Document doc = null; |
| 106 | + try { |
| 107 | + DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); |
| 108 | + DocumentBuilder dBuilder = dbFactory.newDocumentBuilder(); |
| 109 | + doc = dBuilder.parse(xmlFile); |
| 110 | + } catch (SAXException e) { |
| 111 | + e.printStackTrace(); |
| 112 | + |
| 113 | + return map; |
| 114 | + } catch (IOException e) { |
| 115 | + e.printStackTrace(); |
| 116 | + |
| 117 | + return map; |
| 118 | + } catch (ParserConfigurationException e) { |
| 119 | + e.printStackTrace(); |
| 120 | + |
| 121 | + return map; |
| 122 | + } |
| 123 | + |
| 124 | + NodeList servicesNodes = doc.getElementsByTagName("service"); |
| 125 | + for (int i = 0; i < servicesNodes.getLength(); i++) { |
| 126 | + Element node = (Element) servicesNodes.item(i); |
| 127 | + map.put(node.getAttribute("id"), "\\" + node.getAttribute("class")); |
| 128 | + } |
| 129 | + |
| 130 | + cachedServiceMap = map; |
| 131 | + cachedServiceMapLastModified = xmlFileLastModified; |
| 132 | + |
| 133 | + return map; |
| 134 | + } |
| 135 | + |
| 136 | +} |
0 commit comments