7
7
import com .jetbrains .php .PhpIndex ;
8
8
import com .jetbrains .php .lang .documentation .phpdoc .psi .PhpDocComment ;
9
9
import com .jetbrains .php .lang .psi .elements .PhpClass ;
10
+ import fr .adrienbrault .idea .symfony2plugin .doctrine .component .DocumentNamespacesParser ;
10
11
import fr .adrienbrault .idea .symfony2plugin .doctrine .component .EntityNamesServiceParser ;
12
+ import fr .adrienbrault .idea .symfony2plugin .doctrine .dict .DoctrineTypes ;
11
13
import fr .adrienbrault .idea .symfony2plugin .util .PhpElementsUtil ;
12
14
import fr .adrienbrault .idea .symfony2plugin .util .SymfonyBundleUtil ;
13
15
import fr .adrienbrault .idea .symfony2plugin .util .dict .SymfonyBundle ;
14
16
import fr .adrienbrault .idea .symfony2plugin .util .service .ServiceXmlParserFactory ;
15
17
import org .jetbrains .annotations .Nullable ;
16
18
17
- import java .util .Collection ;
18
- import java .util .Map ;
19
+ import java .util .*;
19
20
import java .util .regex .Matcher ;
20
21
import java .util .regex .Pattern ;
21
22
@@ -36,11 +37,11 @@ public static PhpClass getEntityRepositoryClass(Project project, String shortcut
36
37
PhpDocComment docAnnotation = phpClass .getDocComment ();
37
38
if (docAnnotation != null ) {
38
39
39
- // search for @ORM\Entity(repositoryClass="Foo\Bar\RegisterRepository")
40
+ // search for repositoryClass="Foo\Bar\RegisterRepository"
41
+ // @MongoDB\Document; @ORM\Entity
40
42
String docAnnotationText = docAnnotation .getText ();
41
43
Matcher matcher = Pattern .compile ("repositoryClass=[\" |'](.*)[\" |']" ).matcher (docAnnotationText );
42
44
if (matcher .find ()) {
43
- //System.out.println("Annotation: " + shortcutName);
44
45
return PhpElementsUtil .getClass (PhpIndex .getInstance (project ), matcher .group (1 ));
45
46
}
46
47
}
@@ -50,29 +51,9 @@ public static PhpClass getEntityRepositoryClass(Project project, String shortcut
50
51
String classFqnName = phpClass .getPresentableFQN ();
51
52
52
53
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 ;
76
57
}
77
58
}
78
59
@@ -83,14 +64,60 @@ public static PhpClass getEntityRepositoryClass(Project project, String shortcut
83
64
return resolveShortcutName (project , shortcutName + "Repository" );
84
65
}
85
66
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
+
86
111
/**
87
112
*
88
113
* @param project PHPStorm projects
89
114
* @param shortcutName name as MyBundle\Entity\Model or MyBundle:Model
90
115
* @return null|PhpClass
91
116
*/
92
117
@ 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 );
94
121
95
122
if (shortcutName == null ) {
96
123
return null ;
@@ -103,7 +130,15 @@ public static PhpClass resolveShortcutName(Project project, String shortcutName)
103
130
// MyBundle:Folder\Model -> MyBundle\Entity\Folder\Model
104
131
if (shortcutName .contains (":" )) {
105
132
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
+ }
107
142
108
143
int firstDirectorySeparatorIndex = shortcutName .indexOf (":" );
109
144
0 commit comments