|
| 1 | +package fr.adrienbrault.idea.symfony2plugin.doctrine; |
| 2 | + |
| 3 | +import com.intellij.openapi.util.Pair; |
| 4 | +import com.intellij.psi.PsiFile; |
| 5 | +import com.intellij.psi.util.PsiTreeUtil; |
| 6 | +import com.intellij.psi.xml.XmlAttribute; |
| 7 | +import com.intellij.psi.xml.XmlFile; |
| 8 | +import com.intellij.psi.xml.XmlTag; |
| 9 | +import fr.adrienbrault.idea.symfony2plugin.util.yaml.YamlHelper; |
| 10 | +import org.apache.commons.lang.ArrayUtils; |
| 11 | +import org.apache.commons.lang.StringUtils; |
| 12 | +import org.jetbrains.annotations.NotNull; |
| 13 | +import org.jetbrains.annotations.Nullable; |
| 14 | +import org.jetbrains.yaml.psi.YAMLDocument; |
| 15 | +import org.jetbrains.yaml.psi.YAMLFile; |
| 16 | +import org.jetbrains.yaml.psi.YAMLKeyValue; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collection; |
| 20 | +import java.util.Set; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author Daniel Espendiller <daniel@espendiller.net> |
| 24 | + */ |
| 25 | +public class DoctrineUtil { |
| 26 | + |
| 27 | + /** |
| 28 | + * Index metadata file with its class and repository. |
| 29 | + * As of often class stay in static only context |
| 30 | + */ |
| 31 | + @Nullable |
| 32 | + public static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull PsiFile psiFile) { |
| 33 | + |
| 34 | + Collection<Pair<String, String>> pairs = null; |
| 35 | + |
| 36 | + if(psiFile instanceof XmlFile) { |
| 37 | + pairs = getClassRepositoryPair((XmlFile) psiFile); |
| 38 | + } else if(psiFile instanceof YAMLFile) { |
| 39 | + pairs = getClassRepositoryPair((YAMLFile) psiFile); |
| 40 | + } |
| 41 | + |
| 42 | + return pairs; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Extract class and repository from xml meta files |
| 47 | + * We support orm and all odm syntax here |
| 48 | + */ |
| 49 | + @Nullable |
| 50 | + private static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull XmlFile xmlFile) { |
| 51 | + |
| 52 | + XmlTag rootTag = xmlFile.getRootTag(); |
| 53 | + if(rootTag == null || !rootTag.getName().toLowerCase().startsWith("doctrine")) { |
| 54 | + return null; |
| 55 | + } |
| 56 | + |
| 57 | + Collection<Pair<String, String>> pairs = new ArrayList<Pair<String, String>>(); |
| 58 | + |
| 59 | + for (XmlTag xmlTag : (XmlTag[]) ArrayUtils.addAll(rootTag.findSubTags("document"), rootTag.findSubTags("entity"))) { |
| 60 | + |
| 61 | + XmlAttribute attr = xmlTag.getAttribute("name"); |
| 62 | + if(attr == null) { |
| 63 | + continue; |
| 64 | + } |
| 65 | + |
| 66 | + String value = attr.getValue(); |
| 67 | + if(StringUtils.isBlank(value)) { |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + // extract repository-class; allow nullable |
| 72 | + String repositoryClass = null; |
| 73 | + XmlAttribute repositoryClassAttribute = xmlTag.getAttribute("repository-class"); |
| 74 | + if(repositoryClassAttribute != null) { |
| 75 | + String repositoryClassAttributeValue = repositoryClassAttribute.getValue(); |
| 76 | + if(StringUtils.isNotBlank(repositoryClassAttributeValue)) { |
| 77 | + repositoryClass = repositoryClassAttributeValue; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + pairs.add(Pair.create(value, repositoryClass)); |
| 82 | + } |
| 83 | + |
| 84 | + if(pairs.size() == 0) { |
| 85 | + return null; |
| 86 | + } |
| 87 | + |
| 88 | + return pairs; |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Extract class and repository from all yaml files |
| 93 | + * We need to filter on some condition, so we dont index files which not holds meta for doctrine |
| 94 | + * |
| 95 | + * Note: index context method, so file validity in each statement |
| 96 | + */ |
| 97 | + @Nullable |
| 98 | + private static Collection<Pair<String, String>> getClassRepositoryPair(@NotNull YAMLFile yamlFile) { |
| 99 | + |
| 100 | + // we are indexing all yaml files for prefilter on path, |
| 101 | + // if false if check on parse |
| 102 | + String name = yamlFile.getName().toLowerCase(); |
| 103 | + boolean iAmMetadataFile = name.contains(".odm.") || name.contains(".orm.") || name.contains(".mongodb.") || name.contains(".couchdb."); |
| 104 | + |
| 105 | + YAMLDocument yamlDocument = PsiTreeUtil.getChildOfType(yamlFile, YAMLDocument.class); |
| 106 | + if(yamlDocument == null) { |
| 107 | + return null; |
| 108 | + } |
| 109 | + |
| 110 | + YAMLKeyValue[] yamlKeys = PsiTreeUtil.getChildrenOfType(yamlDocument, YAMLKeyValue.class); |
| 111 | + if(yamlKeys == null) { |
| 112 | + return null; |
| 113 | + } |
| 114 | + |
| 115 | + Collection<Pair<String, String>> pairs = new ArrayList<Pair<String, String>>(); |
| 116 | + |
| 117 | + for (YAMLKeyValue yamlKey : yamlKeys) { |
| 118 | + String keyText = yamlKey.getKeyText(); |
| 119 | + if(StringUtils.isBlank(keyText)) { |
| 120 | + continue; |
| 121 | + } |
| 122 | + |
| 123 | + String repositoryClass = YamlHelper.getYamlKeyValueAsString(yamlKey, "repositoryClass"); |
| 124 | + |
| 125 | + // fine repositoryClass exists a valid metadata file |
| 126 | + if(!iAmMetadataFile && repositoryClass != null) { |
| 127 | + iAmMetadataFile = true; |
| 128 | + } |
| 129 | + |
| 130 | + // currently not valid metadata file find valid keys |
| 131 | + // else we are not allowed to store values |
| 132 | + if(!iAmMetadataFile) { |
| 133 | + Set<String> keySet = YamlHelper.getKeySet(yamlKey); |
| 134 | + if(keySet == null) { |
| 135 | + continue; |
| 136 | + } |
| 137 | + |
| 138 | + if(!(keySet.contains("fields") || keySet.contains("id") || keySet.contains("collection") || keySet.contains("db") || keySet.contains("indexes"))) { |
| 139 | + continue; |
| 140 | + } |
| 141 | + |
| 142 | + iAmMetadataFile = true; |
| 143 | + } |
| 144 | + |
| 145 | + pairs.add(Pair.create(keyText, repositoryClass)); |
| 146 | + } |
| 147 | + |
| 148 | + if(pairs.size() == 0) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + |
| 152 | + return pairs; |
| 153 | + } |
| 154 | + |
| 155 | +} |
0 commit comments