|
9 | 9 |
|
10 | 10 | use GraphQL\Language\AST\FieldNode; |
11 | 11 | use Magento\CatalogGraphQl\Model\Category\DepthCalculator; |
12 | | -use Magento\CatalogGraphQl\Model\Category\Hydrator; |
13 | 12 | use Magento\CatalogGraphQl\Model\Category\LevelCalculator; |
14 | 13 | use Magento\Framework\EntityManager\MetadataPool; |
15 | 14 | use Magento\Framework\GraphQl\Schema\Type\ResolveInfo; |
16 | 15 | use Magento\Catalog\Api\Data\CategoryInterface; |
17 | 16 | use Magento\Catalog\Model\ResourceModel\Category\Collection; |
18 | 17 | use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory; |
19 | 18 | use Magento\CatalogGraphQl\Model\AttributesJoiner; |
| 19 | +use Magento\Catalog\Model\Category; |
20 | 20 |
|
21 | 21 | /** |
22 | 22 | * Category tree data provider |
@@ -53,87 +53,64 @@ class CategoryTree |
53 | 53 | */ |
54 | 54 | private $metadata; |
55 | 55 |
|
56 | | - /** |
57 | | - * @var Hydrator |
58 | | - */ |
59 | | - private $hydrator; |
60 | | - |
61 | 56 | /** |
62 | 57 | * @param CollectionFactory $collectionFactory |
63 | 58 | * @param AttributesJoiner $attributesJoiner |
64 | 59 | * @param DepthCalculator $depthCalculator |
65 | 60 | * @param LevelCalculator $levelCalculator |
66 | 61 | * @param MetadataPool $metadata |
67 | | - * @param Hydrator $hydrator |
68 | 62 | */ |
69 | 63 | public function __construct( |
70 | 64 | CollectionFactory $collectionFactory, |
71 | 65 | AttributesJoiner $attributesJoiner, |
72 | 66 | DepthCalculator $depthCalculator, |
73 | 67 | LevelCalculator $levelCalculator, |
74 | | - MetadataPool $metadata, |
75 | | - Hydrator $hydrator |
| 68 | + MetadataPool $metadata |
76 | 69 | ) { |
77 | 70 | $this->collectionFactory = $collectionFactory; |
78 | 71 | $this->attributesJoiner = $attributesJoiner; |
79 | 72 | $this->depthCalculator = $depthCalculator; |
80 | 73 | $this->levelCalculator = $levelCalculator; |
81 | 74 | $this->metadata = $metadata; |
82 | | - $this->hydrator = $hydrator; |
83 | 75 | } |
84 | 76 |
|
85 | 77 | /** |
86 | 78 | * Returns categories tree starting from parent $rootCategoryId |
87 | 79 | * |
88 | 80 | * @param ResolveInfo $resolveInfo |
89 | 81 | * @param int $rootCategoryId |
90 | | - * @return array |
| 82 | + * @return \Iterator |
91 | 83 | */ |
92 | | - public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId) : array |
| 84 | + public function getTree(ResolveInfo $resolveInfo, int $rootCategoryId): \Iterator |
93 | 85 | { |
94 | 86 | $categoryQuery = $resolveInfo->fieldNodes[0]; |
95 | 87 | $collection = $this->collectionFactory->create(); |
96 | 88 | $this->joinAttributesRecursively($collection, $categoryQuery); |
97 | 89 | $depth = $this->depthCalculator->calculate($categoryQuery); |
98 | 90 | $level = $this->levelCalculator->calculate($rootCategoryId); |
| 91 | + |
| 92 | + // If root category is being filter, we've to remove first slash |
| 93 | + if ($rootCategoryId == Category::TREE_ROOT_ID) { |
| 94 | + $regExpPathFilter = sprintf('.*%s/[/0-9]*$', $rootCategoryId); |
| 95 | + } else { |
| 96 | + $regExpPathFilter = sprintf('.*/%s/[/0-9]*$', $rootCategoryId); |
| 97 | + } |
| 98 | + |
99 | 99 | //Search for desired part of category tree |
100 | | - $collection->addPathFilter(sprintf('.*/%s/[/0-9]*$', $rootCategoryId)); |
| 100 | + $collection->addPathFilter($regExpPathFilter); |
| 101 | + |
101 | 102 | $collection->addFieldToFilter('level', ['gt' => $level]); |
102 | 103 | $collection->addFieldToFilter('level', ['lteq' => $level + $depth - self::DEPTH_OFFSET]); |
103 | 104 | $collection->setOrder('level'); |
104 | 105 | $collection->getSelect()->orWhere( |
105 | 106 | $this->metadata->getMetadata(CategoryInterface::class)->getIdentifierField() . ' = ?', |
106 | 107 | $rootCategoryId |
107 | 108 | ); |
108 | | - return $this->processTree($collection->getIterator()); |
109 | | - } |
110 | | - |
111 | | - /** |
112 | | - * Iterates through category tree |
113 | | - * |
114 | | - * @param \Iterator $iterator |
115 | | - * @return array |
116 | | - */ |
117 | | - private function processTree(\Iterator $iterator) : array |
118 | | - { |
119 | | - $tree = []; |
120 | | - while ($iterator->valid()) { |
121 | | - /** @var CategoryInterface $category */ |
122 | | - $category = $iterator->current(); |
123 | | - $iterator->next(); |
124 | | - $nextCategory = $iterator->current(); |
125 | | - $tree[$category->getId()] = $this->hydrator->hydrateCategory($category); |
126 | | - $tree[$category->getId()]['model'] = $category; |
127 | | - if ($nextCategory && (int) $nextCategory->getLevel() !== (int) $category->getLevel()) { |
128 | | - $tree[$category->getId()]['children'] = $this->processTree($iterator); |
129 | | - } |
130 | | - } |
131 | | - |
132 | | - return $tree; |
| 109 | + return $collection->getIterator(); |
133 | 110 | } |
134 | 111 |
|
135 | 112 | /** |
136 | | - * Joins EAV attributes recursively |
| 113 | + * Join attributes recursively |
137 | 114 | * |
138 | 115 | * @param Collection $collection |
139 | 116 | * @param FieldNode $fieldNode |
|
0 commit comments