Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #1 #2 #3 #4 #5 - small issues in generated classes #6

Merged
merged 2 commits into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 87 additions & 3 deletions Generator/ListRepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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',
]
]
],
];
}

Expand Down Expand Up @@ -148,7 +162,7 @@ protected function _getClassMethods()
],
],
];
return [$this->_getDefaultConstructorDefinition(), $getList];
return [$this->_getDefaultConstructorDefinition(), $getList, $this->getAddFilterGroupToCollectionConfig()];
}

/**
Expand All @@ -157,7 +171,7 @@ protected function _getClassMethods()
protected function _getClassProperties()
{
$searchResultsFactory = [
'name' => 'searchResultFactory',
'name' => 'searchResultsFactory',
'visibility' => 'private',
'docblock' => [
'tags' => [
Expand Down Expand Up @@ -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;
}

}
4 changes: 2 additions & 2 deletions Generator/NameMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -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];
}
Expand Down Expand Up @@ -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];
}
Expand Down
44 changes: 40 additions & 4 deletions Generator/Repo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'
],
]
],
];
}

Expand All @@ -99,7 +113,8 @@ protected function _getClassMethods()
$this->getSaveMethodConfig(),
$this->getGetMethodConfig(),
$this->getDeleteMethodConfig(),
$this->getDeleteByIdMethodConfig()
$this->getDeleteByIdMethodConfig(),
$this->getClearMethodConfig()
];
}

Expand Down Expand Up @@ -127,7 +142,7 @@ protected function _getClassProperties()
'tags' => [
[
'name' => 'var',
'description' => $this->nameMatcher->getResourceClassName($this->getSourceClassName())
'description' => '\\' . $this->nameMatcher->getResourceClassName($this->getSourceClassName())
]
],
],
Expand Down Expand Up @@ -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" .
Expand All @@ -236,6 +252,9 @@ private function getDeleteMethodConfig()
];
}

/**
* @return array
*/
private function getDeleteByIdMethodConfig()
{
return [
Expand Down Expand Up @@ -275,6 +294,9 @@ private function getDeleteByIdMethodConfig()
];
}

/**
* @return array
*/
private function getSaveMethodConfig()
{
$interface = $this->nameMatcher->getInterfaceName($this->getSourceClassName());
Expand Down Expand Up @@ -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'
]
];
}
}
14 changes: 12 additions & 2 deletions Generator/UiCollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected function _getClassProperties()
'tags' => [
[
'name' => 'var',
'description' => $this->nameMatcher->getCollectionFactoryClass($this->getSourceClassName())
'description' => '\\' .
$this->nameMatcher->getCollectionFactoryClass($this->getSourceClassName())
]
],
],
Expand All @@ -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'
]
]
],
];
}

Expand Down
6 changes: 3 additions & 3 deletions Generator/UiManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ protected function _getClassProperties()
'docblock' => [
'tags' => [
['name' => 'var',
'description' => '\\' . $this->nameMatcher->getInterfaceName($this->getSourceClassName())
'description' => $this->nameMatcher->getInterfaceName($this->getSourceClassName())
]
],
],
Expand All @@ -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']],
],
];

Expand Down Expand Up @@ -168,7 +168,7 @@ protected function _getClassMethods()
'tags' => [
[
'name' => 'param',
'description' => AbstractModel::class . ' $entity'
'description' => '\\' . AbstractModel::class . ' $entity'
],
[
'name' => 'throws',
Expand Down
10 changes: 5 additions & 5 deletions Test/Unit/Generator/NameMatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand All @@ -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));
}
Expand Down