Skip to content

Commit 957be6d

Browse files
committed
add support for mongodb repositories and documents #205
1 parent 2df520d commit 957be6d

File tree

3 files changed

+85
-34
lines changed

3 files changed

+85
-34
lines changed

src/fr/adrienbrault/idea/symfony2plugin/doctrine/EntityHelper.java

+64-29
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@
77
import com.jetbrains.php.PhpIndex;
88
import com.jetbrains.php.lang.documentation.phpdoc.psi.PhpDocComment;
99
import com.jetbrains.php.lang.psi.elements.PhpClass;
10+
import fr.adrienbrault.idea.symfony2plugin.doctrine.component.DocumentNamespacesParser;
1011
import fr.adrienbrault.idea.symfony2plugin.doctrine.component.EntityNamesServiceParser;
12+
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineTypes;
1113
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1214
import fr.adrienbrault.idea.symfony2plugin.util.SymfonyBundleUtil;
1315
import fr.adrienbrault.idea.symfony2plugin.util.dict.SymfonyBundle;
1416
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
1517
import org.jetbrains.annotations.Nullable;
1618

17-
import java.util.Collection;
18-
import java.util.Map;
19+
import java.util.*;
1920
import java.util.regex.Matcher;
2021
import java.util.regex.Pattern;
2122

