Skip to content

Commit 35eee16

Browse files
Php file generators refactoring - model, resource model, collection, save entity command, etc.
1 parent ebeab55 commit 35eee16

19 files changed

+473
-610
lines changed

src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityByIdCommandGenerator.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ public PsiFile generate(final @NotNull String actionName) {
110110
@Override
111111
protected void fillAttributes(final @NotNull Properties attributes) {
112112
final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder();
113-
final ModelFile modelFile = new ModelFile(data.getModelName());
114-
final String modelType = modelFile.getNamespaceBuilder(data.getModuleName()).getClassFqn();
113+
final ModelFile modelFile = new ModelFile(data.getModuleName(), data.getModelName());
114+
final String modelType = modelFile.getClassFqn();
115115
final String modelFactoryType = modelType.concat("Factory");
116-
final ResourceModelFile resourceFile = new ResourceModelFile(data.getResourceModelName());
116+
final ResourceModelFile resourceFile =
117+
new ResourceModelFile(data.getModuleName(), data.getResourceModelName());
117118

118119
phpClassTypesBuilder
119120
.appendProperty("ENTITY_NAME", data.getEntityName())
@@ -127,8 +128,7 @@ protected void fillAttributes(final @NotNull Properties attributes) {
127128
.append("LOGGER", FrameworkLibraryType.LOGGER.getType())
128129
.append("MODEL", modelType)
129130
.append("MODEL_FACTORY", modelFactoryType)
130-
.append("RESOURCE",
131-
resourceFile.getNamespaceBuilder(data.getModuleName()).getClassFqn())
131+
.append("RESOURCE", resourceFile.getClassFqn())
132132
.mergeProperties(attributes);
133133

134134
attributes.setProperty("USES",

src/com/magento/idea/magento2plugin/actions/generation/generator/EntityDataMapperGenerator.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ public PsiFile generate(final @NotNull String actionName) {
110110
protected void fillAttributes(final @NotNull Properties attributes) {
111111
final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder();
112112

113-
final ModelFile modelFile = new ModelFile(data.getModelName());
113+
final ModelFile modelFile = new ModelFile(data.getModuleName(), data.getModelName());
114114
final DataModelFile dtoFile = new DataModelFile(data.getDtoName());
115115
final DataModelInterfaceFile dtoInterfaceFile =
116116
new DataModelInterfaceFile(data.getDtoInterfaceName());
@@ -128,10 +128,7 @@ protected void fillAttributes(final @NotNull Properties attributes) {
128128
.appendProperty("CLASS_NAME", file.getClassName())
129129
.append("DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getType())
130130
.append("DTO_TYPE", dtoType)
131-
.append(
132-
"MAGENTO_MODEL_TYPE",
133-
modelFile.getNamespaceBuilder(data.getModuleName()).getClassFqn()
134-
)
131+
.append("MAGENTO_MODEL_TYPE", modelFile.getClassFqn())
135132
.append("DTO_FACTORY", dtoType.concat("Factory"))
136133
.append("ABSTRACT_COLLECTION", FrameworkLibraryType.ABSTRACT_COLLECTION.getType())
137134
.mergeProperties(attributes);

src/com/magento/idea/magento2plugin/actions/generation/generator/FileGenerator.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,22 @@
1111
import java.util.Properties;
1212

1313
public abstract class FileGenerator {
14-
private final Project project;
15-
private final NavigateToCreatedFile navigateToCreatedFile;
14+
15+
protected final Project project;
16+
protected final NavigateToCreatedFile navigateToCreatedFile;
1617

1718
public FileGenerator(final Project project) {
1819
this.project = project;
1920
this.navigateToCreatedFile = NavigateToCreatedFile.getInstance();
2021
}
2122

23+
/**
24+
* Generate target file.
25+
*
26+
* @param actionName String
27+
*
28+
* @return PsiFile
29+
*/
2230
public abstract PsiFile generate(final String actionName);
2331

2432
/**
@@ -35,14 +43,26 @@ public PsiFile generate(final String actionName, final boolean openFile) {
3543
if (file != null && openFile) {
3644
navigateToCreatedFile.navigate(project, file);
3745
}
46+
3847
return file;
3948
}
4049

50+
/**
51+
* Get file properties.
52+
*
53+
* @return Properties
54+
*/
4155
protected Properties getAttributes() {
4256
final Properties attributes = new Properties();
4357
this.fillAttributes(attributes);
58+
4459
return attributes;
4560
}
4661

47-
protected abstract void fillAttributes(Properties attributes);
62+
/**
63+
* Fill attributes to be accessible from the file template.
64+
*
65+
* @param attributes Properties
66+
*/
67+
protected abstract void fillAttributes(final Properties attributes);
4868
}

src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,13 @@ public PsiFile generate(final @NotNull String actionName) {
111111
protected void fillAttributes(final @NotNull Properties attributes) {
112112
final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder();
113113
final CollectionModelFile collectionModelFile =
114-
new CollectionModelFile(data.getCollectionName());
114+
new CollectionModelFile(
115+
data.getModuleName(),
116+
data.getCollectionName(),
117+
data.getModelName()
118+
);
115119
final NamespaceBuilder collectionNamespace =
116-
collectionModelFile.getNamespaceBuilder(data.getModuleName(), data.getModelName());
120+
collectionModelFile.getNamespaceBuilder();
117121

118122
phpClassTypesBuilder
119123
.appendProperty("ENTITY_NAME", data.getEntityName())

src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java

Lines changed: 25 additions & 126 deletions
Original file line numberDiff line numberDiff line change
@@ -5,39 +5,20 @@
55

66
package com.magento.idea.magento2plugin.actions.generation.generator;
77

8-
import com.intellij.openapi.command.WriteCommandAction;
98
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;
149
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;
1710
import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
1811
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;
2213
import com.magento.idea.magento2plugin.magento.files.CollectionModelFile;
2314
import com.magento.idea.magento2plugin.magento.files.ModelFile;
2415
import com.magento.idea.magento2plugin.magento.files.ResourceModelFile;
25-
import com.magento.idea.magento2plugin.util.GetFirstClassOfFile;
26-
import com.magento.idea.magento2plugin.util.GetPhpClassByFQN;
2716
import java.util.Properties;
28-
import javax.swing.JOptionPane;
2917
import org.jetbrains.annotations.NotNull;
3018

31-
public class ModuleCollectionGenerator extends FileGenerator {
19+
public class ModuleCollectionGenerator extends PhpFileGenerator {
3220

3321
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;
4122

4223
/**
4324
* Generates new Collection PHP Class based on provided data.
@@ -49,104 +30,32 @@ public ModuleCollectionGenerator(
4930
final @NotNull CollectionData data,
5031
final @NotNull Project project
5132
) {
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);
6134
}
6235

6336
/**
64-
* Generates collection model class.
65-
*
66-
* @param actionName Action name
37+
* Generates new Collection PHP Class based on provided data.
6738
*
68-
* @return PsiFile
39+
* @param data CollectionData
40+
* @param project Project
41+
* @param checkFileAlreadyExists boolean
6942
*/
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;
11850
}
11951

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()
14358
);
144-
145-
if (modelFile == null) {
146-
return null;
147-
}
148-
149-
return getFirstClassOfFile.execute((PhpFile) modelFile);
15059
}
15160

15261
/**
@@ -159,33 +68,23 @@ protected void fillAttributes(final @NotNull Properties attributes) {
15968
final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder();
16069

16170
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());
16473

16574
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())
17376
.appendProperty("DB_NAME", data.getDbTableName())
17477
.appendProperty("MODEL", data.getModelName())
17578
.appendProperty("RESOURCE_MODEL", data.getResourceModelName())
17679
.append("EXTENDS", CollectionModelFile.ABSTRACT_COLLECTION)
17780
.append(
17881
"RESOURCE_MODEL",
179-
resourceModelFile.getNamespaceBuilder(
180-
data.getModuleName()
181-
).getClassFqn(),
82+
resourceModelFile.getClassFqn(),
18283
ResourceModelFile.ALIAS
18384
)
18485
.append(
18586
"MODEL",
186-
modelFile.getNamespaceBuilder(
187-
data.getModuleName()
188-
).getClassFqn(),
87+
modelFile.getClassFqn(),
18988
ModelFile.ALIAS
19089
)
19190
.mergeProperties(attributes);

0 commit comments

Comments
 (0)