Skip to content

Commit

Permalink
Merge pull request #521 from magento/mainline-entity-manager
Browse files Browse the repository at this point in the history
Code generator for a Magento Entity
  • Loading branch information
coderimus authored Apr 8, 2021
2 parents f612120 + 773d57e commit d9ff7f8
Show file tree
Hide file tree
Showing 291 changed files with 15,838 additions and 2,564 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0).

## 3.2.0

### Added

- Code generation for a Magento Entity in [#521](https://github.com/magento/magento2-phpstorm-plugin/pull/521)
- Code generation for email templates in [#350](https://github.com/magento/magento2-phpstorm-plugin/pull/350)
- Reference navigation for disabled observers in `events.xml` in [#439](https://github.com/magento/magento2-phpstorm-plugin/pull/439)
- Line markers for test fixtures in [#477](https://github.com/magento/magento2-phpstorm-plugin/pull/477)

### Changed

- Added ability to set the module sequence at generating new module [#266](https://github.com/magento/magento2-phpstorm-plugin/pull/266)

### Fixed

- ArrayIndexOutOfBoundsException in the New Module Action in [#519](https://github.com/magento/magento2-phpstorm-plugin/pull/519)

## 3.1.3

### Changed
Expand Down
13 changes: 13 additions & 0 deletions resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@

<!-- Module file generators -->
<group id="MagentoNewModuleFileGroup" class="com.magento.idea.magento2plugin.actions.groups.NewModuleFileGroup" text="Module File" popup="true">
<action id="MagentoCreateEntity" class="com.magento.idea.magento2plugin.actions.generation.NewEntityAction" />
<action id="MagentoCreateABlock" class="com.magento.idea.magento2plugin.actions.generation.NewBlockAction" />
<action id="MagentoCreateAController" class="com.magento.idea.magento2plugin.actions.generation.NewControllerAction" />
<action id="MagentoCreateACronjob" class="com.magento.idea.magento2plugin.actions.generation.NewCronjobAction" />
Expand Down Expand Up @@ -229,6 +230,18 @@
<internalFileTemplate name="Magento Data Model Interface"/>
<internalFileTemplate name="Magento Module Declarative Schema XML"/>
<internalFileTemplate name="Magento Module Declarative Schema Whitelist JSON"/>
<internalFileTemplate name="Magento Get List Query Model"/>
<internalFileTemplate name="Magento Entity Save Controller Class"/>
<internalFileTemplate name="Magento Entity Data Mapper"/>
<internalFileTemplate name="Magento Save Entity Command Model"/>
<internalFileTemplate name="Magento Entity Index Adminhtml Controller Class"/>
<internalFileTemplate name="Magento Grid Ui Component Action Column Class"/>
<internalFileTemplate name="Magento PHP Form Generic Button Block Class"/>
<internalFileTemplate name="Magento Entity New Action Controller Class"/>
<internalFileTemplate name="Magento New Entity Layout XML"/>
<internalFileTemplate name="Magento Delete Entity By Id Command"/>
<internalFileTemplate name="Magento Entity Edit Action Controller Class"/>
<internalFileTemplate name="Magento Entity Delete Controller Class"/>

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

Expand Down
18 changes: 18 additions & 0 deletions resources/fileTemplates/code/Magento Entity Data Mapper.html.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html lang="en">
<body>
<p face="verdana" size="-1">

</p>

<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
</tr>
</table>
</body>
</html>
52 changes: 52 additions & 0 deletions resources/fileTemplates/code/Magento Entity Data Mapper.php.ft
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

#set($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

/**
* Converts a collection of ${ENTITY_NAME} entities to an array of data transfer objects.
*/
class ${CLASS_NAME}
{
/**
* @var ${DTO_FACTORY}
*/
private $entityDtoFactory;

/**
* @param ${DTO_FACTORY} $entityDtoFactory
*/
public function __construct(
${DTO_FACTORY} $entityDtoFactory
) {
$this->entityDtoFactory = $entityDtoFactory;
}

/**
* Map magento models to DTO array.
*
* @param ${ABSTRACT_COLLECTION} $collection
*
* @return array|${DTO_TYPE}[]
*/
public function map(${ABSTRACT_COLLECTION} $collection): array
{
$results = [];
/** @var ${MAGENTO_MODEL_TYPE} $item */
foreach ($collection->getItems() as $item) {
/** @var ${DTO_TYPE}|${DATA_OBJECT} $entityDto */
$entityDto = $this->entityDtoFactory->create();
$entityDto->addData($item->getData());

#set($brackets = "[]")
$results$brackets = $entityDto;
}

return $results;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<column name="${COLUMN_NAME}">
<settings>
#if (${COLUMN_FILTER})
<filter>${COLUMN_FILTER}</filter>
#end
<label translate="true">${COLUMN_LABEL}</label>
</settings>
</column>
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,17 @@
<label translate="true">${LABEL}</label>
<dataScope>${NAME}</dataScope>
</settings>
#if(${FORM_ELEMENT} == 'checkbox')
<formElements>
<checkbox>
<settings>
<valueMap>
<map name="false" xsi:type="number">0</map>
<map name="true" xsi:type="number">1</map>
</valueMap>
<prefer>toggle</prefer>
</settings>
</checkbox>
</formElements>
#end
</field>

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

#set($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

/**
* Delete ${ENTITY_NAME} by id Command.
*/
class ${CLASS_NAME}
{
/**
* @var ${LOGGER}
*/
private $logger;

/**
* @var ${MODEL_FACTORY}
*/
private $modelFactory;

/**
* @var ${RESOURCE}
*/
private $resource;

/**
* @param ${LOGGER} $logger
* @param ${MODEL_FACTORY} $modelFactory
* @param ${RESOURCE} $resource
*/
public function __construct(
${LOGGER} $logger,
${MODEL_FACTORY} $modelFactory,
${RESOURCE} $resource
) {
$this->logger = $logger;
$this->modelFactory = $modelFactory;
$this->resource = $resource;
}

/**
* Delete ${ENTITY_NAME}.
*
* @param int $entityId
*
* @return void
* @throws ${COULD_NOT_DELETE}|${NO_SUCH_ENTITY_EXCEPTION}
*/
public function execute(int $entityId)
{
try {
/** @var ${MODEL} $model */
$model = $this->modelFactory->create();
$this->resource->load($model, $entityId, '${ENTITY_ID}');

if (!$model->getData('${ENTITY_ID}')) {
throw new ${NO_SUCH_ENTITY_EXCEPTION}(
__('Could not find ${ENTITY_NAME} with id: `%id`',
[
'id' => $entityId
]
)
);
}

$this->resource->delete($model);
} catch (Exception $exception) {
$this->logger->error(
__('Could not delete ${ENTITY_NAME}. Original message: {message}'),
[
'message' => $exception->getMessage(),
'exception' => $exception
]
);
throw new ${COULD_NOT_DELETE}(__('Could not delete ${ENTITY_NAME}.'));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html lang="en">
<body>
<p face="verdana" size="-1">

</p>

<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
</tr>
</table>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php
#parse("PHP File Header.php")

namespace ${NAMESPACE};

#set($uses = ${USES})
#foreach ($use in $uses.split(","))
use $use;
#end

/**
* Delete ${ENTITY_NAME} controller.
*/
class ${CLASS_NAME} extends ${EXTENDS} implements ${IMPLEMENTS_POST}, ${IMPLEMENTS_GET}
{
/**
* Authorization level of a basic admin session.
*
* @see _isAllowed()
*/
const ADMIN_RESOURCE = '${ADMIN_RESOURCE}';

/**
* @var ${DELETE_COMMAND}
*/
private $deleteByIdCommand;

/**
* @param Context $context
* @param DeleteByIdCommand $deleteByIdCommand
*/
public function __construct(
${CONTEXT} $context,
${DELETE_COMMAND} $deleteByIdCommand
) {
parent::__construct($context);
$this->deleteByIdCommand = $deleteByIdCommand;
}

/**
* Delete ${ENTITY_NAME} action.
*
* @return ${RESULT_INTERFACE}
*/
public function execute()
{
/** @var ${RESULT_INTERFACE} $resultRedirect */
$resultRedirect = $this->resultFactory->create(${RESULT_FACTORY}::TYPE_REDIRECT);
$resultRedirect->setPath('*/*/');
$entityId = (int) $this->getRequest()->getParam('${ENTITY_ID}');

try {
$this->deleteByIdCommand->execute($entityId);
$this->messageManager->addSuccessMessage(__('You have successfully deleted ${ENTITY_NAME} entity'));
} catch (${COULD_NOT_DELETE} | ${NO_SUCH_ENTITY_EXCEPTION} $exception) {
$this->messageManager->addErrorMessage($exception->getMessage());
}

return $resultRedirect;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<html lang="en">
<body>
<p face="verdana" size="-1">

</p>

<table width="100%" border="0" cellpadding="5" cellspacing="0" style="border-collapse: collapse">
<tr>
<td colspan="3"><font face="verdana" size="-1">Template's predefined variables:</font></td>
</tr>
<tr>
<td valign="top"><nobr><font face="verdana" size="-2"><b>${NAMESPACE}</b></font></nobr></td>
<td width="10">&nbsp;</td>
<td width="100%" valign="top"><font face="verdana" size="-1"></font></td>
</tr>
</table>
</body>
</html>
Loading

0 comments on commit d9ff7f8

Please sign in to comment.