|
6 | 6 | package com.magento.idea.magento2plugin.actions.generation.generator;
|
7 | 7 |
|
8 | 8 | import com.intellij.openapi.project.Project;
|
9 |
| -import com.intellij.psi.PsiDirectory; |
10 |
| -import com.intellij.psi.PsiFile; |
11 |
| -import com.jetbrains.php.lang.psi.PhpFile; |
12 |
| -import com.jetbrains.php.lang.psi.elements.PhpClass; |
13 | 9 | import com.magento.idea.magento2plugin.actions.generation.data.EavEntityDataInterface;
|
14 |
| -import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; |
15 |
| -import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; |
16 | 10 | import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil;
|
| 11 | +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; |
17 | 12 | import com.magento.idea.magento2plugin.actions.generation.generator.util.eav.AttributeMapperFactory;
|
18 | 13 | import com.magento.idea.magento2plugin.actions.generation.generator.util.eav.AttributeMapperInterface;
|
19 |
| -import com.magento.idea.magento2plugin.bundles.CommonBundle; |
20 |
| -import com.magento.idea.magento2plugin.bundles.ValidatorBundle; |
21 |
| -import com.magento.idea.magento2plugin.indexes.ModuleIndex; |
22 |
| -import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchPhp; |
23 |
| -import com.magento.idea.magento2plugin.magento.packages.File; |
24 |
| -import com.magento.idea.magento2plugin.magento.packages.Package; |
| 14 | +import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; |
| 15 | +import com.magento.idea.magento2plugin.magento.files.EavAttributeDataPatchFile; |
25 | 16 | import com.magento.idea.magento2plugin.magento.packages.eav.DataPatchDependency;
|
26 |
| -import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; |
27 |
| -import com.sun.istack.NotNull; |
28 |
| -import java.util.ArrayList; |
29 | 17 | import java.util.List;
|
30 | 18 | import java.util.Properties;
|
31 |
| -import javax.swing.JOptionPane; |
32 |
| -import org.jetbrains.annotations.Nullable; |
| 19 | +import org.jetbrains.annotations.NotNull; |
33 | 20 |
|
34 |
| -public class EavAttributeSetupPatchGenerator extends FileGenerator { |
35 |
| - private final EavEntityDataInterface eavEntityData; |
36 |
| - private final Project project; |
37 |
| - private final DirectoryGenerator directoryGenerator; |
38 |
| - private final FileFromTemplateGenerator fileFromTemplateGenerator; |
39 |
| - private final ValidatorBundle validatorBundle; |
40 |
| - private final CommonBundle commonBundle; |
| 21 | +public class EavAttributeSetupPatchGenerator extends PhpFileGenerator { |
| 22 | + private final EavEntityDataInterface data; |
41 | 23 |
|
42 | 24 | /**
|
43 | 25 | * Constructor.
|
44 | 26 | *
|
45 |
| - * @param eavEntityData EavEntityDataInterface |
46 |
| - * @param project Project |
| 27 | + * @param data EavEntityDataInterface |
| 28 | + * @param project Project |
47 | 29 | */
|
48 | 30 | public EavAttributeSetupPatchGenerator(
|
49 |
| - final @NotNull EavEntityDataInterface eavEntityData, |
| 31 | + final @NotNull EavEntityDataInterface data, |
50 | 32 | final Project project
|
51 | 33 | ) {
|
52 |
| - super(project); |
53 |
| - |
54 |
| - this.eavEntityData = eavEntityData; |
55 |
| - this.project = project; |
56 |
| - this.directoryGenerator = new DirectoryGenerator(); |
57 |
| - this.fileFromTemplateGenerator = new FileFromTemplateGenerator(project); |
58 |
| - this.validatorBundle = new ValidatorBundle(); |
59 |
| - this.commonBundle = new CommonBundle(); |
| 34 | + this(data, project, true); |
60 | 35 | }
|
61 | 36 |
|
62 |
| - @Override |
63 |
| - public PsiFile generate(final String actionName) { |
64 |
| - final String errorTitle = commonBundle.message("common.error"); |
65 |
| - final PhpClass dataPatchClass = GetPhpClassByFQN.getInstance(project) |
66 |
| - .execute(getDataPatchFqn()); |
67 |
| - |
68 |
| - if (validateIfFileAlreadyExist(dataPatchClass, errorTitle)) { |
69 |
| - return null; |
70 |
| - } |
71 |
| - |
72 |
| - final PhpFile dataPathFile = createDataPathClass(actionName); |
73 |
| - |
74 |
| - if (validateIfFileCanBeCreated(errorTitle, dataPathFile)) { |
75 |
| - return null; |
76 |
| - } |
77 |
| - |
78 |
| - return dataPathFile; |
79 |
| - } |
80 |
| - |
81 |
| - private String getDataPatchFqn() { |
82 |
| - return eavEntityData.getNamespace() |
83 |
| - + Package.fqnSeparator |
84 |
| - + eavEntityData.getDataPatchName(); |
85 |
| - } |
86 |
| - |
87 |
| - private boolean validateIfFileAlreadyExist( |
88 |
| - final PhpClass dataPatchClass, |
89 |
| - final String errorTitle |
| 37 | + /** |
| 38 | + * Php file generator constructor. |
| 39 | + * |
| 40 | + * @param project Project |
| 41 | + * @param checkFileAlreadyExists boolean |
| 42 | + */ |
| 43 | + public EavAttributeSetupPatchGenerator( |
| 44 | + final @NotNull EavEntityDataInterface data, |
| 45 | + final @NotNull Project project, |
| 46 | + final boolean checkFileAlreadyExists |
90 | 47 | ) {
|
91 |
| - if (dataPatchClass != null) { |
92 |
| - final String errorMessage = validatorBundle.message( |
93 |
| - "validator.file.alreadyExists", |
94 |
| - "Data Patch Class" |
95 |
| - ); |
96 |
| - JOptionPane.showMessageDialog( |
97 |
| - null, |
98 |
| - errorMessage, |
99 |
| - errorTitle, |
100 |
| - JOptionPane.ERROR_MESSAGE |
101 |
| - ); |
102 |
| - |
103 |
| - return true; |
104 |
| - } |
105 |
| - |
106 |
| - return false; |
| 48 | + super(project, checkFileAlreadyExists); |
| 49 | + this.data = data; |
107 | 50 | }
|
108 | 51 |
|
109 |
| - @Nullable |
110 |
| - private PhpFile createDataPathClass(final String actionName) { |
111 |
| - final PsiDirectory parentDirectory = getDataPatchDirectory(); |
112 |
| - final Properties attributes = getAttributes(); |
113 |
| - final PsiFile dataPatchFile = fileFromTemplateGenerator.generate( |
114 |
| - new EavAttributeDataPatchPhp(eavEntityData.getDataPatchName()), |
115 |
| - attributes, |
116 |
| - parentDirectory, |
117 |
| - actionName |
118 |
| - ); |
119 |
| - |
120 |
| - if (dataPatchFile == null) { |
121 |
| - return null; |
122 |
| - } |
123 |
| - |
124 |
| - return (PhpFile) dataPatchFile; |
125 |
| - } |
126 |
| - |
127 |
| - private PsiDirectory getDataPatchDirectory() { |
128 |
| - PsiDirectory parentDirectory = new ModuleIndex(project) |
129 |
| - .getModuleDirectoryByModuleName(eavEntityData.getModuleName()); |
130 |
| - final String[] dataPatchDirectories = eavEntityData.getDirectory().split(File.separator); |
131 |
| - |
132 |
| - for (final String dataPatchDirectory : dataPatchDirectories) { |
133 |
| - parentDirectory = directoryGenerator.findOrCreateSubdirectory( |
134 |
| - parentDirectory, |
135 |
| - dataPatchDirectory |
136 |
| - ); |
137 |
| - } |
138 |
| - |
139 |
| - return parentDirectory; |
140 |
| - } |
141 |
| - |
142 |
| - private boolean validateIfFileCanBeCreated( |
143 |
| - final String errorTitle, |
144 |
| - final PhpFile dataPathFile |
145 |
| - ) { |
146 |
| - if (dataPathFile == null) { |
147 |
| - final String errorMessage = validatorBundle.message( |
148 |
| - "validator.file.cantBeCreated", |
149 |
| - "Data Patch Class" |
150 |
| - ); |
151 |
| - JOptionPane.showMessageDialog( |
152 |
| - null, |
153 |
| - errorMessage, |
154 |
| - errorTitle, |
155 |
| - JOptionPane.ERROR_MESSAGE |
156 |
| - ); |
157 |
| - |
158 |
| - return true; |
159 |
| - } |
160 |
| - return false; |
| 52 | + @Override |
| 53 | + protected AbstractPhpFile initFile() { |
| 54 | + return new EavAttributeDataPatchFile(data.getModuleName(), data.getDataPatchName()); |
161 | 55 | }
|
162 | 56 |
|
163 | 57 | @Override
|
164 | 58 | protected void fillAttributes(final Properties attributes) {
|
165 |
| - attributes.setProperty("NAME", eavEntityData.getDataPatchName()); |
166 |
| - attributes.setProperty("NAMESPACE", eavEntityData.getNamespace()); |
167 |
| - |
168 |
| - final String dataPatchInterface = DataPatchDependency.DATA_PATCH_INTERFACE.getClassPatch(); |
169 |
| - attributes.setProperty( |
170 |
| - "IMPLEMENTS", |
171 |
| - PhpClassGeneratorUtil.getNameFromFqn(dataPatchInterface) |
172 |
| - ); |
173 |
| - |
174 |
| - final String eavSetup = DataPatchDependency.ENV_SETUP.getClassPatch(); |
175 |
| - final String eavSetupFactory = DataPatchDependency.EAV_SETUP_FACTORY.getClassPatch(); |
176 |
| - final String moduleDataSetupInterface = |
177 |
| - DataPatchDependency.MODULE_DATA_SETUP_INTERFACE.getClassPatch(); |
178 |
| - final String entityClass = eavEntityData.getEntityClass(); |
179 |
| - |
| 59 | + final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); |
| 60 | + |
| 61 | + phpClassTypesBuilder |
| 62 | + .appendProperty("CLASS_NAME", data.getDataPatchName()) |
| 63 | + .appendProperty("NAMESPACE", data.getNamespace()) |
| 64 | + .appendProperty("ENTITY_CLASS", data.getEntityClass()) |
| 65 | + .appendProperty("ATTRIBUTE_CODE", data.getCode()) |
| 66 | + .appendProperty("ATTRIBUTE_SET", String.join(",", getAttributesList(data))) |
| 67 | + .append( |
| 68 | + "IMPLEMENTS", |
| 69 | + DataPatchDependency.DATA_PATCH_INTERFACE.getClassPatch() |
| 70 | + ) |
| 71 | + .append( |
| 72 | + "MODULE_DATA_SETUP_INTERFACE", |
| 73 | + DataPatchDependency.MODULE_DATA_SETUP_INTERFACE.getClassPatch()) |
| 74 | + .append( |
| 75 | + "EAV_SETUP_FACTORY", |
| 76 | + DataPatchDependency.EAV_SETUP_FACTORY.getClassPatch() |
| 77 | + ) |
| 78 | + .append( |
| 79 | + "EAV_SETUP", |
| 80 | + DataPatchDependency.ENV_SETUP.getClassPatch() |
| 81 | + ) |
| 82 | + .mergeProperties(attributes); |
180 | 83 | attributes.setProperty(
|
181 |
| - "MODULE_DATA_SETUP_INTERFACE", |
182 |
| - PhpClassGeneratorUtil.getNameFromFqn(moduleDataSetupInterface) |
| 84 | + "USES", |
| 85 | + PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) |
183 | 86 | );
|
184 |
| - attributes.setProperty( |
185 |
| - "EAV_SETUP_FACTORY", |
186 |
| - PhpClassGeneratorUtil.getNameFromFqn(eavSetupFactory) |
187 |
| - ); |
188 |
| - attributes.setProperty( |
189 |
| - "EAV_SETUP", |
190 |
| - PhpClassGeneratorUtil.getNameFromFqn(eavSetup) |
191 |
| - ); |
192 |
| - attributes.setProperty( |
193 |
| - "ENTITY_CLASS", |
194 |
| - entityClass |
195 |
| - ); |
196 |
| - |
197 |
| - final List<String> uses = new ArrayList<>(); |
198 |
| - uses.add(dataPatchInterface); |
199 |
| - uses.add(eavSetup); |
200 |
| - uses.add(eavSetupFactory); |
201 |
| - uses.add(moduleDataSetupInterface); |
202 |
| - |
203 |
| - attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); |
204 |
| - |
205 |
| - attributes.setProperty("ATTRIBUTE_CODE", eavEntityData.getCode()); |
206 |
| - |
207 |
| - final List<String> entityAttributes = getAttributesList(eavEntityData); |
208 |
| - attributes.setProperty("ATTRIBUTE_SET", String.join(",", entityAttributes)); |
209 | 87 | }
|
210 | 88 |
|
211 | 89 | private List<String> getAttributesList(final EavEntityDataInterface eavEntityData) {
|
|
0 commit comments