Skip to content

Commit

Permalink
Extension loader
Browse files Browse the repository at this point in the history
  • Loading branch information
phoenix128 committed Feb 16, 2018
1 parent 1bf2ac4 commit f3837ca
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 14 deletions.
37 changes: 37 additions & 0 deletions Model/Monkey/DddCqrs.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ private function prepare()
'command_delete' => 'Model\\' . $this->entityName . '\\Command\\Delete',
'command_list' => 'Model\\' . $this->entityName . '\\Command\\GetList',

'extension_loader' => 'Model\\' . $this->entityName . 'ExtensionLoader',
'extension_loader_interface' => 'Model\\' . $this->entityName . 'ExtensionLoaderInterface',

'test_wrapper' => 'Test\\Integration\\' . $this->entityName . '\\TestCaseWrapper',
'test_repository' => 'Test\\Integration\\' . $this->entityName . '\\RepositoryTest',
]);
Expand Down Expand Up @@ -595,6 +598,39 @@ private function generateCommandList()
];
}

/**
* Generate extension laoder files
* @throws \Magento\Framework\Exception\LocalizedException
*/
public function generateExtensionLoader()
{
$this->outFiles['extension_loader_interface'] = [
'file' => $this->classes['extension_loader_interface']['file'],
'code' => $this->template->getCodeFromTemplate('ddd-cqrs/Model/ExtensionLoaderInterface', [
'namespace' => $this->classes['extension_loader_interface']['info']['namespace'],
'class' => $this->classes['extension_loader_interface']['info']['class_name'],
'data_interface' => $this->classes['data_interface']['class'],
'entity_var' => $this->entityVar,
'entity_name' => $this->entityName,
]),
];

$extensionFactory = preg_replace('/Interface$/', 'ExtensionFactory', $this->classes['data_interface']['class']);

$this->outFiles['extension_loader'] = [
'file' => $this->classes['extension_loader']['file'],
'code' => $this->template->getCodeFromTemplate('ddd-cqrs/Model/ExtensionLoader', [
'namespace' => $this->classes['extension_loader']['info']['namespace'],
'class' => $this->classes['extension_loader']['info']['class_name'],
'data_interface' => $this->classes['data_interface']['class'],
'entity_var' => $this->entityVar,
'entity_name' => $this->entityName,
'extension_factory' => $extensionFactory,
'extension_loader_interface' => $this->classes['extension_loader_interface']['class'],
]),
];
}

/**
* Inject DI preferences
* @throws \Magento\Framework\Exception\LocalizedException
Expand Down Expand Up @@ -751,6 +787,7 @@ public function execute()
$this->generateCommandDelete();
$this->generateCommandList();
$this->generateRepository();
$this->generateExtensionLoader();

if ($this->tests) {
$this->generateTestCaseWrapper();
Expand Down
2 changes: 1 addition & 1 deletion etc/module.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="MSP_CodeMonkey" setup_version="1.2.9">
<module name="MSP_CodeMonkey" setup_version="1.2.10">
<sequence>
<module name="MSP_Common"/>

Expand Down
4 changes: 1 addition & 3 deletions templates/crud/Api/Data/Interface.template
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

namespace %namespace%;

use Magento\Framework\Api\ExtensibleDataInterface;

interface %class% extends ExtensibleDataInterface
interface %class% extends \Magento\Framework\Api\ExtensibleDataInterface
{
%const_list%

Expand Down
2 changes: 1 addition & 1 deletion templates/ddd-cqrs/Api/Data/Interface.template
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ interface %class% extends ExtensibleDataInterface
* Retrieve existing extension attributes object or create a new one
* @return %extension_interface%|null
*/
public function getExtensionAttributes(): %extension_interface%;
public function getExtensionAttributes();

/**
* Set an extension attributes object
Expand Down
36 changes: 36 additions & 0 deletions templates/ddd-cqrs/Model/ExtensionLoader.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
%header%

declare(strict_types=1);

namespace %namespace%;

class %class% implements %extension_loader_interface%
{
/**
* @var %extension_factory%
*/
private $extensionFactory;

/**
* Extension loader constructor.
* @param %extension_factory% $extensionFactory
* @SuppressWarnings(PHPMD.LongVariables)
*/
public function __construct(
%extension_factory% $extensionFactory
) {
$this->extensionFactory = $extensionFactory;
}

/**
* @inheritdoc
*/
public function execute(%data_interface% $%entity_var%)
{
if ($%entity_var%->getExtensionAttributes() === null) {
$extension = $this->extensionFactory->create();
$%entity_var%->setExtensionAttributes($extension);
}
}
}
20 changes: 20 additions & 0 deletions templates/ddd-cqrs/Model/ExtensionLoaderInterface.template
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
%header%

declare(strict_types=1);

namespace %namespace%;

/**
* Extension attribute loader for %entity_name%
*
* @api
*/
interface %class%
{
/**
* Load extension attributes
* @param %data_interface% $%entity_var%
*/
public function execute(%data_interface% $%entity_var%);
}
11 changes: 2 additions & 9 deletions templates/ddd-cqrs/Model/Model.template
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,9 @@ class %class% extends AbstractExtensibleModel implements
/**
* @inheritdoc
*/
public function getExtensionAttributes(): %extension_interface%
public function getExtensionAttributes()
{
$extensionAttributes = $this->_getExtensionAttributes();
if (null === $extensionAttributes) {
$extensionAttributes = $this->extensionAttributesFactory->create(
%extension_interface%::class
);
$this->setExtensionAttributes($extensionAttributes);
}
return $extensionAttributes;
return $this->_getExtensionAttributes();
}

/**
Expand Down

0 comments on commit f3837ca

Please sign in to comment.