5
5
6
6
package com .magento .idea .magento2plugin .actions .generation .generator ;
7
7
8
- import com .intellij .openapi .command .WriteCommandAction ;
9
8
import com .intellij .openapi .project .Project ;
10
- import com .intellij .psi .PsiDirectory ;
11
- import com .intellij .psi .PsiFile ;
12
- import com .jetbrains .php .lang .psi .PhpFile ;
13
- import com .jetbrains .php .lang .psi .elements .PhpClass ;
14
9
import com .magento .idea .magento2plugin .actions .generation .data .CollectionData ;
15
- import com .magento .idea .magento2plugin .actions .generation .generator .util .DirectoryGenerator ;
16
- import com .magento .idea .magento2plugin .actions .generation .generator .util .FileFromTemplateGenerator ;
17
10
import com .magento .idea .magento2plugin .actions .generation .generator .util .PhpClassGeneratorUtil ;
18
11
import com .magento .idea .magento2plugin .actions .generation .generator .util .PhpClassTypesBuilder ;
19
- import com .magento .idea .magento2plugin .bundles .CommonBundle ;
20
- import com .magento .idea .magento2plugin .bundles .ValidatorBundle ;
21
- import com .magento .idea .magento2plugin .indexes .ModuleIndex ;
12
+ import com .magento .idea .magento2plugin .magento .files .AbstractPhpFile ;
22
13
import com .magento .idea .magento2plugin .magento .files .CollectionModelFile ;
23
14
import com .magento .idea .magento2plugin .magento .files .ModelFile ;
24
15
import com .magento .idea .magento2plugin .magento .files .ResourceModelFile ;
25
- import com .magento .idea .magento2plugin .util .GetFirstClassOfFile ;
26
- import com .magento .idea .magento2plugin .util .GetPhpClassByFQN ;
27
16
import java .util .Properties ;
28
- import javax .swing .JOptionPane ;
29
17
import org .jetbrains .annotations .NotNull ;
30
18
31
- public class ModuleCollectionGenerator extends FileGenerator {
19
+ public class ModuleCollectionGenerator extends PhpFileGenerator {
32
20
33
21
private final CollectionData data ;
34
- private final Project project ;
35
- private final ValidatorBundle validatorBundle ;
36
- private final CommonBundle commonBundle ;
37
- private final GetFirstClassOfFile getFirstClassOfFile ;
38
- private final DirectoryGenerator directoryGenerator ;
39
- private final FileFromTemplateGenerator fileFromTemplateGenerator ;
40
- private final CollectionModelFile file ;
41
22
42
23
/**
43
24
* Generates new Collection PHP Class based on provided data.
@@ -49,104 +30,32 @@ public ModuleCollectionGenerator(
49
30
final @ NotNull CollectionData data ,
50
31
final @ NotNull Project project
51
32
) {
52
- super (project );
53
- this .project = project ;
54
- this .data = data ;
55
- this .directoryGenerator = DirectoryGenerator .getInstance ();
56
- this .fileFromTemplateGenerator = FileFromTemplateGenerator .getInstance (project );
57
- this .getFirstClassOfFile = GetFirstClassOfFile .getInstance ();
58
- this .validatorBundle = new ValidatorBundle ();
59
- this .commonBundle = new CommonBundle ();
60
- file = new CollectionModelFile (data .getCollectionName ());
33
+ this (data , project , true );
61
34
}
62
35
63
36
/**
64
- * Generates collection model class.
65
- *
66
- * @param actionName Action name
37
+ * Generates new Collection PHP Class based on provided data.
67
38
*
68
- * @return PsiFile
39
+ * @param data CollectionData
40
+ * @param project Project
41
+ * @param checkFileAlreadyExists boolean
69
42
*/
70
- @ Override
71
- public PsiFile generate (final @ NotNull String actionName ) {
72
- final PsiFile [] collectionFiles = new PsiFile [1 ];
73
-
74
- WriteCommandAction .runWriteCommandAction (project , () -> {
75
- PhpClass collection = GetPhpClassByFQN .getInstance (project ).execute (
76
- file .getNamespaceBuilder (
77
- data .getModuleName (),
78
- data .getCollectionDirectory ()
79
- ).getClassFqn ()
80
- );
81
-
82
- if (collection != null ) {
83
- final String errorMessage = this .validatorBundle .message (
84
- "validator.file.alreadyExists" ,
85
- "Collection Class"
86
- );
87
- JOptionPane .showMessageDialog (
88
- null ,
89
- errorMessage ,
90
- commonBundle .message ("common.error" ),
91
- JOptionPane .ERROR_MESSAGE
92
- );
93
-
94
- return ;
95
- }
96
-
97
- collection = createClass (actionName );
98
-
99
- if (collection == null ) {
100
- final String errorMessage = this .validatorBundle .message (
101
- "validator.file.cantBeCreated" ,
102
- "Collection Class"
103
- );
104
- JOptionPane .showMessageDialog (
105
- null ,
106
- errorMessage ,
107
- commonBundle .message ("common.error" ),
108
- JOptionPane .ERROR_MESSAGE
109
- );
110
-
111
- return ;
112
- }
113
-
114
- collectionFiles [0 ] = collection .getContainingFile ();
115
- });
116
-
117
- return collectionFiles [0 ];
43
+ public ModuleCollectionGenerator (
44
+ final @ NotNull CollectionData data ,
45
+ final @ NotNull Project project ,
46
+ final boolean checkFileAlreadyExists
47
+ ) {
48
+ super (project , checkFileAlreadyExists );
49
+ this .data = data ;
118
50
}
119
51
120
- /**
121
- * Create collection class.
122
- *
123
- * @param actionName String
124
- *
125
- * @return PhpClass
126
- */
127
- private PhpClass createClass (final @ NotNull String actionName ) {
128
- final PsiDirectory parentDirectory = ModuleIndex .getInstance (project )
129
- .getModuleDirectoryByModuleName (data .getModuleName ());
130
- final PsiFile modelFile ;
131
-
132
- final PsiDirectory collectionDirectory = directoryGenerator .findOrCreateSubdirectories (
133
- parentDirectory ,
134
- file .getDirectory (data .getCollectionDirectory ())
135
- );
136
- final Properties attributes = getAttributes ();
137
-
138
- modelFile = fileFromTemplateGenerator .generate (
139
- file ,
140
- attributes ,
141
- collectionDirectory ,
142
- actionName
52
+ @ Override
53
+ protected AbstractPhpFile initFile () {
54
+ return new CollectionModelFile (
55
+ data .getModuleName (),
56
+ data .getCollectionName (),
57
+ data .getCollectionDirectory ()
143
58
);
144
-
145
- if (modelFile == null ) {
146
- return null ;
147
- }
148
-
149
- return getFirstClassOfFile .execute ((PhpFile ) modelFile );
150
59
}
151
60
152
61
/**
@@ -159,33 +68,23 @@ protected void fillAttributes(final @NotNull Properties attributes) {
159
68
final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder ();
160
69
161
70
final ResourceModelFile resourceModelFile =
162
- new ResourceModelFile (data .getResourceModelName ());
163
- final ModelFile modelFile = new ModelFile (data .getModelName ());
71
+ new ResourceModelFile (data .getModuleName (), data . getResourceModelName ());
72
+ final ModelFile modelFile = new ModelFile (data .getModuleName (), data . getModelName ());
164
73
165
74
phpClassTypesBuilder .appendProperty ("NAME" , data .getCollectionName ())
166
- .appendProperty (
167
- "NAMESPACE" ,
168
- file .getNamespaceBuilder (
169
- data .getModuleName (),
170
- data .getCollectionDirectory ()
171
- ).getNamespace ()
172
- )
75
+ .appendProperty ("NAMESPACE" , file .getNamespaceBuilder ().getNamespace ())
173
76
.appendProperty ("DB_NAME" , data .getDbTableName ())
174
77
.appendProperty ("MODEL" , data .getModelName ())
175
78
.appendProperty ("RESOURCE_MODEL" , data .getResourceModelName ())
176
79
.append ("EXTENDS" , CollectionModelFile .ABSTRACT_COLLECTION )
177
80
.append (
178
81
"RESOURCE_MODEL" ,
179
- resourceModelFile .getNamespaceBuilder (
180
- data .getModuleName ()
181
- ).getClassFqn (),
82
+ resourceModelFile .getClassFqn (),
182
83
ResourceModelFile .ALIAS
183
84
)
184
85
.append (
185
86
"MODEL" ,
186
- modelFile .getNamespaceBuilder (
187
- data .getModuleName ()
188
- ).getClassFqn (),
87
+ modelFile .getClassFqn (),
189
88
ModelFile .ALIAS
190
89
)
191
90
.mergeProperties (attributes );
0 commit comments