From 0b2f495fdda097c55fee23e41740ba153c9307f1 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Fri, 13 Nov 2020 17:59:59 +0200 Subject: [PATCH 1/3] Added generate models action --- resources/META-INF/plugin.xml | 4 + .../Magento Module Collection Class.php.ft | 26 ++ .../Magento Module Collection Class.php.html | 0 .../Magento Module Model Class.php.ft | 26 ++ .../Magento Module Model Class.php.html | 0 ...Magento Module Resource Model Class.php.ft | 26 ++ ...gento Module Resource Model Class.php.html | 0 .../actions/generation/NewModelsAction.java | 56 ++++ .../generation/data/CollectionData.java | 146 ++++++++++ .../actions/generation/data/ModelData.java | 108 +++++++ .../generation/data/ResourceModelData.java | 95 ++++++ .../generation/dialog/NewModelsDialog.form | 168 +++++++++++ .../generation/dialog/NewModelsDialog.java | 271 ++++++++++++++++++ .../generator/ModuleCollectionGenerator.java | 182 ++++++++++++ .../ModuleControllerClassGenerator.java | 27 +- .../generator/ModuleModelGenerator.java | 172 +++++++++++ .../ModuleResourceModelGenerator.java | 177 ++++++++++++ .../generator/util/PhpClassGeneratorUtil.java | 38 +++ .../magento/files/AbstractPhpClass.java | 27 ++ .../magento/files/CollectionPhp.java | 20 ++ .../magento/files/FormButtonBlockPhp.java | 22 +- .../magento/files/ModelPhp.java | 21 ++ .../magento/files/ResourceModelPhp.java | 21 ++ .../generateFile/TestCollection.php | 23 ++ .../generateFile/TestModel.php | 22 ++ .../generateFile/TestResourceModel.php | 21 ++ .../ModuleCollectionGeneratorTest.java | 44 +++ .../generator/ModuleModelGeneratorTest.java | 41 +++ .../ModuleResourceModelGeneratorTest.java | 40 +++ 29 files changed, 1786 insertions(+), 38 deletions(-) create mode 100644 resources/fileTemplates/internal/Magento Module Collection Class.php.ft create mode 100644 resources/fileTemplates/internal/Magento Module Collection Class.php.html create mode 100644 resources/fileTemplates/internal/Magento Module Model Class.php.ft create mode 100644 resources/fileTemplates/internal/Magento Module Model Class.php.html create mode 100644 resources/fileTemplates/internal/Magento Module Resource Model Class.php.ft create mode 100644 resources/fileTemplates/internal/Magento Module Resource Model Class.php.html create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/NewModelsAction.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/ModelData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/data/ResourceModelData.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.form create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java create mode 100644 src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java create mode 100644 src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java create mode 100644 src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java create mode 100644 src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java create mode 100644 src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java create mode 100644 testData/actions/generation/generator/ModuleCollectionGenerator/generateFile/TestCollection.php create mode 100644 testData/actions/generation/generator/ModuleModelGenerator/generateFile/TestModel.php create mode 100644 testData/actions/generation/generator/ModuleResourceModelGenerator/generateFile/TestResourceModel.php create mode 100644 tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGeneratorTest.java create mode 100644 tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGeneratorTest.java create mode 100644 tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGeneratorTest.java diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index 854c516e4..4a8cd7203 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -67,6 +67,7 @@ + @@ -212,6 +213,9 @@ + + + diff --git a/resources/fileTemplates/internal/Magento Module Collection Class.php.ft b/resources/fileTemplates/internal/Magento Module Collection Class.php.ft new file mode 100644 index 000000000..58a15875e --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Collection Class.php.ft @@ -0,0 +1,26 @@ +_init(${MODEL}::class, ${RESOURCE_MODEL}::class); + } +} diff --git a/resources/fileTemplates/internal/Magento Module Collection Class.php.html b/resources/fileTemplates/internal/Magento Module Collection Class.php.html new file mode 100644 index 000000000..e69de29bb diff --git a/resources/fileTemplates/internal/Magento Module Model Class.php.ft b/resources/fileTemplates/internal/Magento Module Model Class.php.ft new file mode 100644 index 000000000..e4ed8d293 --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Model Class.php.ft @@ -0,0 +1,26 @@ +_init(${RESOURCE_MODEL}::class); + } +} diff --git a/resources/fileTemplates/internal/Magento Module Model Class.php.html b/resources/fileTemplates/internal/Magento Module Model Class.php.html new file mode 100644 index 000000000..e69de29bb diff --git a/resources/fileTemplates/internal/Magento Module Resource Model Class.php.ft b/resources/fileTemplates/internal/Magento Module Resource Model Class.php.ft new file mode 100644 index 000000000..701392df7 --- /dev/null +++ b/resources/fileTemplates/internal/Magento Module Resource Model Class.php.ft @@ -0,0 +1,26 @@ +_init('${DB_NAME}', '${ENTITY_ID_COLUMN}'); + } +} diff --git a/resources/fileTemplates/internal/Magento Module Resource Model Class.php.html b/resources/fileTemplates/internal/Magento Module Resource Model Class.php.html new file mode 100644 index 000000000..e69de29bb diff --git a/src/com/magento/idea/magento2plugin/actions/generation/NewModelsAction.java b/src/com/magento/idea/magento2plugin/actions/generation/NewModelsAction.java new file mode 100644 index 000000000..e9034f882 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/NewModelsAction.java @@ -0,0 +1,56 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation; + +import com.intellij.ide.IdeView; +import com.intellij.openapi.actionSystem.AnAction; +import com.intellij.openapi.actionSystem.AnActionEvent; +import com.intellij.openapi.actionSystem.CommonDataKeys; +import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.actionSystem.LangDataKeys; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.magento.idea.magento2plugin.MagentoIcons; +import com.magento.idea.magento2plugin.actions.generation.dialog.NewModelsDialog; + +public class NewModelsAction extends AnAction { + public static final String ACTION_NAME = "Magento 2 Models"; + public static final String ACTION_DESCRIPTION = "Create a new Magento 2 models"; + + /** + * New controller action constructor. + */ + public NewModelsAction() { + super(ACTION_NAME, ACTION_DESCRIPTION, MagentoIcons.MODULE); + } + + @Override + public void actionPerformed(final AnActionEvent event) { + final DataContext dataContext = event.getDataContext(); + final IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext); + + if (view == null) { + return; + } + + final Project project = CommonDataKeys.PROJECT.getData(dataContext); + if (project == null) { + return; + } + + final PsiDirectory directory = view.getOrChooseDirectory(); + if (directory == null) { + return; + } + + NewModelsDialog.open(project, directory); + } + + @Override + public boolean isDumbAware() { + return false; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java new file mode 100644 index 000000000..75fdfa0ce --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java @@ -0,0 +1,146 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +public class CollectionData { + private final String moduleName; + private final String dbTableName; + private final String modelName; + private final String collectionName; + private final String collectionFqn; + private final String collectionDirectory; + private final String collectionNamespace; + private final String resourceModelName; + private final String resourceModelFqn; + private final String modelFqn; + + /** + * Models Data. + * + * @param moduleName String + * @param dbTableName String + * @param modelName String + * @param collectionName String + * @param collectionFqn String + * @param collectionDirectory String + * @param resourceModelName String + * @param resourceModelFqn String + * @param modelFqn String + */ + public CollectionData( + final String moduleName, + final String dbTableName, + final String modelName, + final String collectionName, + final String collectionFqn, + final String collectionDirectory, + final String collectionNamespace, + final String resourceModelName, + final String resourceModelFqn, + final String modelFqn + ) { + this.moduleName = moduleName; + this.dbTableName = dbTableName; + this.modelName = modelName; + this.collectionName = collectionName; + this.collectionFqn = collectionFqn; + this.collectionDirectory = collectionDirectory; + this.collectionNamespace = collectionNamespace; + this.resourceModelName = resourceModelName; + this.resourceModelFqn = resourceModelFqn; + this.modelFqn = modelFqn; + } + + /** + * Module Name. + * + * @return String + */ + public String getModuleName() { + return moduleName; + } + + /** + * DB table Name. + * + * @return String + */ + public String getDbTableName() { + return dbTableName; + } + + /** + * Model Name. + * + * @return String + */ + public String getModelName() { + return modelName; + } + + /** + * Collection Name. + * + * @return String + */ + public String getCollectionName() { + return collectionName; + } + + /** + * Collection FQN. + * + * @return String + */ + public String getCollectionFqn() { + return collectionFqn; + } + + /** + * Collection Directory. + * + * @return String + */ + public String getCollectionDirectory() { + return collectionDirectory; + } + + /** + * Collection Namespace. + * + * @return String + */ + public String getCollectionNamespace() { + return collectionNamespace; + } + + /** + * Resource Model Name. + * + * @return String + */ + public String getResourceModelName() { + return resourceModelName; + } + + /** + * Resource Model FQN. + * + * @return String + */ + public String getResourceModelFqn() { + return resourceModelFqn; + } + + /** + * Model FQN. + * + * @return String + */ + public String getModelFqn() { + return modelFqn; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ModelData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ModelData.java new file mode 100644 index 000000000..a20a2eb4d --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ModelData.java @@ -0,0 +1,108 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +public class ModelData { + private final String moduleName; + private final String dbTableName; + private final String modelName; + private final String resourceName; + private final String fqn; + private final String namespace; + private final String resourceModelFqn; + + /** + * Model Data. + * + * @param moduleName String + * @param dbTableName String + * @param modelName String + * @param resourceName String + * @param fqn String + * @param namespace String + * @param resourceModelFqn String + */ + public ModelData( + final String moduleName, + final String dbTableName, + final String modelName, + final String resourceName, + final String fqn, + final String namespace, + final String resourceModelFqn + ) { + this.moduleName = moduleName; + this.dbTableName = dbTableName; + this.modelName = modelName; + this.resourceName = resourceName; + this.fqn = fqn; + this.namespace = namespace; + this.resourceModelFqn = resourceModelFqn; + } + + /** + * Module Name. + * + * @return String + */ + public String getModuleName() { + return moduleName; + } + + /** + * DB table Name. + * + * @return String + */ + public String getDbTableName() { + return dbTableName; + } + + /** + * Model Name. + * + * @return String + */ + public String getModelName() { + return modelName; + } + + /** + * Resource Name. + * + * @return String + */ + public String getResourceName() { + return resourceName; + } + + /** + * Class FQN. + * + * @return String + */ + public String getFqn() { + return fqn; + } + + /** + * Class Namespace. + * + * @return String + */ + public String getNamespace() { + return namespace; + } + + /** + * Resource model FQN. + * + * @return String + */ + public String getResourceModelFqn() { + return resourceModelFqn; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/ResourceModelData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/ResourceModelData.java new file mode 100644 index 000000000..2d265fe32 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/ResourceModelData.java @@ -0,0 +1,95 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.data; + +public class ResourceModelData { + private final String moduleName; + private final String dbTableName; + private final String resourceModelName; + private final String entityIdColumn; + private final String namespace; + private final String fqn; + + /** + * Resource Model Data. + * + * @param moduleName String + * @param dbTableName String + * @param resourceModelName String + * @param entityIdColumn String + * @param namespace String + * @param fqn String + */ + public ResourceModelData( + final String moduleName, + final String dbTableName, + final String resourceModelName, + final String entityIdColumn, + final String namespace, + final String fqn + ) { + this.moduleName = moduleName; + this.dbTableName = dbTableName; + this.resourceModelName = resourceModelName; + this.entityIdColumn = entityIdColumn; + this.namespace = namespace; + this.fqn = fqn; + } + + /** + * Module Name. + * + * @return String + */ + public String getModuleName() { + return moduleName; + } + + /** + * DB table Name. + * + * @return String + */ + public String getDbTableName() { + return dbTableName; + } + + /** + * Entity ID column. + * + * @return String + */ + public String getEntityIdColumn() { + return entityIdColumn; + } + + /** + * Resource Model name. + * + * @return String + */ + public String getResourceModelName() { + return resourceModelName; + } + + /** + * Namespace getter. + * + * @return String. + */ + public String getNamespace() { + return namespace; + } + + /** + * FQN getter. + * + * @return String. + */ + public String getFqn() { + return fqn; + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.form b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.form new file mode 100644 index 000000000..ec6e68d88 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.form @@ -0,0 +1,168 @@ + +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
diff --git a/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.java b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.java new file mode 100644 index 000000000..8acfe6a4a --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/dialog/NewModelsDialog.java @@ -0,0 +1,271 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.dialog; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.intellij.ui.DocumentAdapter; +import com.magento.idea.magento2plugin.actions.generation.data.CollectionData; +import com.magento.idea.magento2plugin.actions.generation.data.ModelData; +import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.FieldValidation; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.annotation.RuleRegistry; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.IdentifierRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.NotEmptyRule; +import com.magento.idea.magento2plugin.actions.generation.dialog.validator.rule.PhpClassRule; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleCollectionGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder; +import com.magento.idea.magento2plugin.magento.files.ModelPhp; +import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp; +import com.magento.idea.magento2plugin.magento.packages.File; +import com.magento.idea.magento2plugin.util.magento.GetModuleNameByDirectoryUtil; +import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; +import java.awt.event.WindowAdapter; +import java.awt.event.WindowEvent; +import javax.swing.JButton; +import javax.swing.JComponent; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.JTextField; +import javax.swing.KeyStroke; +import javax.swing.event.DocumentEvent; +import org.jetbrains.annotations.NotNull; + +@SuppressWarnings({"PMD.TooManyFields", "PMD.ExcessiveImports"}) +public class NewModelsDialog extends AbstractDialog { + private final String moduleName; + private final Project project; + private JPanel contentPane; + private JButton buttonOK; + private JButton buttonCancel; + + private static final String ACTION_NAME = "Create Models"; + private static final String MODEL_NAME = "Model Name"; + private static final String RESOURCE_MODEL_NAME = "Resource Model Name"; + private static final String DB_TABLE_NAME = "DB Table Name"; + private static final String ENTITY_ID_COLUMN_NAME = "Entity ID Column Name"; + private static final String COLLECTION_NAME = "Collection Name"; + private static final String COLLECTION_DIRECTORY = "Collection Directory"; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, MODEL_NAME}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, MODEL_NAME}) + private JTextField modelName; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, RESOURCE_MODEL_NAME}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, RESOURCE_MODEL_NAME}) + private JTextField resourceModelName; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, DB_TABLE_NAME}) + @FieldValidation(rule = RuleRegistry.IDENTIFIER, + message = {IdentifierRule.MESSAGE, DB_TABLE_NAME}) + private JTextField dbTableName; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, ENTITY_ID_COLUMN_NAME}) + @FieldValidation(rule = RuleRegistry.IDENTIFIER, + message = {IdentifierRule.MESSAGE, ENTITY_ID_COLUMN_NAME}) + private JTextField entityIdColumn; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, COLLECTION_DIRECTORY}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, COLLECTION_DIRECTORY}) + private JTextField collectionDirectory; + + @FieldValidation(rule = RuleRegistry.NOT_EMPTY, + message = {NotEmptyRule.MESSAGE, COLLECTION_NAME}) + @FieldValidation(rule = RuleRegistry.PHP_CLASS, + message = {PhpClassRule.MESSAGE, COLLECTION_NAME}) + private JTextField collectionName; + + private JLabel modelNameLabel;//NOPMD + private JLabel dbTableNameLabel;//NOPMD + private JLabel resourceModelLabel;//NOPMD + private JLabel entityIdColumnLabel;//NOPMD + private JLabel collectionDirectoryLabel;//NOPMD + private JLabel collectionNameLabel;//NOPMD + + /** + * Open new dialog for adding new controller. + * + * @param project Project + * @param directory PsiDirectory + */ + public NewModelsDialog(final Project project, final PsiDirectory directory) { + super(); + this.project = project; + this.moduleName = GetModuleNameByDirectoryUtil.execute(directory, project); + + setContentPane(contentPane); + setModal(true); + getRootPane().setDefaultButton(buttonOK); + buttonOK.addActionListener(e -> onOK()); + buttonCancel.addActionListener(e -> onCancel()); + + // call onCancel() when cross is clicked + setDefaultCloseOperation(DO_NOTHING_ON_CLOSE); + addWindowListener(new WindowAdapter() { + @Override + public void windowClosing(final WindowEvent event) { + onCancel(); + } + }); + + // call onCancel() on ESCAPE + contentPane.registerKeyboardAction( + (final ActionEvent event) -> onCancel(), + KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), + JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT + ); + + this.modelName.getDocument().addDocumentListener(new DocumentAdapter() { + @Override + protected void textChanged(final @NotNull DocumentEvent event) { + updateText(); + } + }); + } + + /** + * Update collection and resource model text. + */ + public void updateText() { + final String modelName = this.modelName.getText(); + this.resourceModelName.setText(modelName); + this.collectionDirectory.setText(modelName); + } + + private String getModuleName() { + return moduleName; + } + + private String getModelName() { + return modelName.getText().trim(); + } + + private String getCollectionName() { + return collectionName.getText().trim(); + } + + private String getResourceModelName() { + return resourceModelName.getText().trim(); + } + + private String getDbTableName() { + return dbTableName.getText().trim(); + } + + private String getEntityIdColumn() { + return entityIdColumn.getText().trim(); + } + + private String getCollectionDirectory() { + return ResourceModelPhp.RESOURCE_MODEL_DIRECTORY + File.separator + + collectionDirectory.getText().trim(); + } + + private NamespaceBuilder getModelNamespace() { + return new NamespaceBuilder(getModuleName(), getModelName(), ModelPhp.MODEL_DIRECTORY); + } + + private NamespaceBuilder getResourceModelNamespace() { + return new NamespaceBuilder( + getModuleName(), + getResourceModelName(), + ResourceModelPhp.RESOURCE_MODEL_DIRECTORY + ); + } + + private NamespaceBuilder getCollectionNamespace() { + return new NamespaceBuilder( + getModuleName(), + getCollectionName(), + getCollectionDirectory() + ); + } + + /** + * Open new controller dialog. + * + * @param project Project + * @param directory PsiDirectory + */ + public static void open(final Project project, final PsiDirectory directory) { + final NewModelsDialog dialog = new NewModelsDialog(project, directory); + dialog.pack(); + dialog.centerDialog(dialog); + dialog.setVisible(true); + } + + private void onOK() { + if (!validateFormFields()) { + return; + } + + generateModelFile(); + generateResourceModelFile(); + generateCollectionFile(); + this.setVisible(false); + } + + private PsiFile generateModelFile() { + final NamespaceBuilder modelNamespace = getModelNamespace(); + final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); + return new ModuleModelGenerator(new ModelData( + getModuleName(), + getDbTableName(), + getModelName(), + getResourceModelName(), + modelNamespace.getClassFqn(), + modelNamespace.getNamespace(), + resourceModelNamespace.getClassFqn() + ), project).generate(NewModelsDialog.ACTION_NAME, true); + } + + private PsiFile generateResourceModelFile() { + final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); + return new ModuleResourceModelGenerator(new ResourceModelData( + getModuleName(), + getDbTableName(), + getResourceModelName(), + getEntityIdColumn(), + resourceModelNamespace.getNamespace(), + resourceModelNamespace.getClassFqn() + ), project).generate(NewModelsDialog.ACTION_NAME, true); + } + + private PsiFile generateCollectionFile() { + final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace(); + final NamespaceBuilder modelNamespace = getModelNamespace(); + final NamespaceBuilder collectionNamespace = getCollectionNamespace(); + return new ModuleCollectionGenerator(new CollectionData( + getModuleName(), + getDbTableName(), + getModelName(), + getCollectionName(), + collectionNamespace.getClassFqn(), + getCollectionDirectory(), + collectionNamespace.getNamespace(), + getResourceModelName(), + resourceModelNamespace.getClassFqn(), + modelNamespace.getClassFqn() + ), project).generate(NewModelsDialog.ACTION_NAME, true); + } + + @Override + protected void onCancel() { + dispose(); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java new file mode 100644 index 000000000..a9cdac186 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java @@ -0,0 +1,182 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.jetbrains.php.lang.psi.PhpFile; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.magento.idea.magento2plugin.actions.generation.data.CollectionData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; +import com.magento.idea.magento2plugin.bundles.CommonBundle; +import com.magento.idea.magento2plugin.bundles.ValidatorBundle; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.CollectionPhp; +import com.magento.idea.magento2plugin.magento.packages.File; +import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import javax.swing.JOptionPane; + +public class ModuleCollectionGenerator extends FileGenerator { + private final CollectionData collectionData; + private final Project project; + private final ValidatorBundle validatorBundle; + private final CommonBundle commonBundle; + private final GetFirstClassOfFile getFirstClassOfFile; + private final DirectoryGenerator directoryGenerator; + private final FileFromTemplateGenerator fileFromTemplateGenerator; + + /** + * Generates new Collection PHP Class based on provided data. + * + * @param collectionData CollectionData + * @param project Project + */ + public ModuleCollectionGenerator( + final CollectionData collectionData, + final Project project + ) { + super(project); + this.project = project; + this.collectionData = collectionData; + this.directoryGenerator = DirectoryGenerator.getInstance(); + this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project); + this.getFirstClassOfFile = GetFirstClassOfFile.getInstance(); + this.validatorBundle = new ValidatorBundle(); + this.commonBundle = new CommonBundle(); + } + + /** + * Generates collection model class. + * + * @param actionName Action name + * @return PsiFile + */ + public PsiFile generate(final String actionName) { + final PsiFile[] collectionFiles = new PsiFile[1]; + + WriteCommandAction.runWriteCommandAction(project, () -> { + PhpClass collection = GetPhpClassByFQN.getInstance(project).execute( + getCollectionModelFqn() + ); + + if (collection != null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.alreadyExists", + "Collection Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + collection = createClass(actionName); + + if (collection == null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.cantBeCreated", + "Collection Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + collectionFiles[0] = collection.getContainingFile(); + }); + + return collectionFiles[0]; + } + + /** + * Get controller module. + * + * @return String + */ + public String getModuleName() { + return collectionData.getModuleName(); + } + + private String getCollectionModelFqn() { + return collectionData.getCollectionFqn(); + } + + private PhpClass createClass(final String actionName) { + PsiDirectory parentDirectory = ModuleIndex.getInstance(project) + .getModuleDirectoryByModuleName(getModuleName()); + final PsiFile modelFile; + + final String[] collectionDirectories = collectionData.getCollectionDirectory().split( + File.separator + ); + for (final String directory: collectionDirectories) { + parentDirectory = directoryGenerator.findOrCreateSubdirectory( + parentDirectory, directory + ); + } + + final Properties attributes = getAttributes(); + modelFile = fileFromTemplateGenerator.generate( + new CollectionPhp(collectionData.getCollectionName()), + attributes, + parentDirectory, + actionName + ); + + if (modelFile == null) { + return null; + } + + return getFirstClassOfFile.execute((PhpFile) modelFile); + } + + protected void fillAttributes(final Properties attributes) { + attributes.setProperty("NAME", collectionData.getCollectionName()); + attributes.setProperty("NAMESPACE", collectionData.getCollectionNamespace()); + + attributes.setProperty("DB_NAME", collectionData.getDbTableName()); + attributes.setProperty("MODEL", PhpClassGeneratorUtil.getNameFromFqn( + collectionData.getModelName()) + ); + attributes.setProperty("RESOURCE_MODEL", PhpClassGeneratorUtil.getNameFromFqn( + collectionData.getResourceModelName()) + ); + final List uses = getUses(); + uses.add(collectionData.getResourceModelFqn()); + uses.add(collectionData.getModelFqn()); + + attributes.setProperty( + "EXTENDS", + PhpClassGeneratorUtil.getNameFromFqn(CollectionPhp.ABSTRACT_COLLECTION) + ); + + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); + } + + private List getUses() { + return new ArrayList<>(Arrays.asList( + CollectionPhp.ABSTRACT_COLLECTION + )); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleControllerClassGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleControllerClassGenerator.java index b3d04cfd9..ff7ab2d44 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleControllerClassGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleControllerClassGenerator.java @@ -14,6 +14,7 @@ import com.magento.idea.magento2plugin.actions.generation.data.ControllerFileData; import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; import com.magento.idea.magento2plugin.bundles.CommonBundle; import com.magento.idea.magento2plugin.bundles.ValidatorBundle; import com.magento.idea.magento2plugin.indexes.ModuleIndex; @@ -28,7 +29,6 @@ import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; import java.util.Properties; import javax.swing.JOptionPane; @@ -189,7 +189,10 @@ protected void fillAttributes(final Properties attributes) { final String httpMethodInterface = getHttpMethodInterfaceByMethod( controllerFileData.getHttpMethodName() ); - attributes.setProperty("IMPLEMENTS", getNameFromFqn(httpMethodInterface)); + attributes.setProperty( + "IMPLEMENTS", + PhpClassGeneratorUtil.getNameFromFqn(httpMethodInterface) + ); final List uses = getUses(); uses.add(httpMethodInterface); @@ -200,22 +203,22 @@ protected void fillAttributes(final Properties attributes) { uses.add(Controller.ADMINHTML_CONTROLLER_FQN); attributes.setProperty( "EXTENDS", - getNameFromFqn(Controller.ADMINHTML_CONTROLLER_FQN) + PhpClassGeneratorUtil.getNameFromFqn(Controller.ADMINHTML_CONTROLLER_FQN) ); attributes.setProperty( "ACL", - getNameFromFqn(controllerFileData.getAcl()) + PhpClassGeneratorUtil.getNameFromFqn(controllerFileData.getAcl()) ); } else { uses.add(Controller.FRONTEND_CONTROLLER_FQN); attributes.setProperty( "EXTENDS", - getNameFromFqn(Controller.FRONTEND_CONTROLLER_FQN) + PhpClassGeneratorUtil.getNameFromFqn(Controller.FRONTEND_CONTROLLER_FQN) ); } } - attributes.setProperty("USES", formatUses(uses)); + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); } private List getUses() { @@ -225,16 +228,4 @@ private List getUses() { Controller.EXCEPTION_CLASS_FQN )); } - - private String formatUses(final List uses) { - Collections.sort(uses); - - return String.join(",", uses); - } - - private String getNameFromFqn(final String fqn) { - final String[] fqnArray = fqn.split("\\\\"); - - return fqnArray[fqnArray.length - 1]; - } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java new file mode 100644 index 000000000..5a746f908 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java @@ -0,0 +1,172 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.jetbrains.php.lang.psi.PhpFile; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.magento.idea.magento2plugin.actions.generation.data.ModelData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; +import com.magento.idea.magento2plugin.bundles.CommonBundle; +import com.magento.idea.magento2plugin.bundles.ValidatorBundle; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.ModelPhp; +import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import javax.swing.JOptionPane; + +public class ModuleModelGenerator extends FileGenerator { + private final ModelData modelData; + private final Project project; + private final ValidatorBundle validatorBundle; + private final CommonBundle commonBundle; + private final GetFirstClassOfFile getFirstClassOfFile; + private final DirectoryGenerator directoryGenerator; + private final FileFromTemplateGenerator fileFromTemplateGenerator; + + /** + * Generates new Model PHP Class based on provided data. + * + * @param modelData ModelData + * @param project Project + */ + public ModuleModelGenerator( + final ModelData modelData, + final Project project + ) { + super(project); + this.project = project; + this.modelData = modelData; + this.directoryGenerator = DirectoryGenerator.getInstance(); + this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project); + this.getFirstClassOfFile = GetFirstClassOfFile.getInstance(); + this.validatorBundle = new ValidatorBundle(); + this.commonBundle = new CommonBundle(); + } + + /** + * Generates model class. + * + * @param actionName Action name + * @return PsiFile + */ + public PsiFile generate(final String actionName) { + final PsiFile[] modelFiles = new PsiFile[1]; + + WriteCommandAction.runWriteCommandAction(project, () -> { + PhpClass model = GetPhpClassByFQN.getInstance(project).execute( + getModelFqn() + ); + + if (model != null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.alreadyExists", + "Model Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + model = createModelClass(actionName); + + if (model == null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.cantBeCreated", + "Model Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + modelFiles[0] = model.getContainingFile(); + }); + + return modelFiles[0]; + } + + /** + * Get module. + * + * @return String + */ + public String getModuleName() { + return modelData.getModuleName(); + } + + private String getModelFqn() { + return modelData.getFqn(); + } + + private PhpClass createModelClass(final String actionName) { + PsiDirectory parentDirectory = ModuleIndex.getInstance(project) + .getModuleDirectoryByModuleName(getModuleName()); + final PsiFile modelFile; + + parentDirectory = directoryGenerator.findOrCreateSubdirectory( + parentDirectory, ModelPhp.MODEL_DIRECTORY + ); + + final Properties attributes = getAttributes(); + modelFile = fileFromTemplateGenerator.generate( + new ModelPhp(modelData.getModelName()), + attributes, + parentDirectory, + actionName + ); + + if (modelFile == null) { + return null; + } + + return getFirstClassOfFile.execute((PhpFile) modelFile); + } + + protected void fillAttributes(final Properties attributes) { + attributes.setProperty("NAME", modelData.getModelName()); + attributes.setProperty("NAMESPACE", modelData.getNamespace()); + + attributes.setProperty("DB_NAME", modelData.getDbTableName()); + attributes.setProperty("RESOURCE_MODEL", PhpClassGeneratorUtil.getNameFromFqn( + modelData.getResourceModelFqn()) + ); + final List uses = getUses(); + + attributes.setProperty( + "EXTENDS", + PhpClassGeneratorUtil.getNameFromFqn(ModelPhp.ABSTRACT_MODEL) + ); + + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); + } + + private List getUses() { + return new ArrayList<>(Arrays.asList( + ModelPhp.ABSTRACT_MODEL, + modelData.getResourceModelFqn() + )); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java new file mode 100644 index 000000000..e1a7542ae --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java @@ -0,0 +1,177 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.command.WriteCommandAction; +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiDirectory; +import com.intellij.psi.PsiFile; +import com.jetbrains.php.lang.psi.PhpFile; +import com.jetbrains.php.lang.psi.elements.PhpClass; +import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData; +import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator; +import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; +import com.magento.idea.magento2plugin.bundles.CommonBundle; +import com.magento.idea.magento2plugin.bundles.ValidatorBundle; +import com.magento.idea.magento2plugin.indexes.ModuleIndex; +import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp; +import com.magento.idea.magento2plugin.magento.packages.File; +import com.magento.idea.magento2plugin.util.GetFirstClassOfFile; +import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import javax.swing.JOptionPane; + +public class ModuleResourceModelGenerator extends FileGenerator { + private final ResourceModelData resourceModelData; + private final Project project; + private final ValidatorBundle validatorBundle; + private final CommonBundle commonBundle; + private final GetFirstClassOfFile getFirstClassOfFile; + private final DirectoryGenerator directoryGenerator; + private final FileFromTemplateGenerator fileFromTemplateGenerator; + + /** + * Generates new Resource Model PHP Class based on provided data. + * + * @param resourceModelData ResourceModelData + * @param project Project + */ + public ModuleResourceModelGenerator( + final ResourceModelData resourceModelData, + final Project project + ) { + super(project); + this.project = project; + this.resourceModelData = resourceModelData; + this.directoryGenerator = DirectoryGenerator.getInstance(); + this.fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project); + this.getFirstClassOfFile = GetFirstClassOfFile.getInstance(); + this.validatorBundle = new ValidatorBundle(); + this.commonBundle = new CommonBundle(); + } + + /** + * Generates resource model class. + * + * @param actionName Action name + * @return PsiFile + */ + public PsiFile generate(final String actionName) { + final PsiFile[] resourceModelFiles = new PsiFile[1]; + + WriteCommandAction.runWriteCommandAction(project, () -> { + PhpClass resourceModel = GetPhpClassByFQN.getInstance(project).execute( + getResourceModelFqn() + ); + + if (resourceModel != null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.alreadyExists", + "Resource Model Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + resourceModel = createClass(actionName); + + if (resourceModel == null) { + final String errorMessage = this.validatorBundle.message( + "validator.file.cantBeCreated", + "Resource Model Class" + ); + JOptionPane.showMessageDialog( + null, + errorMessage, + commonBundle.message("common.error"), + JOptionPane.ERROR_MESSAGE + ); + + return; + } + + resourceModelFiles[0] = resourceModel.getContainingFile(); + }); + + return resourceModelFiles[0]; + } + + /** + * Get module. + * + * @return String + */ + public String getModuleName() { + return resourceModelData.getModuleName(); + } + + private String getResourceModelFqn() { + return resourceModelData.getFqn(); + } + + private PhpClass createClass(final String actionName) { + PsiDirectory parentDirectory = ModuleIndex.getInstance(project) + .getModuleDirectoryByModuleName(getModuleName()); + final PsiFile modelFile; + + final String[] resourceModelDirectories = ResourceModelPhp.RESOURCE_MODEL_DIRECTORY.split( + File.separator + ); + for (final String directory: resourceModelDirectories) { + parentDirectory = directoryGenerator.findOrCreateSubdirectory( + parentDirectory, directory + ); + } + + final Properties attributes = getAttributes(); + modelFile = fileFromTemplateGenerator.generate( + new ResourceModelPhp(resourceModelData.getResourceModelName()), + attributes, + parentDirectory, + actionName + ); + + if (modelFile == null) { + return null; + } + + return getFirstClassOfFile.execute((PhpFile) modelFile); + } + + protected void fillAttributes(final Properties attributes) { + attributes.setProperty("NAME", resourceModelData.getResourceModelName()); + attributes.setProperty("NAMESPACE", resourceModelData.getNamespace()); + + attributes.setProperty("DB_NAME", resourceModelData.getDbTableName()); + attributes.setProperty("ENTITY_ID_COLUMN", PhpClassGeneratorUtil.getNameFromFqn( + resourceModelData.getEntityIdColumn()) + ); + final List uses = getUses(); + + attributes.setProperty( + "EXTENDS", + PhpClassGeneratorUtil.getNameFromFqn(ResourceModelPhp.ABSTRACT_DB) + ); + + attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); + } + + private List getUses() { + return new ArrayList<>(Arrays.asList( + ResourceModelPhp.ABSTRACT_DB + )); + } +} diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java new file mode 100644 index 000000000..2ad8ab5ed --- /dev/null +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java @@ -0,0 +1,38 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator.util; + +import java.util.Collections; +import java.util.List; + +public final class PhpClassGeneratorUtil { + + private PhpClassGeneratorUtil() {} + + /** + * Format PHP class uses. + * + * @param uses List + * @return String + */ + public static String formatUses(final List uses) { + Collections.sort(uses); + + return String.join(",", uses); + } + + /** + * Fetches class name from a fully qualified name. + * + * @param fqn FQN + * @return String + */ + public static String getNameFromFqn(final String fqn) { + final String[] fqnArray = fqn.split("\\\\"); + + return fqnArray[fqnArray.length - 1]; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java b/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java new file mode 100644 index 000000000..8637b82df --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java @@ -0,0 +1,27 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +import com.intellij.lang.Language; +import com.jetbrains.php.lang.PhpLanguage; + +public abstract class AbstractPhpClass implements ModuleFileInterface { + private final String fileName; + + public AbstractPhpClass(String className) { + fileName = className.concat(".php"); + } + + @Override + public String getFileName() { + return this.fileName; + } + + @Override + public Language getLanguage() { + return PhpLanguage.INSTANCE; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java b/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java new file mode 100644 index 000000000..af83dffe5 --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java @@ -0,0 +1,20 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +public class CollectionPhp extends AbstractPhpClass { + public static final String ABSTRACT_COLLECTION + = "Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection"; + + public CollectionPhp(String className) { + super(className); + } + + @Override + public String getTemplate() { + return "Magento Module Collection Class"; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/FormButtonBlockPhp.java b/src/com/magento/idea/magento2plugin/magento/files/FormButtonBlockPhp.java index d68e08703..c85394060 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/FormButtonBlockPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/FormButtonBlockPhp.java @@ -5,10 +5,7 @@ package com.magento.idea.magento2plugin.magento.files; -import com.intellij.lang.Language; -import com.jetbrains.php.lang.PhpLanguage; - -public class FormButtonBlockPhp implements ModuleFileInterface { +public class FormButtonBlockPhp extends AbstractPhpClass { public static String template = "Magento Php Form Button Block Class"; public static String saveMethodTemplate = "Magento Php Form Button Block Type Save"; @@ -21,28 +18,13 @@ public class FormButtonBlockPhp implements ModuleFileInterface { public static final String TYPE_CUSTOM = "Custom"; public static final String DEFAULT_METHOD = "getButtonData"; - private String fileName; public FormButtonBlockPhp(final String className) { - this.setFileName(className.concat(".php")); - } - - @Override - public String getFileName() { - return this.fileName; + super(className); } @Override public String getTemplate() { return template; } - - @Override - public Language getLanguage() { - return PhpLanguage.INSTANCE; - } - - private void setFileName(final String filename) { - this.fileName = filename; - } } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java b/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java new file mode 100644 index 000000000..9002351ad --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java @@ -0,0 +1,21 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +public class ModelPhp extends AbstractPhpClass { + public static final String ABSTRACT_MODEL = + "Magento\\Framework\\Model\\AbstractModel"; + public static final String MODEL_DIRECTORY = "Model"; + + public ModelPhp(String className) { + super(className); + } + + @Override + public String getTemplate() { + return "Magento Module Model Class"; + } +} diff --git a/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java b/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java new file mode 100644 index 000000000..a58b4751d --- /dev/null +++ b/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java @@ -0,0 +1,21 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.magento.files; + +public class ResourceModelPhp extends AbstractPhpClass { + public static final String RESOURCE_MODEL_DIRECTORY = "Model/ResourceModel"; + public static final String ABSTRACT_DB + = "Magento\\Framework\\Model\\ResourceModel\\Db\\AbstractDb"; + + public ResourceModelPhp(final String className) { + super(className); + } + + @Override + public String getTemplate() { + return "Magento Module Resource Model Class"; + } +} diff --git a/testData/actions/generation/generator/ModuleCollectionGenerator/generateFile/TestCollection.php b/testData/actions/generation/generator/ModuleCollectionGenerator/generateFile/TestCollection.php new file mode 100644 index 000000000..92dd1e212 --- /dev/null +++ b/testData/actions/generation/generator/ModuleCollectionGenerator/generateFile/TestCollection.php @@ -0,0 +1,23 @@ +_init(TestModel::class, TestResource::class); + } +} diff --git a/testData/actions/generation/generator/ModuleModelGenerator/generateFile/TestModel.php b/testData/actions/generation/generator/ModuleModelGenerator/generateFile/TestModel.php new file mode 100644 index 000000000..39b7a860a --- /dev/null +++ b/testData/actions/generation/generator/ModuleModelGenerator/generateFile/TestModel.php @@ -0,0 +1,22 @@ +_init(TestResource::class); + } +} diff --git a/testData/actions/generation/generator/ModuleResourceModelGenerator/generateFile/TestResourceModel.php b/testData/actions/generation/generator/ModuleResourceModelGenerator/generateFile/TestResourceModel.php new file mode 100644 index 000000000..170b61c18 --- /dev/null +++ b/testData/actions/generation/generator/ModuleResourceModelGenerator/generateFile/TestResourceModel.php @@ -0,0 +1,21 @@ +_init('my_table', 'entity_id'); + } +} diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGeneratorTest.java new file mode 100644 index 000000000..905539824 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGeneratorTest.java @@ -0,0 +1,44 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.CollectionData; + +public class ModuleCollectionGeneratorTest extends BaseGeneratorTestCase { + /** + * Test generation of collection file. + */ + public void testGenerateFile() { + final Project project = myFixture.getProject(); + final CollectionData collectionFileData = new CollectionData( + "Foo_Bar", + "my_table", + "TestModel", + "TestCollection", + "Foo\\Bar\\Model\\ResourceModel\\TestModel\\TestCollection", + "Model/ResourceModel/TestModel", + "Foo\\Bar\\Model\\ResourceModel\\TestModel", + "TestResource", + "Foo\\Bar\\Model\\ResourceModel\\TestModel\\TestResource", + "Foo\\Bar\\Model\\TestModel" + ); + final ModuleCollectionGenerator generator = new ModuleCollectionGenerator( + collectionFileData, + project + ); + final PsiFile controllerFile = generator.generate("test"); + final String filePath = this.getFixturePath("TestCollection.php"); + final PsiFile expectedFile = myFixture.configureByFile(filePath); + + assertGeneratedFileIsCorrect( + expectedFile, + "src/app/code/Foo/Bar/Model/ResourceModel/TestModel", + controllerFile + ); + } +} diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGeneratorTest.java new file mode 100644 index 000000000..8a51b9274 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGeneratorTest.java @@ -0,0 +1,41 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.ModelData; + +public class ModuleModelGeneratorTest extends BaseGeneratorTestCase { + /** + * Test generation of model file. + */ + public void testGenerateFile() { + final Project project = myFixture.getProject(); + final ModelData modelData = new ModelData( + "Foo_Bar", + "my_table", + "TestModel", + "TestResource", + "Foo\\Bar\\Model\\TestModel", + "Foo\\Bar\\Model", + "Foo\\Bar\\Model\\ResourceModel\\TestResource" + ); + final ModuleModelGenerator generator = new ModuleModelGenerator( + modelData, + project + ); + final PsiFile controllerFile = generator.generate("test"); + final String filePath = this.getFixturePath("TestModel.php"); + final PsiFile expectedFile = myFixture.configureByFile(filePath); + + assertGeneratedFileIsCorrect( + expectedFile, + "src/app/code/Foo/Bar/Model", + controllerFile + ); + } +} diff --git a/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGeneratorTest.java b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGeneratorTest.java new file mode 100644 index 000000000..ebf707249 --- /dev/null +++ b/tests/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGeneratorTest.java @@ -0,0 +1,40 @@ +/* + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ + +package com.magento.idea.magento2plugin.actions.generation.generator; + +import com.intellij.openapi.project.Project; +import com.intellij.psi.PsiFile; +import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData; + +public class ModuleResourceModelGeneratorTest extends BaseGeneratorTestCase { + /** + * Test generation of resource model file. + */ + public void testGenerateFile() { + final Project project = myFixture.getProject(); + final ResourceModelData resourceModelData = new ResourceModelData( + "Foo_Bar", + "my_table", + "TestResourceModel", + "entity_id", + "Foo\\Bar\\Model\\ResourceModel", + "Foo\\Bar\\Model\\ResourceModel\\TestModel" + ); + final ModuleResourceModelGenerator generator = new ModuleResourceModelGenerator( + resourceModelData, + project + ); + final PsiFile controllerFile = generator.generate("test"); + final String filePath = this.getFixturePath("TestResourceModel.php"); + final PsiFile expectedFile = myFixture.configureByFile(filePath); + + assertGeneratedFileIsCorrect( + expectedFile, + "src/app/code/Foo/Bar/Model/ResourceModel", + controllerFile + ); + } +} From 0c0f5ae6bf78a40a356498844b5ecd2ee2b90265 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Fri, 13 Nov 2020 20:48:59 +0200 Subject: [PATCH 2/3] Static fixes --- .../magento2plugin/actions/generation/data/CollectionData.java | 1 + .../idea/magento2plugin/magento/files/AbstractPhpClass.java | 2 +- .../idea/magento2plugin/magento/files/CollectionPhp.java | 2 +- src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java | 2 +- 4 files changed, 4 insertions(+), 3 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java b/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java index 75fdfa0ce..0ada17e3f 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/data/CollectionData.java @@ -5,6 +5,7 @@ package com.magento.idea.magento2plugin.actions.generation.data; +@SuppressWarnings({"PMD.ExcessiveParameterList"}) public class CollectionData { private final String moduleName; private final String dbTableName; diff --git a/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java b/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java index 8637b82df..bba99ba24 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java +++ b/src/com/magento/idea/magento2plugin/magento/files/AbstractPhpClass.java @@ -11,7 +11,7 @@ public abstract class AbstractPhpClass implements ModuleFileInterface { private final String fileName; - public AbstractPhpClass(String className) { + public AbstractPhpClass(final String className) { fileName = className.concat(".php"); } diff --git a/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java b/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java index af83dffe5..62a1efcb5 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java @@ -9,7 +9,7 @@ public class CollectionPhp extends AbstractPhpClass { public static final String ABSTRACT_COLLECTION = "Magento\\Framework\\Model\\ResourceModel\\Db\\Collection\\AbstractCollection"; - public CollectionPhp(String className) { + public CollectionPhp(final String className) { super(className); } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java b/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java index 9002351ad..4d94a7679 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java @@ -10,7 +10,7 @@ public class ModelPhp extends AbstractPhpClass { "Magento\\Framework\\Model\\AbstractModel"; public static final String MODEL_DIRECTORY = "Model"; - public ModelPhp(String className) { + public ModelPhp(final String className) { super(className); } From 9f5d61426c93185a54ca50fe3d22e8bc45ca60ce Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Sat, 21 Nov 2020 21:04:51 +0200 Subject: [PATCH 3/3] Renamed templates --- resources/META-INF/plugin.xml | 6 +++--- ...lection Class.php.ft => Magento Collection Class.php.ft} | 0 ...ion Class.php.html => Magento Collection Class.php.html} | 0 ...Module Model Class.php.ft => Magento Model Class.php.ft} | 0 ...le Model Class.php.html => Magento Model Class.php.html} | 0 ...del Class.php.ft => Magento Resource Model Class.php.ft} | 0 ...Class.php.html => Magento Resource Model Class.php.html} | 0 .../idea/magento2plugin/magento/files/CollectionPhp.java | 2 +- .../magento/idea/magento2plugin/magento/files/ModelPhp.java | 2 +- .../idea/magento2plugin/magento/files/ResourceModelPhp.java | 2 +- 10 files changed, 6 insertions(+), 6 deletions(-) rename resources/fileTemplates/internal/{Magento Module Collection Class.php.ft => Magento Collection Class.php.ft} (100%) rename resources/fileTemplates/internal/{Magento Module Collection Class.php.html => Magento Collection Class.php.html} (100%) rename resources/fileTemplates/internal/{Magento Module Model Class.php.ft => Magento Model Class.php.ft} (100%) rename resources/fileTemplates/internal/{Magento Module Model Class.php.html => Magento Model Class.php.html} (100%) rename resources/fileTemplates/internal/{Magento Module Resource Model Class.php.ft => Magento Resource Model Class.php.ft} (100%) rename resources/fileTemplates/internal/{Magento Module Resource Model Class.php.html => Magento Resource Model Class.php.html} (100%) diff --git a/resources/META-INF/plugin.xml b/resources/META-INF/plugin.xml index b51bfc36d..2eee35173 100644 --- a/resources/META-INF/plugin.xml +++ b/resources/META-INF/plugin.xml @@ -211,9 +211,9 @@ - - - + + + diff --git a/resources/fileTemplates/internal/Magento Module Collection Class.php.ft b/resources/fileTemplates/internal/Magento Collection Class.php.ft similarity index 100% rename from resources/fileTemplates/internal/Magento Module Collection Class.php.ft rename to resources/fileTemplates/internal/Magento Collection Class.php.ft diff --git a/resources/fileTemplates/internal/Magento Module Collection Class.php.html b/resources/fileTemplates/internal/Magento Collection Class.php.html similarity index 100% rename from resources/fileTemplates/internal/Magento Module Collection Class.php.html rename to resources/fileTemplates/internal/Magento Collection Class.php.html diff --git a/resources/fileTemplates/internal/Magento Module Model Class.php.ft b/resources/fileTemplates/internal/Magento Model Class.php.ft similarity index 100% rename from resources/fileTemplates/internal/Magento Module Model Class.php.ft rename to resources/fileTemplates/internal/Magento Model Class.php.ft diff --git a/resources/fileTemplates/internal/Magento Module Model Class.php.html b/resources/fileTemplates/internal/Magento Model Class.php.html similarity index 100% rename from resources/fileTemplates/internal/Magento Module Model Class.php.html rename to resources/fileTemplates/internal/Magento Model Class.php.html diff --git a/resources/fileTemplates/internal/Magento Module Resource Model Class.php.ft b/resources/fileTemplates/internal/Magento Resource Model Class.php.ft similarity index 100% rename from resources/fileTemplates/internal/Magento Module Resource Model Class.php.ft rename to resources/fileTemplates/internal/Magento Resource Model Class.php.ft diff --git a/resources/fileTemplates/internal/Magento Module Resource Model Class.php.html b/resources/fileTemplates/internal/Magento Resource Model Class.php.html similarity index 100% rename from resources/fileTemplates/internal/Magento Module Resource Model Class.php.html rename to resources/fileTemplates/internal/Magento Resource Model Class.php.html diff --git a/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java b/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java index 62a1efcb5..bbc55b149 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/CollectionPhp.java @@ -15,6 +15,6 @@ public CollectionPhp(final String className) { @Override public String getTemplate() { - return "Magento Module Collection Class"; + return "Magento Collection Class"; } } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java b/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java index 4d94a7679..a2ffec498 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ModelPhp.java @@ -16,6 +16,6 @@ public ModelPhp(final String className) { @Override public String getTemplate() { - return "Magento Module Model Class"; + return "Magento Model Class"; } } diff --git a/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java b/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java index a58b4751d..ee47b6c37 100644 --- a/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java +++ b/src/com/magento/idea/magento2plugin/magento/files/ResourceModelPhp.java @@ -16,6 +16,6 @@ public ResourceModelPhp(final String className) { @Override public String getTemplate() { - return "Magento Module Resource Model Class"; + return "Magento Resource Model Class"; } }