Skip to content

Commit 79111d5

Browse files
committed
Merge branch 'mainline-entity-manager' of github.com:magento/magento2-phpstorm-plugin into add-generator-for-delete-controller
2 parents 9c4be7f + 82a4bc2 commit 79111d5

File tree

13 files changed

+637
-5
lines changed

13 files changed

+637
-5
lines changed

resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@
238238
<internalFileTemplate name="Magento PHP Form Generic Button Block Class"/>
239239
<internalFileTemplate name="Magento Entity New Action Controller Class"/>
240240
<internalFileTemplate name="Magento New Entity Layout XML"/>
241+
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
241242
<internalFileTemplate name="Magento Entity Delete Controller Class"/>
242243

243244
<defaultLiveTemplates file="/liveTemplates/MagentoPWA.xml"/>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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+
* Delete ${ENTITY_NAME} by id Command.
13+
*/
14+
class ${CLASS_NAME}
15+
{
16+
/**
17+
* @var ${LOGGER}
18+
*/
19+
private $logger;
20+
21+
/**
22+
* @var ${MODEL_FACTORY}
23+
*/
24+
private $modelFactory;
25+
26+
/**
27+
* @var ${RESOURCE}
28+
*/
29+
private $resource;
30+
31+
/**
32+
* @param ${LOGGER} $logger
33+
* @param ${MODEL_FACTORY} $modelFactory
34+
* @param ${RESOURCE} $resource
35+
*/
36+
public function __construct(
37+
${LOGGER} $logger,
38+
${MODEL_FACTORY} $modelFactory,
39+
${RESOURCE} $resource
40+
) {
41+
$this->logger = $logger;
42+
$this->modelFactory = $modelFactory;
43+
$this->resource = $resource;
44+
}
45+
46+
/**
47+
* Delete ${ENTITY_NAME}.
48+
*
49+
* @param int $entityId
50+
*
51+
* @return void
52+
* @throws ${COULD_NOT_DELETE}|${NO_SUCH_ENTITY_EXCEPTION}
53+
*/
54+
public function execute(int $entityId)
55+
{
56+
try {
57+
/** @var ${MODEL} $model */
58+
$model = $this->modelFactory->create();
59+
$this->resource->load($model, $entityId, '${ENTITY_ID}');
60+
61+
if (!$model->getData('${ENTITY_ID}')) {
62+
throw new ${NO_SUCH_ENTITY_EXCEPTION}(
63+
__('Could not find ${ENTITY_NAME} with id: `%id`',
64+
[
65+
'id' => $entityId
66+
]
67+
)
68+
);
69+
}
70+
71+
$this->resource->delete($model);
72+
} catch (Exception $exception) {
73+
$this->logger->error(
74+
__('Could not delete ${ENTITY_NAME}. Original message: {message}'),
75+
[
76+
'message' => $exception->getMessage(),
77+
'exception' => $exception
78+
]
79+
);
80+
throw new ${COULD_NOT_DELETE}(__('Could not delete ${ENTITY_NAME}.'));
81+
}
82+
}
83+
}
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>

resources/fileTemplates/internal/Magento UI Component Custom Data Provider Class.php.ft

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,17 @@ class ${CLASS_NAME} extends ${EXTENDS}
102102
return $this->loadedData;
103103
}
104104
$this->loadedData = parent::getData();
105+
$itemsById = [];
105106

107+
#set($entityId = "#if(${ENTITY_ID})${ENTITY_ID}#{else}entity_id#end")
108+
#set($entityIdAccessor = '[(int) $item[' + "'$entityId'" + ']]')
109+
#set($getIdAccessor = "[(int) $id]")
106110
foreach ($this->loadedData['items'] as $item) {
107-
#set($idAccessor = '[' + "#if(${ENTITY_ID})'${ENTITY_ID}'#{else}'entity_id'#end" + ']')
108-
$this->loadedData['items'][$item$idAccessor] = $item;
111+
$itemsById$entityIdAccessor = $item;
112+
}
113+
114+
if ($id = $this->request->getParam('$entityId', null)) {
115+
$this->loadedData['entity'] = $itemsById$getIdAccessor;
109116
}
110117

