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 .actions .generation .generator ;
6
7
7
8
import com .intellij .openapi .command .WriteCommandAction ;
19
20
import com .magento .idea .magento2plugin .actions .generation .generator .util .XmlFilePositionUtil ;
20
21
import com .magento .idea .magento2plugin .magento .files .ModuleDiXml ;
21
22
import com .magento .idea .magento2plugin .util .xml .XmlPsiTreeUtil ;
22
- import org .jetbrains .annotations .NotNull ;
23
23
import java .io .IOException ;
24
- import java .util .*;
24
+ import java .util .Collection ;
25
+ import java .util .Properties ;
26
+ import org .jetbrains .annotations .NotNull ;
25
27
26
28
public class PluginDiXmlGenerator extends FileGenerator {
27
29
private final GetCodeTemplate getCodeTemplate ;
28
30
private final FindOrCreateDiXml findOrCreateDiXml ;
29
31
private final XmlFilePositionUtil positionUtil ;
30
- private PluginDiXmlData pluginFileData ;
31
- private Project project ;
32
+ private final PluginDiXmlData pluginFileData ;
33
+ private final Project project ;
32
34
private boolean isTypeDeclared ;
33
35
34
- public PluginDiXmlGenerator (@ NotNull PluginDiXmlData pluginFileData , Project project ) {
36
+ /**
37
+ * Constructor.
38
+ *
39
+ * @param pluginFileData
40
+ * @param project
41
+ */
42
+ public PluginDiXmlGenerator (final @ NotNull PluginDiXmlData pluginFileData , final Project project ) {
35
43
super (project );
36
44
this .pluginFileData = pluginFileData ;
37
45
this .project = project ;
38
46
this .getCodeTemplate = GetCodeTemplate .getInstance (project );
39
- this .findOrCreateDiXml = FindOrCreateDiXml . getInstance (project );
47
+ this .findOrCreateDiXml = new FindOrCreateDiXml (project );
40
48
this .positionUtil = XmlFilePositionUtil .getInstance ();
41
49
}
42
50
43
- public PsiFile generate (String actionName )
44
- {
45
- PsiFile diXmlFile = findOrCreateDiXml .execute (actionName , pluginFileData .getPluginModule (), pluginFileData .getArea ());
46
- XmlAttributeValue typeAttributeValue = getTypeAttributeValue ((XmlFile ) diXmlFile );
51
+ /**
52
+ * Creates a module di.xml file.
53
+ *
54
+ * @param actionName String
55
+ * @return PsiFile
56
+ */
57
+ @ Override
58
+ public PsiFile generate (final String actionName ) {
59
+ final PsiFile diXmlFile = findOrCreateDiXml .execute (actionName , pluginFileData .getPluginModule (), pluginFileData .getArea ());
60
+ final XmlAttributeValue typeAttributeValue = getTypeAttributeValue ((XmlFile ) diXmlFile );
47
61
boolean isPluginDeclared = false ;
48
62
this .isTypeDeclared = false ;
49
63
if (typeAttributeValue != null ) {
@@ -54,22 +68,21 @@ public PsiFile generate(String actionName)
54
68
return null ;
55
69
}
56
70
WriteCommandAction .runWriteCommandAction (project , () -> {
57
- StringBuffer textBuf = new StringBuffer ();
71
+ final StringBuffer textBuf = new StringBuffer ();
58
72
try {
59
73
textBuf .append (getCodeTemplate .execute (ModuleDiXml .TEMPLATE_PLUGIN , getAttributes ()));
60
74
} catch (IOException e ) {
61
- e .printStackTrace ();
62
75
return ;
63
76
}
64
77
65
- int insertPos = isTypeDeclared
78
+ final int insertPos = isTypeDeclared
66
79
? positionUtil .getEndPositionOfTag (PsiTreeUtil .getParentOfType (typeAttributeValue , XmlTag .class ))
67
80
: positionUtil .getRootInsertPosition ((XmlFile ) diXmlFile );
68
81
if (textBuf .length () > 0 && insertPos >= 0 ) {
69
- PsiDocumentManager psiDocumentManager = PsiDocumentManager .getInstance (project );
70
- Document document = psiDocumentManager .getDocument (diXmlFile );
82
+ final PsiDocumentManager psiDocumentManager = PsiDocumentManager .getInstance (project );
83
+ final Document document = psiDocumentManager .getDocument (diXmlFile );
71
84
document .insertString (insertPos , textBuf );
72
- int endPos = insertPos + textBuf .length () + 1 ;
85
+ final int endPos = insertPos + textBuf .length () + 1 ;
73
86
CodeStyleManager .getInstance (project ).reformatText (diXmlFile , insertPos , endPos );
74
87
psiDocumentManager .commitDocument (document );
75
88
}
@@ -78,22 +91,22 @@ public PsiFile generate(String actionName)
78
91
return diXmlFile ;
79
92
}
80
93
81
- private boolean isPluginDeclared (XmlAttributeValue typeAttributeValue ) {
82
- XmlTag xmlTag = PsiTreeUtil .getParentOfType (typeAttributeValue , XmlTag .class );
83
- XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (xmlTag , XmlTag .class );
94
+ private boolean isPluginDeclared (final XmlAttributeValue typeAttributeValue ) {
95
+ final XmlTag xmlTag = PsiTreeUtil .getParentOfType (typeAttributeValue , XmlTag .class );
96
+ final XmlTag [] xmlTags = PsiTreeUtil .getChildrenOfType (xmlTag , XmlTag .class );
84
97
if (xmlTags == null ) {
85
98
return false ;
86
99
}
87
- for (XmlTag child : xmlTags ) {
100
+ for (final XmlTag child : xmlTags ) {
88
101
if (!child .getName ().equals (ModuleDiXml .PLUGIN_TAG_NAME )) {
89
102
continue ;
90
103
}
91
- XmlAttribute [] xmlAttributes = PsiTreeUtil .getChildrenOfType (child , XmlAttribute .class );
92
- for (XmlAttribute xmlAttribute : xmlAttributes ) {
104
+ final XmlAttribute [] xmlAttributes = PsiTreeUtil .getChildrenOfType (child , XmlAttribute .class );
105
+ for (final XmlAttribute xmlAttribute : xmlAttributes ) {
93
106
if (!xmlAttribute .getName ().equals (ModuleDiXml .PLUGIN_TYPE_ATTRIBUTE )) {
94
107
continue ;
95
108
}
96
- String declaredClass = PhpLangUtil .toPresentableFQN (xmlAttribute .getValue ());
109
+ final String declaredClass = PhpLangUtil .toPresentableFQN (xmlAttribute .getValue ());
97
110
if (declaredClass .equals (pluginFileData .getPluginFqn ())) {
98
111
return true ;
99
112
}
@@ -103,20 +116,20 @@ private boolean isPluginDeclared(XmlAttributeValue typeAttributeValue) {
103
116
return false ;
104
117
}
105
118
106
- private XmlAttributeValue getTypeAttributeValue (XmlFile diXml ) {
107
- Collection <XmlAttributeValue > pluginTypes = XmlPsiTreeUtil .findAttributeValueElements (diXml , ModuleDiXml .PLUGIN_TYPE_TAG , ModuleDiXml .PLUGIN_TYPE_ATTR_NAME );
108
- String pluginClassFqn = pluginFileData .getTargetClass ().getPresentableFQN ();
109
- for (XmlAttributeValue pluginType : pluginTypes ) {
110
- if (! PhpLangUtil .toPresentableFQN (pluginType .getValue ()).equals (pluginClassFqn )) {
111
- continue ;
119
+ private XmlAttributeValue getTypeAttributeValue (final XmlFile diXml ) {
120
+ final Collection <XmlAttributeValue > pluginTypes = XmlPsiTreeUtil .findAttributeValueElements (diXml , ModuleDiXml .PLUGIN_TYPE_TAG , ModuleDiXml .PLUGIN_TYPE_ATTR_NAME );
121
+ final String pluginClassFqn = pluginFileData .getTargetClass ().getPresentableFQN ();
122
+ for (final XmlAttributeValue pluginType : pluginTypes ) {
123
+ if (PhpLangUtil .toPresentableFQN (pluginType .getValue ()).equals (pluginClassFqn )) {
124
+ return pluginType ;
112
125
}
113
- return pluginType ;
114
126
}
115
127
116
128
return null ;
117
129
}
118
130
119
- protected void fillAttributes (Properties attributes ) {
131
+ @ Override
132
+ protected void fillAttributes (final Properties attributes ) {
120
133
if (!isTypeDeclared ) {
121
134
attributes .setProperty ("TYPE" , pluginFileData .getTargetClass ().getPresentableFQN ());
122
135
}
0 commit comments