From f3837ca80eda58649e050f501cb18fc01d388c2e Mon Sep 17 00:00:00 2001 From: Riccardo Tempesta Date: Fri, 16 Feb 2018 20:35:25 +0100 Subject: [PATCH] Extension loader --- Model/Monkey/DddCqrs.php | 37 +++++++++++++++++++ etc/module.xml | 2 +- templates/crud/Api/Data/Interface.template | 4 +- .../ddd-cqrs/Api/Data/Interface.template | 2 +- .../ddd-cqrs/Model/ExtensionLoader.template | 36 ++++++++++++++++++ .../Model/ExtensionLoaderInterface.template | 20 ++++++++++ templates/ddd-cqrs/Model/Model.template | 11 +----- 7 files changed, 98 insertions(+), 14 deletions(-) create mode 100644 templates/ddd-cqrs/Model/ExtensionLoader.template create mode 100644 templates/ddd-cqrs/Model/ExtensionLoaderInterface.template diff --git a/Model/Monkey/DddCqrs.php b/Model/Monkey/DddCqrs.php index ddc8107..3bbe3df 100755 --- a/Model/Monkey/DddCqrs.php +++ b/Model/Monkey/DddCqrs.php @@ -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', ]); @@ -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 @@ -751,6 +787,7 @@ public function execute() $this->generateCommandDelete(); $this->generateCommandList(); $this->generateRepository(); + $this->generateExtensionLoader(); if ($this->tests) { $this->generateTestCaseWrapper(); diff --git a/etc/module.xml b/etc/module.xml index 86f11a5..d641ee1 100755 --- a/etc/module.xml +++ b/etc/module.xml @@ -21,7 +21,7 @@ --> - + diff --git a/templates/crud/Api/Data/Interface.template b/templates/crud/Api/Data/Interface.template index 7ddcedd..b18eb9c 100755 --- a/templates/crud/Api/Data/Interface.template +++ b/templates/crud/Api/Data/Interface.template @@ -3,9 +3,7 @@ namespace %namespace%; -use Magento\Framework\Api\ExtensibleDataInterface; - -interface %class% extends ExtensibleDataInterface +interface %class% extends \Magento\Framework\Api\ExtensibleDataInterface { %const_list% diff --git a/templates/ddd-cqrs/Api/Data/Interface.template b/templates/ddd-cqrs/Api/Data/Interface.template index fafbf9c..f560afa 100755 --- a/templates/ddd-cqrs/Api/Data/Interface.template +++ b/templates/ddd-cqrs/Api/Data/Interface.template @@ -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 diff --git a/templates/ddd-cqrs/Model/ExtensionLoader.template b/templates/ddd-cqrs/Model/ExtensionLoader.template new file mode 100644 index 0000000..533179e --- /dev/null +++ b/templates/ddd-cqrs/Model/ExtensionLoader.template @@ -0,0 +1,36 @@ +extensionFactory = $extensionFactory; + } + + /** + * @inheritdoc + */ + public function execute(%data_interface% $%entity_var%) + { + if ($%entity_var%->getExtensionAttributes() === null) { + $extension = $this->extensionFactory->create(); + $%entity_var%->setExtensionAttributes($extension); + } + } +} diff --git a/templates/ddd-cqrs/Model/ExtensionLoaderInterface.template b/templates/ddd-cqrs/Model/ExtensionLoaderInterface.template new file mode 100644 index 0000000..9ad22ca --- /dev/null +++ b/templates/ddd-cqrs/Model/ExtensionLoaderInterface.template @@ -0,0 +1,20 @@ +_getExtensionAttributes(); - if (null === $extensionAttributes) { - $extensionAttributes = $this->extensionAttributesFactory->create( - %extension_interface%::class - ); - $this->setExtensionAttributes($extensionAttributes); - } - return $extensionAttributes; + return $this->_getExtensionAttributes(); } /**