@@ -36,11 +37,11 @@ public static PhpClass getEntityRepositoryClass(Project project, String shortcut
3637
PhpDocComment docAnnotation = phpClass.getDocComment();
3738
if(docAnnotation != null) {
3839

39-
// search for @ORM\Entity(repositoryClass="Foo\Bar\RegisterRepository")
40+
// search for repositoryClass="Foo\Bar\RegisterRepository"
41+
// @MongoDB\Document; @ORM\Entity
4042
String docAnnotationText = docAnnotation.getText();
4143
Matcher matcher = Pattern.compile("repositoryClass=[\"|'](.*)[\"|']").matcher(docAnnotationText);
4244
if (matcher.find()) {
43-
//System.out.println("Annotation: " + shortcutName);
4445
return PhpElementsUtil.getClass(PhpIndex.getInstance(project), matcher.group(1));
4546
}
4647
}
@@ -50,29 +51,9 @@ public static PhpClass getEntityRepositoryClass(Project project, String shortcut
5051
String classFqnName = phpClass.getPresentableFQN();
5152

5253
if(classFqnName != null) {
53-
String entityName = classFqnName.substring(symfonyBundle.getNamespaceName().length() - 1);
54-
if(entityName.startsWith("Entity\\")) {
55-
entityName = entityName.substring("Entity\\".length());
56-
}
57-
58-
// entities in sub folder: 'Foo\Bar' -> 'Foo.Bar.orm.yml'
59-
String entityFile = "Resources/config/doctrine/" + entityName.replace("\\", ".") + ".orm.yml";
60-
VirtualFile virtualFile = symfonyBundle.getRelative(entityFile);
61-
if(virtualFile != null) {
62-
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
63-
if(psiFile != null) {
64-
65-
// search for "repositoryClass: Foo\Bar\RegisterRepository" also provide quoted values
66-
Matcher matcher = Pattern.compile("[\\s]*repositoryClass:[\\s]*[\"|']*(.*)[\"|']*").matcher(psiFile.getText());
67-
if (matcher.find()) {
68-
//System.out.println("Yml: " + shortcutName);
69-
return PhpElementsUtil.getClass(PhpIndex.getInstance(project), matcher.group(1));
70-
}
71-
72-
// we found entity config so no other check needed
73-
return null;
74-
}
75-
54+
PhpClass repositoryClass = getEntityRepositoryClass(project, symfonyBundle, classFqnName);
55+
if(repositoryClass != null) {
56+
return repositoryClass;
7657
}
7758
}
7859

@@ -83,14 +64,60 @@ public static PhpClass getEntityRepositoryClass(Project project, String shortcut
8364
return resolveShortcutName(project, shortcutName + "Repository");
8465
}
8566

67+
@Nullable
68+
private static PhpClass getEntityRepositoryClass(Project project, SymfonyBundle symfonyBundle, String classFqnName) {
69+
70+
// some default bundle search path
71+
// Bundle/Resources/config/doctrine/Product.orm.yml
72+
// Bundle/Resources/config/doctrine/Product.mongodb.yml
73+
List<String[]> managerConfigs = new ArrayList<String[]>();
74+
managerConfigs.add(new String[] { "Entity", "orm"});
75+
managerConfigs.add(new String[] { "Document", "mongodb"});
76+
77+
for(String[] managerConfig: managerConfigs) {
78+
String entityName = classFqnName.substring(symfonyBundle.getNamespaceName().length() - 1);
79+
if(entityName.startsWith(managerConfig[0] + "\\")) {
80+
entityName = entityName.substring((managerConfig[0] + "\\").length());
81+
}
82+
83+
// entities in sub folder: 'Foo\Bar' -> 'Foo.Bar.orm.yml'
84+
String entityFile = "Resources/config/doctrine/" + entityName.replace("\\", ".") + String.format(".%s.yml", managerConfig[1]);
85+
VirtualFile virtualFile = symfonyBundle.getRelative(entityFile);
86+
if(virtualFile != null) {
87+
PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
88+
if(psiFile != null) {
89+
90+
// search for "repositoryClass: Foo\Bar\RegisterRepository" also provide quoted values
91+
Matcher matcher = Pattern.compile("[\\s]*repositoryClass:[\\s]*[\"|']*(.*)[\"|']*").matcher(psiFile.getText());
92+
if (matcher.find()) {
93+
return PhpElementsUtil.getClass(PhpIndex.getInstance(project), matcher.group(1));
94+
}
95+
96+
// we found entity config so no other check needed
97+
return null;
98+
}
99+
100+
}
101+
}
102+
103+
return null;
104+
}
105+
106+
@Nullable
107+
public static PhpClass resolveShortcutName(Project project, String shortcutName) {
108+
return resolveShortcutName(project, shortcutName, DoctrineTypes.Manager.ODM, DoctrineTypes.Manager.ODM);
109+
}
110+
86111
/**
87112
*
88113
* @param project PHPStorm projects
89114
* @param shortcutName name as MyBundle\Entity\Model or MyBundle:Model
90115
* @return null|PhpClass
91116
*/
92117
@Nullable
93-
public static PhpClass resolveShortcutName(Project project, String shortcutName) {
118+
public static PhpClass resolveShortcutName(Project project, String shortcutName, DoctrineTypes.Manager... managers) {
119+
120+
List<DoctrineTypes.Manager> managerList = Arrays.asList(managers);
94121

95122
if(shortcutName == null) {
96123
return null;
@@ -103,7 +130,15 @@ public static PhpClass resolveShortcutName(Project project, String shortcutName)
103130
// MyBundle:Folder\Model -> MyBundle\Entity\Folder\Model
104131
if (shortcutName.contains(":")) {
105132

106-
Map<String, String> em = ServiceXmlParserFactory.getInstance(project, EntityNamesServiceParser.class).getEntityNameMap();
133+
Map<String, String> em = new HashMap<String, String>();
134+
135+
if(managerList.contains(DoctrineTypes.Manager.ORM)) {
136+
em.putAll(ServiceXmlParserFactory.getInstance(project, EntityNamesServiceParser.class).getEntityNameMap());
137+
}
138+
139+
if(managerList.contains(DoctrineTypes.Manager.ODM)) {
140+
em.putAll(ServiceXmlParserFactory.getInstance(project, DocumentNamespacesParser.class).getNamespaceMap());
141+
}
107142

108143
int firstDirectorySeparatorIndex = shortcutName.indexOf(":");
109144

src/fr/adrienbrault/idea/symfony2plugin/doctrine/EntityReference.java

+16-5
Original file line numberDiff line numberDiff line change
@@ -10,24 +10,23 @@
1010
import com.jetbrains.php.lang.psi.elements.PhpNamespace;
1111
import com.jetbrains.php.lang.psi.elements.StringLiteralExpression;
1212
import fr.adrienbrault.idea.symfony2plugin.Symfony2InterfacesUtil;
13+
import fr.adrienbrault.idea.symfony2plugin.doctrine.component.DocumentNamespacesParser;
1314
import fr.adrienbrault.idea.symfony2plugin.doctrine.component.EntityNamesServiceParser;
1415
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineEntityLookupElement;
1516
import fr.adrienbrault.idea.symfony2plugin.doctrine.dict.DoctrineTypes;
1617
import fr.adrienbrault.idea.symfony2plugin.util.PhpElementsUtil;
1718
import fr.adrienbrault.idea.symfony2plugin.util.service.ServiceXmlParserFactory;
1819
import org.jetbrains.annotations.NotNull;
1920

20-
import java.util.ArrayList;
21-
import java.util.Collection;
22-
import java.util.List;
23-
import java.util.Map;
21+
import java.util.*;
2422

2523
/**
2624
* @author Daniel Espendiller <daniel@espendiller.net>
2725
*/
2826
public class EntityReference extends PsiPolyVariantReferenceBase<PsiElement> {
2927
private String entityName;
3028
private boolean useClassNameAsLookupString = false;
29+
private List<DoctrineTypes.Manager> doctrineManagers;
3130

3231
public EntityReference(@NotNull StringLiteralExpression element, boolean useClassNameAsLookupString) {
3332
this(element);
@@ -37,6 +36,10 @@ public EntityReference(@NotNull StringLiteralExpression element, boolean useClas
3736
public EntityReference(@NotNull StringLiteralExpression element) {
3837
super(element);
3938
entityName = element.getContents();
39+
40+
this.doctrineManagers = new ArrayList<DoctrineTypes.Manager>();
41+
this.doctrineManagers.add(DoctrineTypes.Manager.ORM);
42+
this.doctrineManagers.add(DoctrineTypes.Manager.ODM);
4043
}
4144

4245
@NotNull
@@ -63,7 +66,15 @@ public Object[] getVariants() {
6366

6467
PhpIndex phpIndex = PhpIndex.getInstance(getElement().getProject());
6568

66-
Map<String, String> entityNamespaces = ServiceXmlParserFactory.getInstance(getElement().getProject(), EntityNamesServiceParser.class).getEntityNameMap();
69+
Map<String, String> entityNamespaces = new HashMap<String, String>();
70+
71+
if(this.doctrineManagers.contains(DoctrineTypes.Manager.ORM)) {
72+
entityNamespaces.putAll(ServiceXmlParserFactory.getInstance(getElement().getProject(), EntityNamesServiceParser.class).getEntityNameMap());
73+
}
74+
75+
if(this.doctrineManagers.contains(DoctrineTypes.Manager.ODM)) {
76+
entityNamespaces.putAll(ServiceXmlParserFactory.getInstance(getElement().getProject(), DocumentNamespacesParser.class).getNamespaceMap());
77+
}
6778

6879
List<LookupElement> results = new ArrayList<LookupElement>();
6980

src/fr/adrienbrault/idea/symfony2plugin/doctrine/dict/DoctrineTypes.java

+5
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,9 @@
22

33
public interface DoctrineTypes {
44
public static final String REPOSITORY_INTERFACE = "\\Doctrine\\Common\\Persistence\\ObjectRepository";
5+
6+
public enum Manager {
7+
ORM, ODM
8+
}
9+
510
}

0 commit comments

Comments
 (0)