diff --git a/Generator/ListRepo.php b/Generator/ListRepo.php index b80d3bc..001d6e9 100644 --- a/Generator/ListRepo.php +++ b/Generator/ListRepo.php @@ -26,6 +26,7 @@ use Magento\Framework\Code\Generator\DefinedClasses; use Magento\Framework\Code\Generator\EntityAbstract; use Magento\Framework\Code\Generator\Io; +use Umc\Crud\Model\ResourceModel\StoreAwareAbstractModel; class ListRepo extends EntityAbstract { @@ -76,7 +77,20 @@ protected function _getDefaultConstructorDefinition() ], 'body' => ' $this->searchResultsFactory = $searchResultsFactory; ' . "\n" . '$this->collectionFactory = $collectionFactory;', - 'docblock' => [], + 'docblock' => [ + 'tags' => [ + [ + 'name' => 'param', + 'description' => '\\' . $this->nameMatcher->getSearchResultFactory($this->getSourceClassName()) + . ' $searchResultsFactory', + ], + [ + 'name' => 'param', + 'description' => '\\' . $this->nameMatcher->getCollectionFactoryClass($this->getSourceClassName()) + . ' $collectionFactory', + ] + ] + ], ]; } @@ -148,7 +162,7 @@ protected function _getClassMethods() ], ], ]; - return [$this->_getDefaultConstructorDefinition(), $getList]; + return [$this->_getDefaultConstructorDefinition(), $getList, $this->getAddFilterGroupToCollectionConfig()]; } /** @@ -157,7 +171,7 @@ protected function _getClassMethods() protected function _getClassProperties() { $searchResultsFactory = [ - 'name' => 'searchResultFactory', + 'name' => 'searchResultsFactory', 'visibility' => 'private', 'docblock' => [ 'tags' => [ @@ -186,4 +200,74 @@ protected function _getClassProperties() return [$searchResultsFactory, $collectionFactory]; } //phpcs: enable + + /** + * @return array + */ + private function getAddFilterGroupToCollectionConfig() + { + $sourceClass = $this->getSourceClassName(); + $body = ' $fields = [];' . "\n"; + $body .= '$conditions = []; ' . "\n"; + $resourceModel = $this->nameMatcher->getResourceClassName($sourceClass); + $body .= 'foreach ($filterGroup->getFilters() as $filter) {' . "\n"; + if (is_subclass_of($resourceModel, StoreAwareAbstractModel::class, true)) { + $body .= ' if ($filter->getField() === \'store_id\') {' . "\n"; + $body .= ' $collection->addStoreFilter($filter->getValue(), true);' . "\n"; + $body .= ' } else {' . "\n"; + $body .= $this->getFilterConditions(8); + $body .= ' }'; + } else { + $body .= $this->getFilterConditions(4); + } + $body .= '}' . "\n"; + $body .= 'if ($fields) {' . "\n"; + $body .= ' $collection->addFieldToFilter($fields, $conditions);' . "\n"; + $body .= '}' . "\n"; + $body .= 'return $this;' . "\n"; + return [ + 'name' => 'addFilterGroupToCollection', + 'parameters' => [ + [ + 'name' => 'filterGroup', + 'type' => '\\' . \Magento\Framework\Api\Search\FilterGroup::class, + ], + [ + 'name' => 'collection', + 'type' => '\\' . $this->nameMatcher->getCollectionClass($sourceClass), + ], + ], + 'body' => $body, + 'docblock' => [ + 'tags' => [ + [ + 'name' => 'param', + 'description' => '\\' . \Magento\Framework\Api\Search\FilterGroup::class . ' $filterGroup' + ], + [ + 'name' => 'param', + 'description' => '\\' . $this->nameMatcher->getCollectionClass($sourceClass) . ' $collection' + ], + [ + 'name' => 'return', + 'description' => '$this' + ], + ], + ], + ]; + } + + /** + * @param $indent + * @return string + */ + private function getFilterConditions($indent) + { + $padd = str_repeat(' ', $indent); + $text = $padd . '$condition = $filter->getConditionType() ? $filter->getConditionType() : \'eq\';' . "\n"; + $text .= $padd . '$fields[] = $filter->getField();' . "\n"; + $text .= $padd . '$conditions[] = [$condition => $filter->getValue()];' . "\n"; + return $text; + } + } diff --git a/Generator/NameMatcher.php b/Generator/NameMatcher.php index 3ee22e7..c756afc 100644 --- a/Generator/NameMatcher.php +++ b/Generator/NameMatcher.php @@ -62,7 +62,7 @@ public function getResourceClassName($class) if (!isset($this->cache['resource'][$class])) { $parts = explode('\\', $class); $parts[2] = 'Model\ResourceModel'; //replace 'Model' with 'Model\ResourceModel'; - $this->cache['resource'][$class] = '\\' . implode('\\', $parts); + $this->cache['resource'][$class] = implode('\\', $parts); } return $this->cache['resource'][$class]; } @@ -110,7 +110,7 @@ public function getSearchResultsClass($class) if (!isset($this->cache['search_results'][$class])) { $parts = explode('\\', $class); $parts[2] = 'Api\Data'; //replace 'Model' with 'Api\Data'; - $this->cache['search_results'][$class] = '\\' . implode('\\', $parts) . 'SearchResultsInterface'; + $this->cache['search_results'][$class] = implode('\\', $parts) . 'SearchResultsInterface'; } return $this->cache['search_results'][$class]; } diff --git a/Generator/Repo.php b/Generator/Repo.php index 8ef438e..1fd08ea 100644 --- a/Generator/Repo.php +++ b/Generator/Repo.php @@ -70,11 +70,25 @@ protected function _getDefaultConstructorDefinition() ], [ 'name' => 'resource', - 'type' => $this->nameMatcher->getResourceClassName($this->getSourceClassName()), + 'type' => '\\' . $this->nameMatcher->getResourceClassName($this->getSourceClassName()), ] ], 'body' => "\t" . '$this->factory = $factory; ' . "\n" . '$this->resource = $resource;', - 'docblock' => [], + 'docblock' => [ + 'tags' => [ + [ + 'name' => 'param', + 'description' => $this->nameMatcher->getInterfaceFactoryClass($this->getSourceClassName()) . + ' $factory' + ], + [ + 'name' => 'param', + 'description' => '\\' . + $this->nameMatcher->getResourceClassName($this->getSourceClassName()) . + ' $resource' + ], + ] + ], ]; } @@ -99,7 +113,8 @@ protected function _getClassMethods() $this->getSaveMethodConfig(), $this->getGetMethodConfig(), $this->getDeleteMethodConfig(), - $this->getDeleteByIdMethodConfig() + $this->getDeleteByIdMethodConfig(), + $this->getClearMethodConfig() ]; } @@ -127,7 +142,7 @@ protected function _getClassProperties() 'tags' => [ [ 'name' => 'var', - 'description' => $this->nameMatcher->getResourceClassName($this->getSourceClassName()) + 'description' => '\\' . $this->nameMatcher->getResourceClassName($this->getSourceClassName()) ] ], ], @@ -225,6 +240,7 @@ private function getDeleteMethodConfig() ], ], 'body' => ' try {' . "\n" . + ' $id = $entity->getId();' . "\n" . ' $this->resource->delete($entity);' . "\n" . ' unset($this->cache[$id]);' . "\n" . '} catch (\Exception $exception) {' . "\n" . @@ -236,6 +252,9 @@ private function getDeleteMethodConfig() ]; } + /** + * @return array + */ private function getDeleteByIdMethodConfig() { return [ @@ -275,6 +294,9 @@ private function getDeleteByIdMethodConfig() ]; } + /** + * @return array + */ private function getSaveMethodConfig() { $interface = $this->nameMatcher->getInterfaceName($this->getSourceClassName()); @@ -312,4 +334,18 @@ private function getSaveMethodConfig() 'return $entity;' ]; } + + /** + * @return array + */ + private function getClearMethodConfig() + { + return [ + 'name' => 'clear', + 'body' => ' $this->cache = [];', + 'docblock' => [ + 'shortDescription' => 'clear the loaded entities' + ] + ]; + } } diff --git a/Generator/UiCollectionProvider.php b/Generator/UiCollectionProvider.php index 7c92331..7908309 100644 --- a/Generator/UiCollectionProvider.php +++ b/Generator/UiCollectionProvider.php @@ -69,7 +69,8 @@ protected function _getClassProperties() 'tags' => [ [ 'name' => 'var', - 'description' => $this->nameMatcher->getCollectionFactoryClass($this->getSourceClassName()) + 'description' => '\\' . + $this->nameMatcher->getCollectionFactoryClass($this->getSourceClassName()) ] ], ], @@ -91,7 +92,16 @@ protected function _getDefaultConstructorDefinition() ] ], 'body' => "\t" . '$this->factory = $factory;' . "\n", - 'docblock' => [], + 'docblock' => [ + 'tags' => [ + [ + 'name' => 'param', + 'description' => '\\' . + $this->nameMatcher->getCollectionFactoryClass($this->getSourceClassName()) . + ' $factory' + ] + ] + ], ]; } diff --git a/Generator/UiManager.php b/Generator/UiManager.php index 7a940b3..f268e08 100644 --- a/Generator/UiManager.php +++ b/Generator/UiManager.php @@ -103,7 +103,7 @@ protected function _getClassProperties() 'docblock' => [ 'tags' => [ ['name' => 'var', - 'description' => '\\' . $this->nameMatcher->getInterfaceName($this->getSourceClassName()) + 'description' => $this->nameMatcher->getInterfaceName($this->getSourceClassName()) ] ], ], @@ -112,7 +112,7 @@ protected function _getClassProperties() 'name' => 'factory', 'visibility' => 'factory', 'docblock' => [ - 'tags' => [['name' => 'var', 'description' => '\\' . $this->getSourceClassName() . 'Factory']], + 'tags' => [['name' => 'var', 'description' => $this->getSourceClassName() . 'Factory']], ], ]; @@ -168,7 +168,7 @@ protected function _getClassMethods() 'tags' => [ [ 'name' => 'param', - 'description' => AbstractModel::class . ' $entity' + 'description' => '\\' . AbstractModel::class . ' $entity' ], [ 'name' => 'throws', diff --git a/Test/Unit/Generator/NameMatcherTest.php b/Test/Unit/Generator/NameMatcherTest.php index 2346c01..96dd760 100644 --- a/Test/Unit/Generator/NameMatcherTest.php +++ b/Test/Unit/Generator/NameMatcherTest.php @@ -64,7 +64,7 @@ public function testGetInterfaceFactoryClass() */ public function testGetResourceClassName() { - $expected = '\Namespace\Module\Model\ResourceModel\Entity'; + $expected = 'Namespace\Module\Model\ResourceModel\Entity'; $model = '\Namespace\Module\Model\Entity'; $this->assertEquals($expected, $this->nameMatcher->getResourceClassName($model)); } @@ -84,7 +84,7 @@ public function testGetRepositoryInterfaceName() */ public function testGetCollectionClass() { - $expected = '\Namespace\Module\Model\ResourceModel\Entity\Collection'; + $expected = 'Namespace\Module\Model\ResourceModel\Entity\Collection'; $model = '\Namespace\Module\Model\Entity'; $this->assertEquals($expected, $this->nameMatcher->getCollectionClass($model)); } @@ -94,7 +94,7 @@ public function testGetCollectionClass() */ public function testGetCollectionFactoryClass() { - $expected = '\Namespace\Module\Model\ResourceModel\Entity\CollectionFactory'; + $expected = 'Namespace\Module\Model\ResourceModel\Entity\CollectionFactory'; $model = '\Namespace\Module\Model\Entity'; $this->assertEquals($expected, $this->nameMatcher->getCollectionFactoryClass($model)); } @@ -104,7 +104,7 @@ public function testGetCollectionFactoryClass() */ public function testGetSearchResultsClass() { - $expected = '\Namespace\Module\Api\Data\EntitySearchResultsInterface'; + $expected = 'Namespace\Module\Api\Data\EntitySearchResultsInterface'; $model = '\Namespace\Module\Model\Entity'; $this->assertEquals($expected, $this->nameMatcher->getSearchResultsClass($model)); } @@ -114,7 +114,7 @@ public function testGetSearchResultsClass() */ public function testGetSearchResultFactory() { - $expected = '\Namespace\Module\Api\Data\EntitySearchResultsInterfaceFactory'; + $expected = 'Namespace\Module\Api\Data\EntitySearchResultsInterfaceFactory'; $model = '\Namespace\Module\Model\Entity'; $this->assertEquals($expected, $this->nameMatcher->getSearchResultFactory($model)); }