Skip to content

Commit 191788c

Browse files
author
Vitaliy
authored
Merge pull request #487 from bohdan-harniuk/new-entity-action-layout-generation
Added new entity layout file generation, test for generation
2 parents 3ce4d07 + 5a4f816 commit 191788c

File tree

9 files changed

+314
-0
lines changed

9 files changed

+314
-0
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,7 @@
237237
<internalFileTemplate name="Magento Grid Ui Component Action Column Class"/>
238238
<internalFileTemplate name="Magento PHP Form Generic Button Block Class"/>
239239
<internalFileTemplate name="Magento Entity New Action Controller Class"/>
240+
<internalFileTemplate name="Magento New Entity Layout XML"/>
240241

241242
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
242243

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
2+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
<update handle="${EDIT_ENTITY_LAYOUT}"/>
4+
</page>

resources/fileTemplates/internal/Magento New Entity Layout XML.xml.html

Whitespace-only changes.
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.data;
7+
8+
import org.jetbrains.annotations.NotNull;
9+
10+
public class NewEntityLayoutData {
11+
12+
private final String moduleName;
13+
private final String newActionPath;
14+
private final String editActionPath;
15+
16+
/**
17+
* New entity layout data.
18+
*
19+
* @param moduleName String
20+
* @param newActionPath String
21+
* @param editActionPath String
22+
*/
23+
public NewEntityLayoutData(
24+
final @NotNull String moduleName,
25+
final @NotNull String newActionPath,
26+
final @NotNull String editActionPath
27+
) {
28+
this.moduleName = moduleName;
29+
this.newActionPath = newActionPath;
30+
this.editActionPath = editActionPath;
31+
}
32+
33+
/**
34+
* Get module name.
35+
*
36+
* @return String
37+
*/
38+
public String getModuleName() {
39+
return moduleName;
40+
}
41+
42+
/**
43+
* Get new action path.
44+
*
45+
* @return String
46+
*/
47+
public String getNewActionPath() {
48+
return newActionPath;
49+
}
50+
51+
/**
52+
* Get edit action path.
53+
*
54+
* @return String
55+
*/
56+
public String getEditActionPath() {
57+
return editActionPath;
58+
}
59+
}

