Skip to content

Commit b6da2b7

Browse files
Merge branch 'mainline-entity-manager' of github.com:magento/magento2-phpstorm-plugin into data-mapper-for-entity-manager
2 parents 0097124 + 1d702ef commit b6da2b7

File tree

14 files changed

+867
-21
lines changed

14 files changed

+867
-21
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@
229229
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
230230
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
231231
<internalFileTemplate name="Magento Get List Query Model"/>
232+
<internalFileTemplate name="Magento Entity Save Controller Class"/>
232233
<internalFileTemplate name="Magento Entity Data Mapper"/>
233234

234235
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
<?php
2+
#parse("PHP File Header.php")
3+
4+
namespace ${NAMESPACE};
5+
6+
#set($uses = ${USES})
7+
#foreach ($use in $uses.split(","))
8+
use $use;
9+
#end
10+
11+
/**
12+
* Save ${ENTITY_NAME} controller action.
13+
*/
14+
class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS}
15+
{
16+
/**
17+
* Authorization level of a basic admin session
18+
*
19+
* @see _isAllowed()
20+
*/
21+
const ADMIN_RESOURCE = '${ADMIN_RESOURCE}';
22+
23+
/**
24+
* @var ${DATA_PERSISTOR}
25+
*/
26+
private $dataPersistor;
27+
28+
/**
29+
* @var ${SAVE_COMMAND}
30+
*/
31+
private $saveCommand;
32+
33+
/**
34+
* @var ${ENTITY_DTO_FACTORY}
35+
*/
36+
private $entityDataFactory;
37+
38+
/**
39+
* @param Context $context
40+
* @param ${DATA_PERSISTOR} $dataPersistor
41+
* @param ${SAVE_COMMAND} $saveCommand
42+
* @param ${ENTITY_DTO_FACTORY} $entityDataFactory
43+
*/
44+
public function __construct(
45+
Context $context,
46+
${DATA_PERSISTOR} $dataPersistor,
47+
${SAVE_COMMAND} $saveCommand,
48+
${ENTITY_DTO_FACTORY} $entityDataFactory
49+
) {
50+
parent::__construct($context);
51+
$this->dataPersistor = $dataPersistor;
52+
$this->saveCommand = $saveCommand;
53+
$this->entityDataFactory = $entityDataFactory;
54+
}
55+
56+
/**
57+
* Save ${ENTITY_NAME} Action.
58+
*
59+
* @return ResponseInterface|ResultInterface
60+
*/
61+
public function execute()
62+
{
63+
$resultRedirect = $this->resultRedirectFactory->create();
64+
$params = $this->getRequest()->getParams();
65+
66+
try {
67+
/** @var ${ENTITY_DTO}|${DATA_OBJECT} $entityModel */
68+
$entityModel = $this->entityDataFactory->create();
69+
$entityModel->addData($params);
70+
$this->saveCommand->execute($entityModel);
71+
$this->messageManager->addSuccessMessage(
72+
__('The ${ENTITY_NAME} data was saved successfully')
73+
);
74+
$this->dataPersistor->clear('entity');
75+
} catch (${COULD_NOT_SAVE} $exception) {
76+
$this->messageManager->addErrorMessage($exception->getMessage());
77+
$this->dataPersistor->set('entity', $params);
78+
79+
return $resultRedirect->setPath('*/*/edit', [
80+
'${ENTITY_ID}'=> $this->getRequest()->getParam('${ENTITY_ID}')
81+
]);
82+
}
83+
84+
return $resultRedirect->setPath('*/*/');
85+
}
86+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<html lang="en">
2+
<body>
3+
<p face="verdana" size="-1">
4+
5+
</p>
6+
7+
<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
8+
<tr>
9+
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
10+
</tr>
11+
<tr>
12+
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
13+
<td width="10">&nbsp;</td>
14+
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
15+
</tr>
16+
</table>
17+
</body>
18+
</html>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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 SaveEntityControllerFileData {
11+
12+
private final String entityName;
13+
private final String moduleName;
14+
private final String namespace;
15+
private final String saveCommandFqn;
16+
private final String dtoType;
17+
private final String acl;
18+
private final String entityId;
19+
20+
/**
21+
* Controller save file constructor.
22+
*
23+
* @param entityName String
24+
* @param moduleName String
25+
* @param namespace String
26+
* @param saveCommandFqn String
27+
* @param acl String
28+
* @param dtoType String
29+
* @param entityId String
30+
*/
31+
public SaveEntityControllerFileData(
32+
final @NotNull String entityName,
33+
final @NotNull String moduleName,
34+
final @NotNull String namespace,
35+
final @NotNull String saveCommandFqn,
36+
final @NotNull String dtoType,
37+
final @NotNull String acl,
38+
final @NotNull String entityId
39+
) {
40+
this.entityName = entityName;
41+
this.moduleName = moduleName;
42+
this.namespace = namespace;
43+
this.saveCommandFqn = saveCommandFqn;
44+
this.dtoType = dtoType;
45+
this.acl = acl;
46+
this.entityId = entityId;
47+
}
48+
49+
/**
50+
* Get entity id.
51+
*
52+
* @return String
53+
*/
54+
public String getEntityId() {
55+
return entityId;
56+
}
57+
58+
/**
59+
* Get acl.
60+
*
61+
* @return String
62+
*/
63+
public String getAcl() {
64+
return acl;
65+
}
66+
67+
/**
68+
* Get dto type.
69+
*
70+
* @return String
71+
*/
72+
public String getDtoType() {
73+
return dtoType;
74+
}
75+
76+
/**
77+
* Get save command Fqn.
78+
*
79+
* @return String
80+
*/
81+
public String getSaveCommandFqn() {
82+
return saveCommandFqn;
83+
}
84+
85+
/**
86+
* Get entity name.
87+
*
88+
* @return String
89+
*/
90+
public String getEntityName() {
91+
return entityName;
92+
}
93+
94+
/**
95+
* Get module name.
96+
*
97+
* @return String
98+
*/
99+
public String getModuleName() {
100+
return moduleName;
101+
}
102+
103+
/**
104+
* Get namespace.
105+
*
106+
* @return String
107+
*/
108+
public String getNamespace() {
109+
return namespace;
110+
}
111+
}

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

Lines changed: 55 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
1919
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
2020
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
21+
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
2122
import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData;
2223
import com.magento.idea.magento2plugin.actions.generation.data.GetListQueryModelData;
2324
import com.magento.idea.magento2plugin.actions.generation.data.LayoutXmlData;
@@ -26,6 +27,7 @@
2627
import com.magento.idea.magento2plugin.actions.generation.data.PreferenceDiXmFileData;
2728
import com.magento.idea.magento2plugin.actions.generation.data.ResourceModelData;
2829
import com.magento.idea.magento2plugin.actions.generation.data.RoutesXmlData;
30+
import com.magento.idea.magento2plugin.actions.generation.data.SaveEntityControllerFileData;
2931
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentDataProviderData;
3032
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormButtonData;
3133
import com.magento.idea.magento2plugin.actions.generation.data.UiComponentFormFieldData;
@@ -40,6 +42,7 @@
4042
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
4143
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator;
4244
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
45+
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
4346
import com.magento.idea.magento2plugin.actions.generation.generator.EntityDataMapperGenerator;
4447
import com.magento.idea.magento2plugin.actions.generation.generator.GetListQueryModelGenerator;
4548
import com.magento.idea.magento2plugin.actions.generation.generator.LayoutXmlGenerator;
@@ -50,6 +53,7 @@
5053
import com.magento.idea.magento2plugin.actions.generation.generator.ModuleResourceModelGenerator;
5154
import com.magento.idea.magento2plugin.actions.generation.generator.PreferenceDiXmlGenerator;
5255
import com.magento.idea.magento2plugin.actions.generation.generator.RoutesXmlGenerator;
56+
import com.magento.idea.magento2plugin.actions.generation.generator.SaveEntityControllerFileGenerator;
5357
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentDataProviderGenerator;
5458
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentFormGenerator;
5559
import com.magento.idea.magento2plugin.actions.generation.generator.UiComponentGridXmlGenerator;
@@ -63,6 +67,7 @@
6367
import com.magento.idea.magento2plugin.magento.files.ModuleMenuXml;
6468
import com.magento.idea.magento2plugin.magento.files.ResourceModelPhp;
6569
import com.magento.idea.magento2plugin.magento.files.UiComponentDataProviderPhp;
70+
import com.magento.idea.magento2plugin.magento.files.actions.SaveActionFile;
6671
import com.magento.idea.magento2plugin.magento.packages.Areas;
6772
import com.magento.idea.magento2plugin.magento.packages.File;
6873
import com.magento.idea.magento2plugin.magento.packages.HttpMethod;
@@ -111,7 +116,7 @@
111116
"PMD.GodClass",
112117
"PMD.TooManyMethods",
113118
"PMD.CyclomaticComplexity",
114-
"PMD.ExcessiveClassLength",
119+
"PMD.ExcessiveClassLength"
115120
})
116121
public class NewEntityDialog extends AbstractDialog {
117122
@NotNull
@@ -283,7 +288,7 @@ private void onOK() {
283288

284289
generateRoutesXmlFile();
285290
generateViewControllerFile();
286-
generateSubmitControllerFile();
291+
generateSaveControllerFile();
287292
generateEntityDataMapperFile();
288293
generateModelGetListQueryFile();
289294
generateDataProviderFile();
@@ -308,6 +313,49 @@ private void onOK() {
308313
this.setVisible(false);
309314
}
310315

316+
/**
317+
* Generate Save Controller file.
318+
*/
319+
private void generateSaveControllerFile() {
320+
final NamespaceBuilder dtoModelNamespace = getDataModelNamespace();
321+
final NamespaceBuilder dtoInterfaceModelNamespace = getDataModelInterfaceNamespace();
322+
final NamespaceBuilder namespace = new NamespaceBuilder(
323+
getModuleName(),
324+
SaveActionFile.CLASS_NAME,
325+
SaveActionFile.getDirectory(getEntityName())
326+
);
327+
final String dtoType;
328+
329+
if (createInterface.isSelected()) {
330+
dtoType = dtoInterfaceModelNamespace.getClassFqn();
331+
} else {
332+
dtoType = dtoModelNamespace.getClassFqn();
333+
}
334+
335+
new SaveEntityControllerFileGenerator(new SaveEntityControllerFileData(
336+
getEntityName(),
337+
getModuleName(),
338+
namespace.getNamespace(),
339+
getSaveEntityCommandClassFqn(),
340+
dtoType,
341+
getAcl(),
342+
getEntityIdColumn()
343+
), project).generate(ACTION_NAME, false);
344+
}
345+
346+
/**
347+
* Get save entity command class Fqn.
348+
*
349+
* @return String
350+
*/
351+
private String getSaveEntityCommandClassFqn() {
352+
//TODO: change this stub after the save command generated will be implemented.
353+
final NamespaceBuilder namespaceBuilder =
354+
new NamespaceBuilder(getModuleName(), "SaveCommand", "Command/" + getEntityName());
355+
356+
return namespaceBuilder.getClassFqn();
357+
}
358+
311359
private PsiFile generateModelFile() {
312360
final NamespaceBuilder modelNamespace = getModelNamespace();
313361
final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace();
@@ -579,24 +627,11 @@ private String getControllerDirectory() {
579627
return ControllerBackendPhp.DEFAULT_DIR + File.separator;
580628
}
581629

582-
private PsiFile generateSubmitControllerFile() {
583-
final NamespaceBuilder namespace = new NamespaceBuilder(
584-
getModuleName(),
585-
getSubmitActionName(),
586-
getViewControllerDirectory()
587-
);
588-
return new ModuleControllerClassGenerator(new ControllerFileData(
589-
getViewControllerDirectory(),
590-
getSubmitActionName(),
591-
getModuleName(),
592-
Areas.adminhtml.toString(),
593-
HttpMethod.POST.toString(),
594-
getAcl(),
595-
true,
596-
namespace.getNamespace()
597-
), project).generate(ACTION_NAME, false);
598-
}
599-
630+
/**
631+
* Get Acl id.
632+
*
633+
* @return String
634+
*/
600635
public String getAcl() {
601636
return acl.getText().trim();
602637
}

0 commit comments

Comments
 (0)