1
- /**
1
+ /*
2
2
* Copyright © Magento, Inc. All rights reserved.
3
3
* See COPYING.txt for license details.
4
4
*/
5
+
5
6
package com .magento .idea .magento2plugin .magento .packages ;
6
7
7
8
import com .intellij .json .psi .JsonFile ;
8
9
import com .intellij .json .psi .JsonObject ;
10
+ import com .intellij .openapi .components .Service ;
9
11
import com .intellij .openapi .project .DumbService ;
10
12
import com .intellij .openapi .project .Project ;
11
13
import com .intellij .openapi .vfs .VirtualFile ;
18
20
import com .intellij .psi .xml .XmlTag ;
19
21
import com .intellij .util .indexing .FileBasedIndex ;
20
22
import com .magento .idea .magento2plugin .stubs .indexes .ModulePackageIndex ;
21
- import org .jetbrains .annotations .NotNull ;
22
- import org .jetbrains .annotations .Nullable ;
23
23
import java .util .ArrayList ;
24
24
import java .util .Collection ;
25
25
import java .util .HashMap ;
26
26
import java .util .Map ;
27
+ import org .jetbrains .annotations .NotNull ;
28
+ import org .jetbrains .annotations .Nullable ;
29
+
30
+ @ Service
31
+ public final class MagentoComponentManager {
27
32
28
- public class MagentoComponentManager {
29
33
private Map <String , MagentoComponent > components = new HashMap <>();
30
34
private long cacheStartTime ;
31
- private static final int CACHE_LIFE_TIME = 20000 ;
32
- private static MagentoComponentManager magentoComponentManager ;
33
- private Project project ;
35
+ private static final int CACHE_LIFE_TIME = 20000 ;//NOPMD
36
+ private final Project project ;
34
37
35
- private MagentoComponentManager (Project project ){
38
+ public MagentoComponentManager (final Project project ) {
36
39
this .project = project ;
37
40
}
38
41
39
- public static MagentoComponentManager getInstance (@ NotNull Project project ) {
40
- if (magentoComponentManager == null ) {
41
- magentoComponentManager = new MagentoComponentManager (project );
42
- }
43
- return magentoComponentManager ;
42
+ public static MagentoComponentManager getInstance (final @ NotNull Project project ) {
43
+ return project .getService (MagentoComponentManager .class );
44
44
}
45
45
46
46
public Collection <MagentoComponent > getAllComponents () {
47
47
return getComponents ().values ();
48
48
}
49
49
50
+ /**
51
+ * Get all components of the specified type.
52
+ *
53
+ * @param type Class
54
+ *
55
+ * @return Collection
56
+ */
50
57
@ SuppressWarnings ("unchecked" )
51
- public <T extends MagentoComponent > Collection <T > getAllComponentsOfType (@ NotNull Class <T > type ) {
52
- Collection <T > result = new ArrayList <>();
53
- Map <String , MagentoComponent > components = getComponents ();
54
- for (String key : components .keySet ()) {
58
+ public <T extends MagentoComponent > Collection <T > getAllComponentsOfType (
59
+ final @ NotNull Class <T > type
60
+ ) {
61
+ final Collection <T > result = new ArrayList <>();
62
+ final Map <String , MagentoComponent > components = getComponents ();
63
+
64
+ for (final String key : components .keySet ()) {
55
65
if (type .isInstance (components .get (key ))) {
56
66
result .add ((T )components .get (key ));
57
67
}
@@ -60,8 +70,9 @@ public <T extends MagentoComponent> Collection<T> getAllComponentsOfType(@NotNul
60
70
return result ;
61
71
}
62
72
63
- synchronized private Map <String , MagentoComponent > getComponents () {
64
- if (DumbService .getInstance (project ).isDumb () || project .isDisposed ()) {
73
+ @ SuppressWarnings ("PMD.AvoidSynchronizedAtMethodLevel" )
74
+ private synchronized Map <String , MagentoComponent > getComponents () {
75
+ if (project .isDisposed () || DumbService .getInstance (project ).isDumb ()) {
65
76
return new HashMap <>();
66
77
}
67
78
@@ -74,9 +85,16 @@ synchronized private Map<String, MagentoComponent> getComponents() {
74
85
return components ;
75
86
}
76
87
88
+ /**
89
+ * Get component for the specified file.
90
+ *
91
+ * @param psiFile PsiFile
92
+ *
93
+ * @return MagentoComponent
94
+ */
77
95
@ Nullable
78
- public MagentoComponent getComponentForFile (@ NotNull PsiFile psiFile ) {
79
- for (MagentoComponent magentoComponent : this .getAllComponents ()) {
96
+ public MagentoComponent getComponentForFile (final @ NotNull PsiFile psiFile ) {
97
+ for (final MagentoComponent magentoComponent : this .getAllComponents ()) {
80
98
if (magentoComponent .isFileInContext (psiFile )) {
81
99
return magentoComponent ;
82
100
}
@@ -85,10 +103,21 @@ public MagentoComponent getComponentForFile(@NotNull PsiFile psiFile) {
85
103
return null ;
86
104
}
87
105
106
+ /**
107
+ * Get component of type for the specified file.
108
+ *
109
+ * @param psiFile PsiFile
110
+ * @param type Class
111
+ *
112
+ * @return T
113
+ */
88
114
@ Nullable
89
115
@ SuppressWarnings ("unchecked" )
90
- public <T extends MagentoComponent > T getComponentOfTypeForFile (@ NotNull PsiFile psiFile , @ NotNull Class <T > type ) {
91
- for (MagentoComponent magentoComponent : this .getAllComponents ()) {
116
+ public <T extends MagentoComponent > T getComponentOfTypeForFile (
117
+ final @ NotNull PsiFile psiFile ,
118
+ final @ NotNull Class <T > type
119
+ ) {
120
+ for (final MagentoComponent magentoComponent : this .getAllComponents ()) {
92
121
if (type .isInstance (magentoComponent ) && magentoComponent .isFileInContext (psiFile )) {
93
122
return (T )magentoComponent ;
94
123
}
@@ -97,72 +126,97 @@ public <T extends MagentoComponent> T getComponentOfTypeForFile(@NotNull PsiFile
97
126
return null ;
98
127
}
99
128
100
- synchronized public void flushModules () {
129
+ @ SuppressWarnings ("PMD.AvoidSynchronizedAtMethodLevel" )
130
+ public synchronized void flushModules () {
101
131
components = new HashMap <>();
102
132
}
103
133
134
+ @ SuppressWarnings ({"PMD.AvoidInstantiatingObjectsInLoops" , "PMD.AvoidDeeplyNestedIfStmts" })
104
135
private void loadModules () {
105
- Collection <String > packages = FileBasedIndex .getInstance ().getAllKeys (ModulePackageIndex .KEY , this .project );
106
- PsiManager psiManager = PsiManager .getInstance (this .project );
107
- for (String packageName : packages ) {
136
+ final Collection <String > packages = FileBasedIndex
137
+ .getInstance ()
138
+ .getAllKeys (ModulePackageIndex .KEY , this .project );
139
+ final PsiManager psiManager = PsiManager .getInstance (this .project );
140
+
141
+ for (final String packageName : packages ) {
108
142
if (components .containsKey (packageName )) {
109
143
continue ;
110
144
}
111
145
112
- Collection <VirtualFile > containingFiles = FileBasedIndex .getInstance ()
113
- .getContainingFiles (ModulePackageIndex .KEY , packageName , GlobalSearchScope .allScope (this .project ));
146
+ final Collection <VirtualFile > containingFiles = FileBasedIndex
147
+ .getInstance ()
148
+ .getContainingFiles (
149
+ ModulePackageIndex .KEY ,
150
+ packageName ,
151
+ GlobalSearchScope .allScope (this .project )
152
+ );
114
153
115
- if (containingFiles .size () > 0 ) {
116
- VirtualFile configurationFile = containingFiles .iterator ().next ();
154
+ if (!containingFiles .isEmpty ()) {
155
+ final VirtualFile configurationFile = containingFiles .iterator ().next ();
156
+ final PsiFile psiFile = psiManager .findFile (configurationFile );
117
157
118
- PsiFile psiFile = psiManager . findFile ( configurationFile );
119
- if ( psiFile != null && psiFile instanceof JsonFile ) {
120
- JsonObject jsonObject = PsiTreeUtil .getChildOfType ((JsonFile ) psiFile , JsonObject .class );
158
+ if ( psiFile instanceof JsonFile ) {
159
+ final JsonObject jsonObject = PsiTreeUtil
160
+ .getChildOfType ((JsonFile ) psiFile , JsonObject .class );
121
161
if (jsonObject == null ) {
122
162
continue ;
123
163
}
124
164
125
165
MagentoComponent magentoComponent ;
126
- ComposerPackageModel composerPackageModel = new ComposerPackageModelImpl (jsonObject );
166
+ final ComposerPackageModel composerPackageModel = new ComposerPackageModelImpl (
167
+ jsonObject
168
+ );
169
+
127
170
if ("magento2-module" .equals (composerPackageModel .getType ())) {
128
- magentoComponent = new MagentoModuleImpl (new ComposerPackageModelImpl (jsonObject ), psiFile .getContainingDirectory ());
171
+ magentoComponent = new MagentoModuleImpl (
172
+ new ComposerPackageModelImpl (jsonObject ),
173
+ psiFile .getContainingDirectory ()
174
+ );
129
175
} else {
130
- magentoComponent = new MagentoComponentImp (new ComposerPackageModelImpl (jsonObject ), psiFile .getContainingDirectory ());
176
+ magentoComponent = new MagentoComponentImp (
177
+ new ComposerPackageModelImpl (jsonObject ),
178
+ psiFile .getContainingDirectory ()
179
+ );
131
180
}
132
181
133
182
components .put (
134
- packageName ,
135
- magentoComponent
183
+ packageName ,
184
+ magentoComponent
136
185
);
137
186
}
138
187
}
139
188
}
140
189
}
141
190
}
142
191
192
+ @ SuppressWarnings ("checkstyle:OneTopLevelClass" )
143
193
class MagentoModuleImpl extends MagentoComponentImp implements MagentoModule {
144
194
private static final String DEFAULT_MODULE_NAME = "Undefined module" ;
145
195
private static final String CONFIGURATION_PATH = "etc" ;
146
196
private String moduleName ;
147
197
148
- public MagentoModuleImpl (@ NotNull ComposerPackageModel composerPackageModel , @ NotNull PsiDirectory directory ) {
198
+ public MagentoModuleImpl (
199
+ final @ NotNull ComposerPackageModel composerPackageModel ,
200
+ final @ NotNull PsiDirectory directory
201
+ ) {
149
202
super (composerPackageModel , directory );
150
203
}
151
204
205
+ @ SuppressWarnings ({"PMD.AvoidDeeplyNestedIfStmts" })
152
206
@ Override
153
207
public String getMagentoName () {
154
208
if (moduleName != null ) {
155
209
return moduleName ;
156
210
}
157
211
158
- PsiDirectory configurationDir = directory .findSubdirectory (CONFIGURATION_PATH );
212
+ final PsiDirectory configurationDir = directory .findSubdirectory (CONFIGURATION_PATH );
159
213
if (configurationDir != null ) {
160
- PsiFile configurationFile = configurationDir .findFile ("module.xml" );
214
+ final PsiFile configurationFile = configurationDir .findFile ("module.xml" );
161
215
162
- if (configurationFile != null && configurationFile instanceof XmlFile ) {
163
- XmlTag rootTag = ((XmlFile ) configurationFile ).getRootTag ();
216
+ if (configurationFile instanceof XmlFile ) {
217
+ final XmlTag rootTag = ((XmlFile ) configurationFile ).getRootTag ();
164
218
if (rootTag != null ) {
165
- XmlTag module = rootTag .findFirstSubTag ("module" );
219
+ final XmlTag module = rootTag .findFirstSubTag ("module" );
166
220
if (module != null && module .getAttributeValue ("name" ) != null ) {
167
221
moduleName = module .getAttributeValue ("name" );
168
222
return moduleName ;
0 commit comments