src/com/magento/idea/magento2plugin/actions/generation/dialog/NewEntityDialog.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData;
2929
import com.magento.idea.magento2plugin.actions.generation.data.ModelData;
3030
import com.magento.idea.magento2plugin.actions.generation.data.NewActionEntityControllerFileData;
31+
import com.magento.idea.magento2plugin.actions.generation.data.NewEntityLayoutData;
3132
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
3233
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
3334
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
@@ -66,6 +67,7 @@
6667
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator;
6768
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
6869
import com.magento.idea.magento2plugin.actions.generation.generator.NewActionEntityControllerFileGenerator;
70+
import com.magento.idea.magento2plugin.actions.generation.generator.NewEntityLayoutGenerator;
6971
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
7072
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
7173
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityCommandGenerator;
@@ -365,6 +367,7 @@ private void onOK() {
365367
generateUiComponentGridFile();
366368
generateFormViewControllerFile();
367369
generateFormLayoutFile();
370+
generateNewEntityLayoutFile();
368371
generateSaveEntityCommandFile();
369372
generateFormSaveControllerFile();
370373
generateFormUiComponentGenericButtonFile();
@@ -1087,6 +1090,19 @@ private String getEditViewAction() {
10871090
+ "edit";
10881091
}
10891092

1093+
/**
1094+
* Get new entity action path.
1095+
*
1096+
* @return String
1097+
*/
1098+
private String getNewEntityAction() {
1099+
return getRoute()
1100+
+ File.separator
1101+
+ FirstLetterToLowercaseUtil.convert(getEntityName())
1102+
+ File.separator
1103+
+ "new";
1104+
}
1105+
10901106
/**
10911107
* Get delete action path.
10921108
*
@@ -1135,6 +1151,14 @@ private void generateFormLayoutFile() {
11351151
), project).generate(ACTION_NAME, false);
11361152
}
11371153

1154+
private void generateNewEntityLayoutFile() {
1155+
new NewEntityLayoutGenerator(new NewEntityLayoutData(
1156+
getModuleName(),
1157+
getNewEntityAction(),
1158+
getEditViewAction()
1159+
), project).generate(ACTION_NAME, false);
1160+
}
1161+
11381162
/**
11391163
* Run SaveCommand.php file generator for an entity.
11401164
*/
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.generator;
7+
8+
import com.intellij.openapi.project.Project;
9+
import com.intellij.psi.PsiDirectory;
10+
import com.intellij.psi.PsiFile;
11+
import com.magento.idea.magento2plugin.actions.generation.data.NewEntityLayoutData;
12+
import com.magento.idea.magento2plugin.actions.generation.generator.util.DirectoryGenerator;
13+
import com.magento.idea.magento2plugin.actions.generation.generator.util.FileFromTemplateGenerator;
14+
import com.magento.idea.magento2plugin.indexes.ModuleIndex;
15+
import com.magento.idea.magento2plugin.magento.files.NewEntityLayoutFile;
16+
import com.magento.idea.magento2plugin.magento.packages.Areas;
17+
import com.magento.idea.magento2plugin.magento.packages.File;
18+
import com.magento.idea.magento2plugin.util.magento.FileBasedIndexUtil;
19+
import java.util.Properties;
20+
import org.jetbrains.annotations.NotNull;
21+
22+
public class NewEntityLayoutGenerator extends FileGenerator {
23+
24+
private final Project project;
25+
private final NewEntityLayoutData data;
26+
private final NewEntityLayoutFile file;
27+
private final FileFromTemplateGenerator fileFromTemplateGenerator;
28+
private final DirectoryGenerator directoryGenerator;
29+
private final ModuleIndex moduleIndex;
30+
private final boolean checkFileAlreadyExists;
31+
32+
/**
33+
* New entity layout data constructor.
34+
*
35+
* @param data NewEntityLayoutData
36+
* @param project Project
37+
*/
38+
public NewEntityLayoutGenerator(
39+
final @NotNull NewEntityLayoutData data,
40+
final @NotNull Project project
41+
) {
42+
this(data, project, true);
43+
}
44+
45+
/**
46+
* New entity layout data constructor.
47+
*
48+
* @param data NewEntityLayoutData
49+
* @param project Project
50+
* @param checkFileAlreadyExists boolean
51+
*/
52+
public NewEntityLayoutGenerator(
53+
final @NotNull NewEntityLayoutData data,
54+
final @NotNull Project project,
55+
final boolean checkFileAlreadyExists
56+
) {
57+
super(project);
58+
this.data = data;
59+
this.project = project;
60+
this.checkFileAlreadyExists = checkFileAlreadyExists;
61+
file = new NewEntityLayoutFile(data.getNewActionPath().replace(File.separator, "_"));
62+
fileFromTemplateGenerator = FileFromTemplateGenerator.getInstance(project);
63+
directoryGenerator = DirectoryGenerator.getInstance();
64+
moduleIndex = ModuleIndex.getInstance(project);
65+
}
66+
67+
/**
68+
* Generate new entity layout.
69+
*
70+
* @param actionName String
71+
*
72+
* @return PsiFile
73+
*/
74+
@Override
75+
public PsiFile generate(final @NotNull String actionName) {
76+
final PsiFile layout = FileBasedIndexUtil.findModuleViewFile(
77+
file.getFileName(),
78+
Areas.adminhtml,
79+
data.getModuleName(),
80+
project,
81+
file.getParentDirectory()
82+
);
83+
84+
if (this.checkFileAlreadyExists && layout != null) {
85+
return layout;
86+
}
87+
88+
final PsiDirectory moduleBaseDir = moduleIndex.getModuleDirectoryByModuleName(
89+
data.getModuleName()
90+
);
91+
final PsiDirectory layoutBaseDir = directoryGenerator.findOrCreateSubdirectories(
92+
moduleBaseDir,
93+
file.getDirectory()
94+
);
95+
96+
return fileFromTemplateGenerator.generate(
97+
file,
98+
getAttributes(),
99+
layoutBaseDir,
100+
actionName
101+
);
102+
}
103+
104+
/**
105+
* Fill layout file attributes.
106+
*
107+
* @param attributes Properties
108+
*/
109+
@Override
110+
protected void fillAttributes(final @NotNull Properties attributes) {
111+
attributes.setProperty(
112+
"EDIT_ENTITY_LAYOUT",
113+
data.getEditActionPath().replace(File.separator, "_")
114+
);
115+
}
116+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.magento.files;
7+
8+
import com.intellij.lang.Language;
9+
import com.intellij.lang.xml.XMLLanguage;
10+
import org.jetbrains.annotations.NotNull;
11+
12+
public class NewEntityLayoutFile implements ModuleFileInterface {
13+
14+
public static final String TEMPLATE = "Magento New Entity Layout XML";
15+
private static final String PARENT_DIRECTORY = "layout";
16+
private static final String DIRECTORY = "view/adminhtml/layout";
17+
private final String filename;
18+
19+
/**
20+
* New entity layout file constructor.
21+
*
22+
* @param filename String
23+
*/
24+
public NewEntityLayoutFile(final @NotNull String filename) {
25+
this.filename = filename;
26+
}
27+
28+
/**
29+
* Get parent directory for file.
30+
*
31+
* @return String
32+
*/
33+
public String getParentDirectory() {
34+
return PARENT_DIRECTORY;
35+
}
36+
37+
/**
38+
* Get directory for file.
39+
*
40+
* @return String
41+
*/
42+
public String getDirectory() {
43+
return DIRECTORY;
44+
}
45+
46+
@Override
47+
public String getFileName() {
48+
return filename.concat(".xml");
49+
}
50+
51+
@Override
52+
public String getTemplate() {
53+
return TEMPLATE;
54+
}
55+
56+
@Override
57+
public Language getLanguage() {
58+
return XMLLanguage.INSTANCE;
59+
}
60+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="admin-1column"
2+
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
3+
<update handle="book_book_edit"/>
4+
</page>
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright © Magento, Inc. All rights reserved.
3+
* See COPYING.txt for license details.
4+
*/
5+
6+
package com.magento.idea.magento2plugin.actions.generation.generator;
7+
8+
import com.intellij.psi.PsiFile;
9+
import com.magento.idea.magento2plugin.actions.generation.data.NewEntityLayoutData;
10+
import com.magento.idea.magento2plugin.magento.files.NewEntityLayoutFile;
11+
import com.magento.idea.magento2plugin.magento.packages.File;
12+
13+
public class NewEntityLayoutGeneratorTest extends BaseGeneratorTestCase {
14+
15+
private static final String MODULE_NAME = "Foo_Bar";
16+
private static final String EDIT_ACTION = "book/book/edit";
17+
private static final String NEW_ACTION = "book/book/new";
18+
19+
/**
20+
* Test new entity layout file generation.
21+
*/
22+
public void testGenerateNewEntityLayoutFile() {
23+
final NewEntityLayoutData data = new NewEntityLayoutData(
24+
MODULE_NAME,
25+
NEW_ACTION,
26+
EDIT_ACTION
27+
);
28+
final NewEntityLayoutFile file =
29+
new NewEntityLayoutFile(data.getNewActionPath().replace(File.separator, "_"));
30+
31+
final NewEntityLayoutGenerator generator = new NewEntityLayoutGenerator(
32+
data,
33+
myFixture.getProject(),
34+
false
35+
);
36+
final PsiFile newActionLayoutFile = generator.generate("test");
37+
final String filePath = this.getFixturePath(file.getFileName());
38+
final PsiFile expectedFile = myFixture.configureByFile(filePath);
39+
40+
assertGeneratedFileIsCorrect(
41+
expectedFile,
42+
file.getDirectory(),
43+
newActionLayoutFile
44+
);
45+
}
46+
}

0 commit comments

Comments
 (0)