Skip to content

Commit dc6cb89

Browse files
Added new entity layout file generation, test for generation
1 parent f429534 commit dc6cb89

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
@@ -236,6 +236,7 @@
236236
<internalFileTemplate name="Magento Entity Index Adminhtml Controller Class"/>
237237
<internalFileTemplate name="Magento Grid Ui Component Action Column Class"/>
238238
<internalFileTemplate name="Magento PHP Form Generic Button Block Class"/>
239+
<internalFileTemplate name="Magento New Entity Layout XML"/>
239240

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

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
@@ -27,6 +27,7 @@
2727
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
2828
import com.magento.idea.magento2plugin.actions.generation.data.MenuXmlData;
2929
import com.magento.idea.magento2plugin.actions.generation.data.ModelData;
30+
import com.magento.idea.magento2plugin.actions.generation.data.NewEntityLayoutData;
3031
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
3132
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
3233
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
@@ -64,6 +65,7 @@
6465
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleControllerClassGenerator;
6566
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleModelGenerator;
6667
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
68+
import com.magento.idea.magento2plugin.actions.generation.generator.NewEntityLayoutGenerator;
6769
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
6870
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
6971
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityCommandGenerator;
@@ -362,6 +364,7 @@ private void onOK() {
362364
generateUiComponentGridFile();
363365
generateFormViewControllerFile();
364366
generateFormLayoutFile();
367+
generateNewEntityLayoutFile();
365368
generateSaveEntityCommandFile();
366369
generateFormSaveControllerFile();
367370
generateFormUiComponentGenericButtonFile();
@@ -1083,6 +1086,19 @@ private String getEditViewAction() {
10831086
+ "edit";
10841087
}
10851088

1089+
/**
1090+
* Get new entity action path.
1091+
*
1092+
* @return String
1093+
*/
1094+
private String getNewEntityAction() {
1095+
return getRoute()
1096+
+ File.separator
1097+
+ FirstLetterToLowercaseUtil.convert(getEntityName())
1098+
+ File.separator
1099+
+ "new";
1100+
}
1101+
10861102
/**
10871103
* Get delete action path.
10881104
*
@@ -1131,6 +1147,14 @@ private void generateFormLayoutFile() {
11311147
), project).generate(ACTION_NAME, false);
11321148
}
11331149

1150+
private void generateNewEntityLayoutFile() {
1151+
new NewEntityLayoutGenerator(new NewEntityLayoutData(
1152+
getModuleName(),
1153+
getNewEntityAction(),
1154+
getEditViewAction()
1155+
), project).generate(ACTION_NAME, false);
1156+
}
1157+
11341158
/**
11351159
* Run SaveCommand.php file generator for an entity.
11361160
*/
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)