111118
return $this->loadedData;

resources/fileTemplates/internal/Magento UI Component Form XML.xml.ft

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
</argument>
1010
<settings>
1111
<namespace>${NAME}</namespace>
12+
<dataScope>data.general</dataScope>
1213
<deps>
1314
<dep>${NAME}.${NAME}_data_source</dep>
1415
</deps>
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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 DeleteEntityByIdCommandData {
11+
private final String moduleName;
12+
private final String entityName;
13+
private final String namespace;
14+
private final String classFqn;
15+
private final String modelClassFqn;
16+
private final String resourceModelClassFqn;
17+
private final String entityId;
18+
19+
/**
20+
* Delete Command DTO Constructor.
21+
*
22+
* @param moduleName String
23+
* @param entityName String
24+
* @param namespace String
25+
* @param classFqn String
26+
* @param modelClassFqn String
27+
* @param resourceModelClassFqn String
28+
* @param entityId String
29+
*/
30+
public DeleteEntityByIdCommandData(
31+
final @NotNull String moduleName,
32+
final @NotNull String entityName,
33+
final @NotNull String namespace,
34+
final @NotNull String classFqn,
35+
final @NotNull String modelClassFqn,
36+
final @NotNull String resourceModelClassFqn,
37+
final @NotNull String entityId
38+
) {
39+
this.moduleName = moduleName;
40+
this.entityName = entityName;
41+
this.namespace = namespace;
42+
this.classFqn = classFqn;
43+
this.modelClassFqn = modelClassFqn;
44+
this.resourceModelClassFqn = resourceModelClassFqn;
45+
this.entityId = entityId;
46+
}
47+
48+
/**
49+
* Get module name.
50+
*
51+
* @return String
52+
*/
53+
public String getModuleName() {
54+
return moduleName;
55+
}
56+
57+
/**
58+
* Get entity name.
59+
*
60+
* @return String
61+
*/
62+
public String getEntityName() {
63+
return entityName;
64+
}
65+
66+
/**
67+
* Get namespace.
68+
*
69+
* @return String
70+
*/
71+
public String getNamespace() {
72+
return namespace;
73+
}
74+
75+
/**
76+
* Get class FQN.
77+
*
78+
* @return String
79+
*/
80+
public String getClassFqn() {
81+
return classFqn;
82+
}
83+
84+
/**
85+
* Get entity model class FQN.
86+
*
87+
* @return String
88+
*/
89+
public String getModelClassFqn() {
90+
return modelClassFqn;
91+
}
92+
93+
/**
94+
* Get entity resource model class FQN.
95+
*
96+
* @return String
97+
*/
98+
public String getResourceModelClassFqn() {
99+
return resourceModelClassFqn;
100+
}
101+
102+
/**
103+
* Get entity Id.
104+
*
105+
* @return String
106+
*/
107+
public String getEntityId() {
108+
return entityId;
109+
}
110+
}

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

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.magento.idea.magento2plugin.actions.generation.data.DataModelData;
2121
import com.magento.idea.magento2plugin.actions.generation.data.DataModelInterfaceData;
2222
import com.magento.idea.magento2plugin.actions.generation.data.DbSchemaXmlData;
23+
import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityByIdCommandData;
2324
import com.magento.idea.magento2plugin.actions.generation.data.DeleteEntityControllerFileData;
2425
import com.magento.idea.magento2plugin.actions.generation.data.EntityDataMapperData;
2526
import com.magento.idea.magento2plugin.actions.generation.data.FormGenericButtonBlockData;
@@ -57,6 +58,7 @@
5758
import com.magento.idea.magento2plugin.actions.generation.generator.DataModelInterfaceGenerator;
5859
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaWhitelistJsonGenerator;
5960
import com.magento.idea.magento2plugin.actions.generation.generator.DbSchemaXmlGenerator;
61+
import com.magento.idea.magento2plugin.actions.generation.generator.DeleteEntityByIdCommandGenerator;
6062
import com.magento.idea.magento2plugin.actions.generation.generator.DeleteEntityControllerFileGenerator;
6163
import com.magento.idea.magento2plugin.actions.generation.generator.EntityDataMapperGenerator;
6264
import com.magento.idea.magento2plugin.actions.generation.generator.FormGenericButtonBlockGenerator;
@@ -91,6 +93,7 @@
9193
import com.magento.idea.magento2plugin.magento.files.actions.AdminListViewActionFile;
9294
import com.magento.idea.magento2plugin.magento.files.actions.NewActionFile;
9395
import com.magento.idea.magento2plugin.magento.files.actions.SaveActionFile;
96+
import com.magento.idea.magento2plugin.magento.files.commands.DeleteEntityByIdCommandFile;
9497
import com.magento.idea.magento2plugin.magento.files.commands.SaveEntityCommandFile;
9598
import com.magento.idea.magento2plugin.magento.packages.Areas;
9699
import com.magento.idea.magento2plugin.magento.packages.File;
@@ -372,6 +375,7 @@ private void onOK() {
372375
generateFormLayoutFile();
373376
generateNewEntityLayoutFile();
374377
generateSaveEntityCommandFile();
378+
generateDeleteEntityByIdCommandFile();
375379
generateFormSaveControllerFile();
376380
generateFormUiComponentGenericButtonFile();
377381
generateFormNewActionControllerFile();
@@ -1200,6 +1204,33 @@ private void generateSaveEntityCommandFile() {
12001204
modelNamespace.getClassFqn(),
12011205
resourceModelNamespace.getClassFqn(),
12021206
dtoType
1207+
), project).generate(ACTION_NAME, true);
1208+
}
1209+
1210+
/**
1211+
* Run DeleteEntityById.php file generator for an entity.
1212+
*/
1213+
private void generateDeleteEntityByIdCommandFile() {
1214+
final String classFqn = DeleteEntityByIdCommandFile.getClassFqn(
1215+
getModuleName(),
1216+
getEntityName()
1217+
);
1218+
final String namespace = DeleteEntityByIdCommandFile.getNamespace(
1219+
getModuleName(),
1220+
getEntityName()
1221+
);
1222+
final NamespaceBuilder modelNamespace = getModelNamespace();
1223+
final NamespaceBuilder resourceModelNamespace = getResourceModelNamespace();
1224+
1225+
new DeleteEntityByIdCommandGenerator(
1226+
new DeleteEntityByIdCommandData(
1227+
getModuleName(),
1228+
getEntityName(),
1229+
namespace,
1230+
classFqn,
1231+
modelNamespace.getClassFqn(),
1232+
resourceModelNamespace.getClassFqn(),
1233+
getEntityIdColumn()
12031234
),
12041235
project
12051236
).generate(ACTION_NAME, true);
@@ -1283,8 +1314,8 @@ private String getDeleteEntityCommandClassFqn() {
12831314
final NamespaceBuilder namespaceBuilder =
12841315
new NamespaceBuilder(
12851316
getModuleName(),
1286-
"DeleteByIdCommand", //TODO after command added: DeleteEntityCommandFile.CLASS_NAME,
1287-
"Command".concat("/" + getEntityName()) //TODO after command added: DeleteEntityCommandFile.getDirectory(getEntityName())
1317+
DeleteEntityByIdCommandFile.CLASS_NAME,
1318+
DeleteEntityByIdCommandFile.getDirectory(getEntityName())
12881319
);
12891320

12901321
return namespaceBuilder.getClassFqn();

0 commit comments

Comments
 (0)