From f7f8f4b655f81478f8102cd733d6653333ac5c03 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 7 May 2021 15:40:27 +0300 Subject: [PATCH 1/2] Simplified file properties definition for the php file generation --- .../generator/DataModelGenerator.java | 23 +++------ .../DeleteEntityByIdCommandGenerator.java | 19 +++---- .../DeleteEntityControllerFileGenerator.java | 22 +++------ .../generator/EditEntityActionGenerator.java | 22 +++------ .../generator/EntityDataMapperGenerator.java | 20 ++------ .../FormGenericButtonBlockGenerator.java | 21 +++----- .../generator/GetListQueryModelGenerator.java | 19 ++----- .../GridActionColumnFileGenerator.java | 24 +++------ .../generator/IndexActionGenerator.java | 24 +++------ .../generator/ModuleCollectionGenerator.java | 25 +++------- .../generator/ModuleModelGenerator.java | 28 +++-------- .../ModuleResourceModelGenerator.java | 24 +++------ ...ewActionEntityControllerFileGenerator.java | 22 +++------ .../generator/PhpFileGenerator.java | 26 ++++++++++ .../generator/SaveEntityCommandGenerator.java | 27 +++------- .../SaveEntityControllerFileGenerator.java | 21 +++----- .../UiComponentDataProviderGenerator.java | 22 +++------ .../php/WebApiInterfaceGenerator.java | 15 +++--- .../generator/util/PhpClassGeneratorUtil.java | 13 +++++ .../generator/util/PhpClassTypesBuilder.java | 49 +++++++++++++++++-- .../idea/magento2plugin/util/RegExUtil.java | 3 +- 21 files changed, 200 insertions(+), 269 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DataModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DataModelGenerator.java index 331ae6cb6..6f7561bb5 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DataModelGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DataModelGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.DataModelData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.DataModelFile; import com.magento.idea.magento2plugin.magento.files.DataModelInterfaceFile; @@ -60,29 +58,20 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("NAME", data.getName()) - .appendProperty("PROPERTIES", data.getProperties()) - .appendProperty("HASINTERFACE", Boolean.toString(data.hasInterface())) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("NAME", data.getName(), false) + .append("PROPERTIES", data.getProperties(), false) + .append("HASINTERFACE", Boolean.toString(data.hasInterface()), false) .append("EXTENDS", DataModelFile.DATA_OBJECT); if (data.hasInterface()) { - phpClassTypesBuilder.append( + typesBuilder.append( "IMPLEMENTS", new DataModelInterfaceFile( data.getModuleName(), data.getInterfaceName() ).getClassFqn()); } - - phpClassTypesBuilder.mergeProperties(attributes); - - attributes.setProperty( - "USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) - ); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityByIdCommandGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityByIdCommandGenerator.java index bb9b80a9d..24ada3467 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityByIdCommandGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityByIdCommandGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityByIdCommandData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.ModelFile; import com.magento.idea.magento2plugin.magento.files.ResourceModelFile; @@ -63,18 +61,17 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); final ModelFile modelFile = new ModelFile(data.getModuleName(), data.getModelName()); final String modelType = modelFile.getClassFqn(); final String modelFactoryType = modelType.concat("Factory"); final ResourceModelFile resourceFile = new ResourceModelFile(data.getModuleName(), data.getResourceModelName()); - phpClassTypesBuilder - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("CLASS_NAME", DeleteEntityByIdCommandFile.CLASS_NAME) - .appendProperty("ENTITY_ID", data.getEntityId()) + typesBuilder + .append("ENTITY_NAME", data.getEntityName(), false) + .append("NAMESPACE", file.getNamespace(), false) + .append("CLASS_NAME", DeleteEntityByIdCommandFile.CLASS_NAME, false) + .append("ENTITY_ID", data.getEntityId(), false) .append("Exception", "Exception") .append("COULD_NOT_DELETE", ExceptionType.COULD_NOT_DELETE.getType()) .append("NO_SUCH_ENTITY_EXCEPTION", @@ -82,10 +79,6 @@ protected void fillAttributes(final @NotNull Properties attributes) { .append("LOGGER", FrameworkLibraryType.LOGGER.getType()) .append("MODEL", modelType) .append("MODEL_FACTORY", modelFactoryType) - .append("RESOURCE", resourceFile.getClassFqn()) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("RESOURCE", resourceFile.getClassFqn()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityControllerFileGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityControllerFileGenerator.java index 0832fc370..153892910 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityControllerFileGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/DeleteEntityControllerFileGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityControllerFileData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.actions.DeleteActionFile; import com.magento.idea.magento2plugin.magento.files.commands.DeleteEntityByIdCommandFile; @@ -64,14 +62,12 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", DeleteActionFile.CLASS_NAME) - .appendProperty("ADMIN_RESOURCE", data.getAcl()) - .appendProperty("ENTITY_ID", data.getEntityId()) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", DeleteActionFile.CLASS_NAME, false) + .append("ADMIN_RESOURCE", data.getAcl(), false) + .append("ENTITY_ID", data.getEntityId(), false) .append("DELETE_COMMAND", new DeleteEntityByIdCommandFile( data.getModuleName(), @@ -86,10 +82,6 @@ protected void fillAttributes(final @NotNull Properties attributes) { .append("IMPLEMENTS_GET", HttpMethod.GET.getInterfaceFqn()) .append("NO_SUCH_ENTITY_EXCEPTION", ExceptionType.NO_SUCH_ENTITY_EXCEPTION.getType()) - .append("COULD_NOT_DELETE", ExceptionType.COULD_NOT_DELETE.getType()) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("COULD_NOT_DELETE", ExceptionType.COULD_NOT_DELETE.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/EditEntityActionGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/EditEntityActionGenerator.java index c6c5d4b78..905110f49 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/EditEntityActionGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/EditEntityActionGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.EditEntityActionData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.actions.EditActionFile; import com.magento.idea.magento2plugin.magento.packages.HttpMethod; @@ -62,22 +60,16 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", file.getClassName()) - .appendProperty("ADMIN_RESOURCE", data.getAcl()) - .appendProperty("MENU_IDENTIFIER", data.getMenu()) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", file.getClassName(), false) + .append("ADMIN_RESOURCE", data.getAcl(), false) + .append("MENU_IDENTIFIER", data.getMenu(), false) .append("EXTENDS", BackendModuleType.EXTENDS.getType()) .append("IMPLEMENTS", HttpMethod.GET.getInterfaceFqn()) .append("RESULT_INTERFACE", FrameworkLibraryType.RESULT_INTERFACE.getType()) .append("RESULT_FACTORY", FrameworkLibraryType.RESULT_FACTORY.getType()) - .append("RESULT_PAGE", BackendModuleType.RESULT_PAGE.getType()) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("RESULT_PAGE", BackendModuleType.RESULT_PAGE.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/EntityDataMapperGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/EntityDataMapperGenerator.java index 2149af4f4..0deb41025 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/EntityDataMapperGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/EntityDataMapperGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.DataModelFile; import com.magento.idea.magento2plugin.magento.files.DataModelInterfaceFile; @@ -63,8 +61,6 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - final ModelFile modelFile = new ModelFile(data.getModuleName(), data.getModelName()); final DataModelFile dtoFile = new DataModelFile(data.getDtoName(), data.getModuleName()); final DataModelInterfaceFile dtoInterfaceFile = @@ -77,20 +73,14 @@ protected void fillAttributes(final @NotNull Properties attributes) { dtoType = dtoFile.getClassFqn(); } - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", file.getClassName()) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", file.getClassName(), false) .append("DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getType()) .append("DTO_TYPE", dtoType) .append("MAGENTO_MODEL_TYPE", modelFile.getClassFqn()) .append("DTO_FACTORY", dtoType.concat("Factory")) - .append("ABSTRACT_COLLECTION", FrameworkLibraryType.ABSTRACT_COLLECTION.getType()) - .mergeProperties(attributes); - - attributes.setProperty( - "USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) - ); + .append("ABSTRACT_COLLECTION", FrameworkLibraryType.ABSTRACT_COLLECTION.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/FormGenericButtonBlockGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/FormGenericButtonBlockGenerator.java index cdef815c5..64c875a13 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/FormGenericButtonBlockGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/FormGenericButtonBlockGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.FormGenericButtonBlockData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.FormGenericButtonBlockFile; import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType; @@ -63,22 +61,17 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); final String entityIdGetter = "get" + Arrays.stream(data.getEntityId().split("_")) .map(s -> s.substring(0, 1).toUpperCase(Locale.getDefault()) + s.substring(1)) .collect(Collectors.joining()); - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", FormGenericButtonBlockFile.CLASS_NAME) - .appendProperty("ENTITY_ID", data.getEntityId()) - .appendProperty("ENTITY_ID_GETTER", entityIdGetter) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", FormGenericButtonBlockFile.CLASS_NAME, false) + .append("ENTITY_ID", data.getEntityId(), false) + .append("ENTITY_ID_GETTER", entityIdGetter, false) .append("CONTEXT", FormGenericButtonBlockFile.CONTEXT) - .append("URL", FrameworkLibraryType.URL.getType()) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("URL", FrameworkLibraryType.URL.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java index c91908286..2a660bbc7 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/GetListQueryModelGenerator.java @@ -8,8 +8,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData; import com.magento.idea.magento2plugin.actions.generation.generator.util.NamespaceBuilder; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.CollectionModelFile; import com.magento.idea.magento2plugin.magento.files.EntityDataMapperFile; @@ -63,7 +61,6 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); final CollectionModelFile collectionModelFile = new CollectionModelFile( data.getModuleName(), @@ -73,10 +70,10 @@ protected void fillAttributes(final @NotNull Properties attributes) { final NamespaceBuilder collectionNamespace = collectionModelFile.getNamespaceBuilder(); - phpClassTypesBuilder - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("CLASS_NAME", GetListQueryFile.CLASS_NAME) + typesBuilder + .append("ENTITY_NAME", data.getEntityName(), false) + .append("NAMESPACE", file.getNamespace(), false) + .append("CLASS_NAME", GetListQueryFile.CLASS_NAME, false) .append( "ENTITY_COLLECTION_TYPE", collectionNamespace.getClassFqn() @@ -111,12 +108,6 @@ protected void fillAttributes(final @NotNull Properties attributes) { .append( "SEARCH_RESULT_FACTORY_TYPE", FrameworkLibraryType.SEARCH_RESULT.getFactory() - ) - .mergeProperties(attributes); - - attributes.setProperty( - "USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) - ); + ); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/GridActionColumnFileGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/GridActionColumnFileGenerator.java index 386984ddd..795a7e7a0 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/GridActionColumnFileGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/GridActionColumnFileGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.GridActionColumnData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.GridActionColumnFile; import com.magento.idea.magento2plugin.magento.packages.code.FrameworkLibraryType; @@ -60,22 +58,16 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("ENTITY_ID", data.getEntityIdColumn()) - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("CLASS_NAME", file.getClassName()) - .appendProperty("EDIT_URL_PATH", data.getEditUrlPath()) - .appendProperty("DELETE_URL_PATH", data.getDeleteUrlPath()) + typesBuilder + .append("ENTITY_NAME", data.getEntityName(), false) + .append("ENTITY_ID", data.getEntityIdColumn(), false) + .append("NAMESPACE", file.getNamespace(), false) + .append("CLASS_NAME", file.getClassName(), false) + .append("EDIT_URL_PATH", data.getEditUrlPath(), false) + .append("DELETE_URL_PATH", data.getDeleteUrlPath(), false) .append("PARENT_CLASS", GridActionColumnFile.PARENT_CLASS) .append("URL", FrameworkLibraryType.URL.getType()) .append("CONTEXT", GridActionColumnFile.CONTEXT) - .append("UI_COMPONENT_FACTORY", GridActionColumnFile.UI_COMPONENT_FACTORY) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("UI_COMPONENT_FACTORY", GridActionColumnFile.UI_COMPONENT_FACTORY); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/IndexActionGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/IndexActionGenerator.java index 300f3fcbc..bcd1e4dea 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/IndexActionGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/IndexActionGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.IndexActionData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.actions.IndexActionFile; import com.magento.idea.magento2plugin.magento.packages.HttpMethod; @@ -62,24 +60,16 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", IndexActionFile.CLASS_NAME) - .appendProperty("ACL", data.getAcl()) - .appendProperty("MENU", data.getMenu()) - .appendProperty("NAMESPACE", file.getNamespace()) + typesBuilder + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", IndexActionFile.CLASS_NAME, false) + .append("ACL", data.getAcl(), false) + .append("MENU", data.getMenu(), false) + .append("NAMESPACE", file.getNamespace(), false) .append("PARENT_CLASS_NAME", BackendModuleType.EXTENDS.getType()) .append("HTTP_GET_METHOD", HttpMethod.GET.getInterfaceFqn()) .append("RESULT", FrameworkLibraryType.RESULT_INTERFACE.getType()) .append("RESPONSE", FrameworkLibraryType.RESPONSE_INTERFACE.getType()) - .append("RESULT_FACTORY", FrameworkLibraryType.RESULT_FACTORY.getType()) - .mergeProperties(attributes); - - attributes.setProperty( - "USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) - ); + .append("RESULT_FACTORY", FrameworkLibraryType.RESULT_FACTORY.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java index 64c6c7eaf..2718b0093 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleCollectionGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.CollectionData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.CollectionModelFile; import com.magento.idea.magento2plugin.magento.files.ModelFile; @@ -65,17 +63,16 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - final ResourceModelFile resourceModelFile = new ResourceModelFile(data.getModuleName(), data.getResourceModelName()); final ModelFile modelFile = new ModelFile(data.getModuleName(), data.getModelName()); - phpClassTypesBuilder.appendProperty("NAME", data.getCollectionName()) - .appendProperty("NAMESPACE", file.getNamespaceBuilder().getNamespace()) - .appendProperty("DB_NAME", data.getDbTableName()) - .appendProperty("MODEL", data.getModelName()) - .appendProperty("RESOURCE_MODEL", data.getResourceModelName()) + typesBuilder + .append("NAME", data.getCollectionName(), false) + .append("NAMESPACE", file.getNamespaceBuilder().getNamespace(), false) + .append("DB_NAME", data.getDbTableName(), false) + .append("MODEL", data.getModelName(), false) + .append("RESOURCE_MODEL", data.getResourceModelName(), false) .append("EXTENDS", CollectionModelFile.ABSTRACT_COLLECTION) .append( "RESOURCE_MODEL", @@ -86,14 +83,6 @@ protected void fillAttributes(final @NotNull Properties attributes) { "MODEL", modelFile.getClassFqn(), ModelFile.ALIAS - ) - .mergeProperties(attributes); - - attributes.setProperty( - "USES", - PhpClassGeneratorUtil.formatUses( - phpClassTypesBuilder.getUses() - ) - ); + ); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java index e31252d08..0d6cc30a3 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleModelGenerator.java @@ -8,7 +8,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.ModelData; import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.ModelFile; import com.magento.idea.magento2plugin.magento.files.ResourceModelFile; @@ -60,30 +59,19 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); final ResourceModelFile resourceModelFile = new ResourceModelFile(data.getModuleName(), data.getResourceName()); - phpClassTypesBuilder - .appendProperty("NAME", data.getModelName()) - .appendProperty( - "NAMESPACE", - file.getNamespace() - ) - .appendProperty("DB_NAME", data.getDbTableName()) + typesBuilder + .append("NAME", data.getModelName(), false) + .append("NAMESPACE", file.getNamespace(), false) + .append("DB_NAME", data.getDbTableName(), false) + .append("RESOURCE_MODEL", resourceModelFile.getClassFqn(), ResourceModelFile.ALIAS) .append( - "RESOURCE_MODEL", - resourceModelFile.getClassFqn(), - ResourceModelFile.ALIAS - ) - .appendProperty( "EXTENDS", - PhpClassGeneratorUtil.getNameFromFqn(ModelFile.ABSTRACT_MODEL) + PhpClassGeneratorUtil.getNameFromFqn(ModelFile.ABSTRACT_MODEL), + false ) - .append("ABSTRACT_MODEL", ModelFile.ABSTRACT_MODEL) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("ABSTRACT_MODEL", ModelFile.ABSTRACT_MODEL); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java index b885508a2..877b2af40 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/ModuleResourceModelGenerator.java @@ -7,11 +7,8 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.ResourceModelFile; -import java.util.List; import java.util.Properties; import org.jetbrains.annotations.NotNull; @@ -60,20 +57,11 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAME", data.getResourceModelName()) - .appendProperty( - "NAMESPACE", - file.getNamespace() - ) - .appendProperty("DB_NAME", data.getDbTableName()) - .appendProperty("ENTITY_ID_COLUMN", data.getEntityIdColumn()) - .append("EXTENDS", ResourceModelFile.ABSTRACT_DB) - .mergeProperties(attributes); - - final List uses = phpClassTypesBuilder.getUses(); - attributes.setProperty("USES", PhpClassGeneratorUtil.formatUses(uses)); + typesBuilder + .append("NAME", data.getResourceModelName(), false) + .append("NAMESPACE", file.getNamespace(), false) + .append("DB_NAME", data.getDbTableName(), false) + .append("ENTITY_ID_COLUMN", data.getEntityIdColumn(), false) + .append("EXTENDS", ResourceModelFile.ABSTRACT_DB); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/NewActionEntityControllerFileGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/NewActionEntityControllerFileGenerator.java index 21444054d..419bdfac0 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/NewActionEntityControllerFileGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/NewActionEntityControllerFileGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.NewActionEntityControllerFileData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.actions.NewActionFile; import com.magento.idea.magento2plugin.magento.packages.HttpMethod; @@ -58,22 +56,16 @@ protected AbstractPhpFile initFile() { @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAMESPACE", data.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", file.getClassName()) - .appendProperty("ADMIN_RESOURCE", data.getAcl()) - .appendProperty("MENU_IDENTIFIER", data.getMenu()) + typesBuilder + .append("NAMESPACE", data.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", file.getClassName(), false) + .append("ADMIN_RESOURCE", data.getAcl(), false) + .append("MENU_IDENTIFIER", data.getMenu(), false) .append("EXTENDS", BackendModuleType.EXTENDS.getType()) .append("IMPLEMENTS", HttpMethod.GET.getInterfaceFqn()) .append("RESULT_INTERFACE", FrameworkLibraryType.RESULT_INTERFACE.getType()) .append("RESULT_FACTORY", FrameworkLibraryType.RESULT_FACTORY.getType()) - .append("RESULT_PAGE", BackendModuleType.RESULT_PAGE.getType()) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("RESULT_PAGE", BackendModuleType.RESULT_PAGE.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java index e3b81f08c..f34e2bfb5 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java @@ -11,17 +11,24 @@ import com.jetbrains.php.lang.psi.elements.PhpClass; 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.actions.generation.generator.util.PhpClassTypesBuilder; 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.AbstractPhpFile; import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; import javax.swing.JOptionPane; +import java.util.Properties; import org.jetbrains.annotations.NotNull; public abstract class PhpFileGenerator extends FileGenerator { + private static final String IMPORT_PROPERTY_NAME = "USES"; + protected AbstractPhpFile file; + // Util that simplifies file properties definition. + protected final PhpClassTypesBuilder typesBuilder; private final ValidatorBundle validatorBundle; private final CommonBundle commonBundle; private final FileFromTemplateGenerator fileFromTemplateGenerator; @@ -46,6 +53,7 @@ public PhpFileGenerator( fileFromTemplateGenerator = new FileFromTemplateGenerator(project); directoryGenerator = DirectoryGenerator.getInstance(); moduleIndex = new ModuleIndex(project); + typesBuilder = new PhpClassTypesBuilder(); } /** @@ -143,4 +151,22 @@ protected AbstractPhpFile getFile() { return file; } + + @Override + protected Properties getAttributes() { + final @NotNull Properties attributes = super.getAttributes(); + + if (typesBuilder.hasProperties()) { + typesBuilder.mergeProperties(attributes); + } + + if (!typesBuilder.getUses().isEmpty() && !typesBuilder.hasProperty(IMPORT_PROPERTY_NAME)) { + attributes.setProperty( + IMPORT_PROPERTY_NAME, + PhpClassGeneratorUtil.formatUses(typesBuilder.getUses()) + ); + } + + return attributes; + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGenerator.java index 7cd7a0506..301d46265 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityCommandGenerator.java @@ -8,8 +8,6 @@ import com.google.common.base.CaseFormat; import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityCommandData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.DataModelFile; import com.magento.idea.magento2plugin.magento.files.DataModelInterfaceFile; @@ -66,12 +64,10 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", SaveEntityCommandFile.CLASS_NAME) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", SaveEntityCommandFile.CLASS_NAME, false) .append("EXCEPTION", "Exception") .append("DATA_OBJECT", FrameworkLibraryType.DATA_OBJECT.getType()) .append("COULD_NOT_SAVE", ExceptionType.COULD_NOT_SAVE.getType()) @@ -89,29 +85,22 @@ protected void fillAttributes(final @NotNull Properties attributes) { final DataModelInterfaceFile dataModelInterfaceFile = new DataModelInterfaceFile(data.getModuleName(), data.getDtoInterfaceName()); final String dtoType = dataModelInterfaceFile.getClassFqn(); - phpClassTypesBuilder.append("DTO", dtoType); + typesBuilder.append("DTO", dtoType); } else { final DataModelFile dataModelFile = new DataModelFile(data.getModuleName(), data.getDtoName()); final String dtoType = dataModelFile.getClassFqn(); - phpClassTypesBuilder.append("DTO", dtoType); + typesBuilder.append("DTO", dtoType); } final String dtoProperty = CaseFormat.UPPER_CAMEL.to( CaseFormat.LOWER_CAMEL, data.getEntityName() ); - phpClassTypesBuilder - .appendProperty("DTO_PROPERTY", dtoProperty) + typesBuilder + .append("DTO_PROPERTY", dtoProperty, false) .append("MODEL", modelType) .append("MODEL_FACTORY", modelFactoryType) .append("RESOURCE", resourceType); - - phpClassTypesBuilder.mergeProperties(attributes); - - attributes.setProperty( - "USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses()) - ); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityControllerFileGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityControllerFileGenerator.java index 5acd1477a..3b658d459 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityControllerFileGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/SaveEntityControllerFileGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityControllerFileData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.DataModelFile; import com.magento.idea.magento2plugin.magento.files.DataModelInterfaceFile; @@ -65,7 +63,6 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); String dtoType; if (data.isDtoWithInterface()) { @@ -78,12 +75,12 @@ protected void fillAttributes(final @NotNull Properties attributes) { dtoType = dataModelFile.getClassFqn(); } - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("ENTITY_NAME", data.getEntityName()) - .appendProperty("CLASS_NAME", SaveActionFile.CLASS_NAME) - .appendProperty("ENTITY_ID", data.getEntityId()) - .appendProperty("ADMIN_RESOURCE", data.getAcl()) + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("ENTITY_NAME", data.getEntityName(), false) + .append("CLASS_NAME", SaveActionFile.CLASS_NAME, false) + .append("ENTITY_ID", data.getEntityId(), false) + .append("ADMIN_RESOURCE", data.getAcl(), false) .append("IMPLEMENTS", HttpMethod.POST.getInterfaceFqn()) .append("DATA_PERSISTOR", FrameworkLibraryType.DATA_PERSISTOR.getType()) .append("EXTENDS", BackendModuleType.EXTENDS.getType()) @@ -99,10 +96,6 @@ protected void fillAttributes(final @NotNull Properties attributes) { .append("COULD_NOT_SAVE", SaveActionFile.COULD_NOT_SAVE) .append("CONTEXT", BackendModuleType.CONTEXT.getType()) .append("RESPONSE_INTERFACE", FrameworkLibraryType.RESPONSE_INTERFACE.getType()) - .append("RESULT_INTERFACE", FrameworkLibraryType.RESULT_INTERFACE.getType()) - .mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); + .append("RESULT_INTERFACE", FrameworkLibraryType.RESULT_INTERFACE.getType()); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java index d400439cb..cf8e04309 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/UiComponentDataProviderGenerator.java @@ -7,8 +7,6 @@ import com.intellij.openapi.project.Project; import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderFile; import com.magento.idea.magento2plugin.magento.files.queries.GetListQueryFile; @@ -67,19 +65,17 @@ protected AbstractPhpFile initFile() { */ @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); - - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("CLASS_NAME", data.getName()) - .appendProperty("HAS_GET_LIST_QUERY", "false") + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("CLASS_NAME", data.getName(), false) + .append("HAS_GET_LIST_QUERY", "false", false) .append("EXTENDS", UiComponentDataProviderFile.DEFAULT_DATA_PROVIDER); if (data.getEntityIdFieldName() != null && data.getEntityName() != null) { - phpClassTypesBuilder.appendProperty("ENTITY_ID", data.getEntityIdFieldName()); + typesBuilder.append("ENTITY_ID", data.getEntityIdFieldName(), false); - phpClassTypesBuilder - .appendProperty("HAS_GET_LIST_QUERY", "true") + typesBuilder + .append("HAS_GET_LIST_QUERY", "true", false) .append( "GET_LIST_QUERY_TYPE", new GetListQueryFile(moduleName, data.getEntityName()).getClassFqn() @@ -92,9 +88,5 @@ protected void fillAttributes(final @NotNull Properties attributes) { .append("SEARCH_RESULT_FACTORY", UiComponentDataProviderFile.SEARCH_RESULT_FACTORY); } - phpClassTypesBuilder.mergeProperties(attributes); - - attributes.setProperty("USES", - PhpClassGeneratorUtil.formatUses(phpClassTypesBuilder.getUses())); } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/php/WebApiInterfaceGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/php/WebApiInterfaceGenerator.java index 71625366d..8d12a2256 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/php/WebApiInterfaceGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/php/WebApiInterfaceGenerator.java @@ -11,7 +11,6 @@ import com.magento.idea.magento2plugin.actions.generation.data.php.WebApiInterfaceData; import com.magento.idea.magento2plugin.actions.generation.generator.PhpFileGenerator; import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassGeneratorUtil; -import com.magento.idea.magento2plugin.actions.generation.generator.util.PhpClassTypesBuilder; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.magento.files.WebApiInterfaceFile; import com.magento.idea.magento2plugin.util.php.PhpTypeMetadataParserUtil; @@ -75,7 +74,6 @@ protected void onFileGenerated(final PsiFile generatedFile) { @Override protected void fillAttributes(final @NotNull Properties attributes) { - final PhpClassTypesBuilder phpClassTypesBuilder = new PhpClassTypesBuilder(); final StringBuilder methodsString = new StringBuilder(); final List typeList = new ArrayList<>(); @@ -99,13 +97,12 @@ protected void fillAttributes(final @NotNull Properties attributes) { } } - phpClassTypesBuilder - .appendProperty("NAMESPACE", file.getNamespace()) - .appendProperty("DESCRIPTION", data.getDescription()) - .appendProperty("INTERFACE_NAME", data.getName()) - .appendProperty("METHODS_DELIMITER", WebApiInterfaceFile.METHODS_DELIMITER) - .appendProperty("METHODS", methodsString.toString()) - .mergeProperties(attributes); + typesBuilder + .append("NAMESPACE", file.getNamespace(), false) + .append("DESCRIPTION", data.getDescription(), false) + .append("INTERFACE_NAME", data.getName(), false) + .append("METHODS_DELIMITER", WebApiInterfaceFile.METHODS_DELIMITER, false) + .append("METHODS", methodsString.toString(), false); if (!typeList.isEmpty()) { attributes.setProperty( 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 index c364dbb49..b36b3518e 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java @@ -7,6 +7,8 @@ import java.util.Collections; import java.util.List; +import com.jetbrains.php.lang.PhpLangUtil; +import com.magento.idea.magento2plugin.util.RegExUtil; import org.jetbrains.annotations.NotNull; public final class PhpClassGeneratorUtil { @@ -38,4 +40,15 @@ public static String getNameFromFqn(final @NotNull String fqn) { return fqnArray[fqnArray.length - 1]; } + + /** + * Check if provided string is a valid PHP class FQN. + * + * @param fqnCandidate String + * + * @return boolean + */ + public static boolean isValidFqn(final @NotNull String fqnCandidate) { + return PhpLangUtil.isFqn(fqnCandidate) || fqnCandidate.matches(RegExUtil.PhpRegex.FQN); + } } diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassTypesBuilder.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassTypesBuilder.java index d08ee95c3..e4693bd52 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassTypesBuilder.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassTypesBuilder.java @@ -42,18 +42,39 @@ public PhpClassTypesBuilder appendProperty( } /** - * Append type. + * Append property or type. * * @param propertyName String - * @param typeFqn String + * @param propertyValue String + * + * @return PhpClassTypesBuilder + */ + public PhpClassTypesBuilder append( + final @NotNull String propertyName, + final @NotNull String propertyValue + ) { + return append(propertyName, propertyValue, true); + } + + /** + * Append property or type. + * + * @param propertyName String + * @param propertyValue String + * @param isAddToImports boolean * * @return PhpClassTypesBuilder */ public PhpClassTypesBuilder append( final @NotNull String propertyName, - final @NotNull String typeFqn + final @NotNull String propertyValue, + final boolean isAddToImports ) { - return append(propertyName, typeFqn, null); + if (isAddToImports && PhpClassGeneratorUtil.isValidFqn(propertyValue)) { + return append(propertyName, propertyValue, null); + } + + return appendProperty(propertyName, propertyValue); } /** @@ -122,6 +143,26 @@ public List getUses() { return uses; } + /** + * Check if any property exists. + * + * @return boolean + */ + public boolean hasProperties() { + return !properties.isEmpty(); + } + + /** + * Check if property exists. + * + * @param propertyName String + * + * @return boolean + */ + public boolean hasProperty(final @NotNull String propertyName) { + return properties.containsKey(propertyName); + } + /** * Check if there is any duplicated type name. * diff --git a/src/com/magento/idea/magento2plugin/util/RegExUtil.java b/src/com/magento/idea/magento2plugin/util/RegExUtil.java index 04c381a89..83ce45ed9 100644 --- a/src/com/magento/idea/magento2plugin/util/RegExUtil.java +++ b/src/com/magento/idea/magento2plugin/util/RegExUtil.java @@ -1,4 +1,4 @@ -/** +/* * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ @@ -47,6 +47,7 @@ public class RegExUtil { = "(\\d+)\\.(\\d+)\\.(\\d+)[a-zA-Z0-9_\\-]*"; public static class Magento { + public static final String PHP_CLASS = "[A-Z][a-zA-Z0-9]+"; From 65acd5bed0ccf767d155500b4a9da373a6c50a00 Mon Sep 17 00:00:00 2001 From: bohdan-harniuk Date: Fri, 7 May 2021 15:49:59 +0300 Subject: [PATCH 2/2] Fixed static checks issues --- .../actions/generation/generator/PhpFileGenerator.java | 2 +- .../generation/generator/util/PhpClassGeneratorUtil.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java b/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java index f34e2bfb5..de6437ea6 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/PhpFileGenerator.java @@ -18,8 +18,8 @@ import com.magento.idea.magento2plugin.indexes.ModuleIndex; import com.magento.idea.magento2plugin.magento.files.AbstractPhpFile; import com.magento.idea.magento2plugin.util.GetPhpClassByFQN; -import javax.swing.JOptionPane; import java.util.Properties; +import javax.swing.JOptionPane; import org.jetbrains.annotations.NotNull; public abstract class PhpFileGenerator extends FileGenerator { 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 index b36b3518e..00b8f7a2d 100644 --- a/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java +++ b/src/com/magento/idea/magento2plugin/actions/generation/generator/util/PhpClassGeneratorUtil.java @@ -5,10 +5,10 @@ package com.magento.idea.magento2plugin.actions.generation.generator.util; -import java.util.Collections; -import java.util.List; import com.jetbrains.php.lang.PhpLangUtil; import com.magento.idea.magento2plugin.util.RegExUtil; +import java.util.Collections; +import java.util.List; import org.jetbrains.annotations.NotNull; public final class PhpClassGeneratorUtil {