From 4a423c8310ae9bc1d3db04460b549ab381888f62 Mon Sep 17 00:00:00 2001 From: Maxime Leclercq Date: Mon, 24 Jul 2023 15:00:26 +0200 Subject: [PATCH] refactor: use tagged_iterator for post filters and sorters --- UPGRADE-2.0.md | 2 + src/Resources/config/services.yaml | 59 ++++++++++++------- .../PostFilterRegistryInterface.php | 20 ------- .../PostFilter/ProductTaxonRegistry.php | 28 --------- .../Request/ProductRequest/InstantSearch.php | 4 +- src/Search/Request/ProductRequest/Search.php | 30 ++++++---- src/Search/Request/ProductRequest/Taxon.php | 32 ++++++---- .../Request/Sorting/ProductSorterRegistry.php | 28 --------- .../Sorting/SorterRegistryInterface.php | 20 ------- 9 files changed, 82 insertions(+), 141 deletions(-) delete mode 100644 src/Search/Request/PostFilter/PostFilterRegistryInterface.php delete mode 100644 src/Search/Request/PostFilter/ProductTaxonRegistry.php delete mode 100644 src/Search/Request/Sorting/ProductSorterRegistry.php delete mode 100644 src/Search/Request/Sorting/SorterRegistryInterface.php diff --git a/UPGRADE-2.0.md b/UPGRADE-2.0.md index 5f9cba76..a60eda32 100644 --- a/UPGRADE-2.0.md +++ b/UPGRADE-2.0.md @@ -3,3 +3,5 @@ - Remove `MonsieurBiz\SyliusSearchPlugin\Search\Request\QueryFilter\ProductSearchRegistry` service to use an iterator of services tagged `monsieurbiz.search.request.product_search_filter` - Remove `monsieurbiz.search.request.query_filter.product_instant_search_registry` service to use an iterator of services tagged `monsieurbiz.search.request.product_instant_search_filter` - Remove `MonsieurBiz\SyliusSearchPlugin\Search\Request\QueryFilter\ProductTaxonRegistry` service to use an iterator of services tagged `monsieurbiz.search.request.product_taxon_filter` +- Remove `MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\ProductTaxonRegistry` service to use an iterator of services tagged `monsieurbiz.search.request.product_post_filter` +- Remove `MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\ProductSorterRegistry` service to use an iterator of services tagged `monsieurbiz.search.request.product_sorter` diff --git a/src/Resources/config/services.yaml b/src/Resources/config/services.yaml index 3e2316df..95ed627e 100644 --- a/src/Resources/config/services.yaml +++ b/src/Resources/config/services.yaml @@ -152,25 +152,42 @@ services: - { name: monsieurbiz.search.request.product_taxon_filter } # Define post filters - MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\ProductTaxonRegistry: - arguments: - - [ - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\AttributesPostFilter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\MainTaxonPostFilter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\OptionsPostFilter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\PricePostFilter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\ProductTaxonPostFilter', - ] + MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\AttributesPostFilter: + tags: + - { name: monsieurbiz.search.request.product_post_filter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\MainTaxonPostFilter: + tags: + - { name: monsieurbiz.search.request.product_post_filter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\OptionsPostFilter: + tags: + - { name: monsieurbiz.search.request.product_post_filter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\PricePostFilter: + tags: + - { name: monsieurbiz.search.request.product_post_filter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\Product\ProductTaxonPostFilter: + tags: + - { name: monsieurbiz.search.request.product_post_filter } # Define sorters - MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\ProductSorterRegistry: - arguments: - - [ - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\PositionSorter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\PriceSorter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\NameSorter', - '@MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\CreatedAtSorter', - ] + MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\PositionSorter: + tags: + - { name: monsieurbiz.search.request.product_sorter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\PriceSorter: + tags: + - { name: monsieurbiz.search.request.product_sorter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\NameSorter: + tags: + - { name: monsieurbiz.search.request.product_sorter } + + MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\Product\CreatedAtSorter: + tags: + - { name: monsieurbiz.search.request.product_sorter } # Functions score MonsieurBiz\SyliusSearchPlugin\Search\Request\FunctionScore\Product\InStockWeightFunction: @@ -188,8 +205,8 @@ services: MonsieurBiz\SyliusSearchPlugin\Search\Request\ProductRequest\Search: arguments: $queryFilters: !tagged_iterator { tag: 'monsieurbiz.search.request.product_search_filter' } - $postFilterRegistry: '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\ProductTaxonRegistry' - $sorterRegistry: '@MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\ProductSorterRegistry' + $postFilters: !tagged_iterator { tag: 'monsieurbiz.search.request.product_post_filter' } + $sorters: !tagged_iterator { tag: 'monsieurbiz.search.request.product_sorter' } MonsieurBiz\SyliusSearchPlugin\Search\Request\ProductRequest\InstantSearch: arguments: @@ -198,8 +215,8 @@ services: MonsieurBiz\SyliusSearchPlugin\Search\Request\ProductRequest\Taxon: arguments: $queryFilters: !tagged_iterator { tag: 'monsieurbiz.search.request.product_taxon_filter' } - $postFilterRegistry: '@MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\ProductTaxonRegistry' - $sorterRegistry: '@MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\ProductSorterRegistry' + $postFilters: !tagged_iterator { tag: 'monsieurbiz.search.request.product_post_filter' } + $sorters: !tagged_iterator { tag: 'monsieurbiz.search.request.product_sorter' } # Define the filter builders MonsieurBiz\SyliusSearchPlugin\Search\ResponseFactory: diff --git a/src/Search/Request/PostFilter/PostFilterRegistryInterface.php b/src/Search/Request/PostFilter/PostFilterRegistryInterface.php deleted file mode 100644 index 74037c55..00000000 --- a/src/Search/Request/PostFilter/PostFilterRegistryInterface.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter; - -use Sylius\Component\Registry\ServiceRegistryInterface; - -interface PostFilterRegistryInterface extends ServiceRegistryInterface -{ -} diff --git a/src/Search/Request/PostFilter/ProductTaxonRegistry.php b/src/Search/Request/PostFilter/ProductTaxonRegistry.php deleted file mode 100644 index 8cdc1b25..00000000 --- a/src/Search/Request/PostFilter/ProductTaxonRegistry.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter; - -use Sylius\Component\Registry\ServiceRegistry; - -final class ProductTaxonRegistry extends ServiceRegistry implements PostFilterRegistryInterface -{ - public function __construct(array $queryFilters = []) - { - parent::__construct(PostFilterInterface::class, 'monsieurbiz.search'); - - foreach ($queryFilters as $queryFilter) { - $this->register(\get_class($queryFilter), $queryFilter); - } - } -} diff --git a/src/Search/Request/ProductRequest/InstantSearch.php b/src/Search/Request/ProductRequest/InstantSearch.php index 9ddbae6e..62111563 100644 --- a/src/Search/Request/ProductRequest/InstantSearch.php +++ b/src/Search/Request/ProductRequest/InstantSearch.php @@ -29,6 +29,9 @@ final class InstantSearch implements RequestInterface private ?RequestConfiguration $configuration; + /** + * @var iterable + */ private iterable $queryFilters; private FunctionScoreRegistryInterface $functionScoreRegistry; @@ -63,7 +66,6 @@ public function getQuery(): Query $qb = new QueryBuilder(); $boolQuery = $qb->query()->bool(); - /** @var QueryFilterInterface $queryFilter */ foreach ($this->queryFilters as $queryFilter) { $queryFilter->apply($boolQuery, $this->configuration); } diff --git a/src/Search/Request/ProductRequest/Search.php b/src/Search/Request/ProductRequest/Search.php index f8c6642f..38a87349 100644 --- a/src/Search/Request/ProductRequest/Search.php +++ b/src/Search/Request/ProductRequest/Search.php @@ -20,11 +20,11 @@ use MonsieurBiz\SyliusSearchPlugin\Repository\ProductOptionRepositoryInterface; use MonsieurBiz\SyliusSearchPlugin\Search\Request\AggregationBuilder; use MonsieurBiz\SyliusSearchPlugin\Search\Request\FunctionScore\FunctionScoreRegistryInterface; -use MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\PostFilterRegistryInterface; +use MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\PostFilterInterface; use MonsieurBiz\SyliusSearchPlugin\Search\Request\QueryFilter\QueryFilterInterface; use MonsieurBiz\SyliusSearchPlugin\Search\Request\RequestConfiguration; use MonsieurBiz\SyliusSearchPlugin\Search\Request\RequestInterface; -use MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\SorterRegistryInterface; +use MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\SorterInterface; use Sylius\Component\Registry\ServiceRegistryInterface; final class Search implements RequestInterface @@ -39,11 +39,20 @@ final class Search implements RequestInterface private AggregationBuilder $aggregationBuilder; + /** + * @var iterable + */ private iterable $queryFilters; - private PostFilterRegistryInterface $postFilterRegistry; + /** + * @var iterable + */ + private iterable $postFilters; - private SorterRegistryInterface $sorterRegistry; + /** + * @var iterable + */ + private iterable $sorters; private FunctionScoreRegistryInterface $functionScoreRegistry; @@ -53,8 +62,8 @@ public function __construct( ProductOptionRepositoryInterface $productOptionRepository, AggregationBuilder $aggregationBuilder, iterable $queryFilters, - PostFilterRegistryInterface $postFilterRegistry, - SorterRegistryInterface $sorterRegistry, + iterable $postFilters, + iterable $sorters, FunctionScoreRegistryInterface $functionScoreRegistry ) { /** @var DocumentableInterface $documentable */ @@ -64,8 +73,8 @@ public function __construct( $this->productOptionRepository = $productOptionRepository; $this->aggregationBuilder = $aggregationBuilder; $this->queryFilters = $queryFilters; - $this->postFilterRegistry = $postFilterRegistry; - $this->sorterRegistry = $sorterRegistry; + $this->postFilters = $postFilters; + $this->sorters = $sorters; $this->functionScoreRegistry = $functionScoreRegistry; } @@ -89,21 +98,20 @@ public function getQuery(): Query $qb = new QueryBuilder(); $boolQuery = $qb->query()->bool(); - /** @var QueryFilterInterface $queryFilter */ foreach ($this->queryFilters as $queryFilter) { $queryFilter->apply($boolQuery, $this->configuration); } $query = Query::create($boolQuery); $postFilter = new Query\BoolQuery(); - foreach ($this->postFilterRegistry->all() as $postFilterApplier) { + foreach ($this->postFilters as $postFilterApplier) { $postFilterApplier->apply($postFilter, $this->configuration); } $query->setPostFilter($postFilter); $this->addAggregations($query, $postFilter); - foreach ($this->sorterRegistry->all() as $sorter) { + foreach ($this->sorters as $sorter) { $sorter->apply($query, $this->configuration); } diff --git a/src/Search/Request/ProductRequest/Taxon.php b/src/Search/Request/ProductRequest/Taxon.php index 6b073354..5aee646d 100644 --- a/src/Search/Request/ProductRequest/Taxon.php +++ b/src/Search/Request/ProductRequest/Taxon.php @@ -20,11 +20,11 @@ use MonsieurBiz\SyliusSearchPlugin\Repository\ProductOptionRepositoryInterface; use MonsieurBiz\SyliusSearchPlugin\Search\Request\AggregationBuilder; use MonsieurBiz\SyliusSearchPlugin\Search\Request\FunctionScore\FunctionScoreRegistryInterface; -use MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\PostFilterRegistryInterface; +use MonsieurBiz\SyliusSearchPlugin\Search\Request\PostFilter\PostFilterInterface; use MonsieurBiz\SyliusSearchPlugin\Search\Request\QueryFilter\QueryFilterInterface; use MonsieurBiz\SyliusSearchPlugin\Search\Request\RequestConfiguration; use MonsieurBiz\SyliusSearchPlugin\Search\Request\RequestInterface; -use MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\SorterRegistryInterface; +use MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting\SorterInterface; use RuntimeException; use Sylius\Component\Channel\Context\ChannelContextInterface; use Sylius\Component\Registry\ServiceRegistryInterface; @@ -43,11 +43,20 @@ final class Taxon implements RequestInterface private ?RequestConfiguration $configuration; + /** + * @var iterable + */ private iterable $queryFilters; - private PostFilterRegistryInterface $postFilterRegistry; + /** + * @var iterable + */ + private iterable $postFilters; - private SorterRegistryInterface $sorterRegistry; + /** + * @var iterable + */ + private iterable $sorters; private FunctionScoreRegistryInterface $functionScoreRegistry; @@ -58,8 +67,8 @@ public function __construct( ChannelContextInterface $channelContext, AggregationBuilder $aggregationBuilder, iterable $queryFilters, - PostFilterRegistryInterface $postFilterRegistry, - SorterRegistryInterface $sorterRegistry, + iterable $postFilters, + iterable $sorters, FunctionScoreRegistryInterface $functionScoreRegistry ) { /** @var DocumentableInterface $documentable */ @@ -70,8 +79,8 @@ public function __construct( $this->channelContext = $channelContext; $this->aggregationBuilder = $aggregationBuilder; $this->queryFilters = $queryFilters; - $this->postFilterRegistry = $postFilterRegistry; - $this->sorterRegistry = $sorterRegistry; + $this->postFilters = $postFilters; + $this->sorters = $sorters; $this->functionScoreRegistry = $functionScoreRegistry; } @@ -90,21 +99,20 @@ public function getQuery(): Query $qb = new QueryBuilder(); $boolQuery = $qb->query()->bool(); - /** @var QueryFilterInterface $queryFilter */ foreach ($this->queryFilters as $queryFilter) { $queryFilter->apply($boolQuery, $this->configuration); } + $query = Query::create($boolQuery); $postFilter = new Query\BoolQuery(); - - foreach ($this->postFilterRegistry->all() as $postFilterApplier) { + foreach ($this->postFilters as $postFilterApplier) { $postFilterApplier->apply($postFilter, $this->configuration); } $query->setPostFilter($postFilter); $this->addAggregations($query, $postFilter); - foreach ($this->sorterRegistry->all() as $sorter) { + foreach ($this->sorters as $sorter) { $sorter->apply($query, $this->configuration); } diff --git a/src/Search/Request/Sorting/ProductSorterRegistry.php b/src/Search/Request/Sorting/ProductSorterRegistry.php deleted file mode 100644 index f06efd3e..00000000 --- a/src/Search/Request/Sorting/ProductSorterRegistry.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting; - -use Sylius\Component\Registry\ServiceRegistry; - -final class ProductSorterRegistry extends ServiceRegistry implements SorterRegistryInterface -{ - public function __construct(array $queryFilters = []) - { - parent::__construct(SorterInterface::class, 'monsieurbiz.search'); - - foreach ($queryFilters as $queryFilter) { - $this->register(\get_class($queryFilter), $queryFilter); - } - } -} diff --git a/src/Search/Request/Sorting/SorterRegistryInterface.php b/src/Search/Request/Sorting/SorterRegistryInterface.php deleted file mode 100644 index 05dd08cf..00000000 --- a/src/Search/Request/Sorting/SorterRegistryInterface.php +++ /dev/null @@ -1,20 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE.txt - * file that was distributed with this source code. - */ - -declare(strict_types=1); - -namespace MonsieurBiz\SyliusSearchPlugin\Search\Request\Sorting; - -use Sylius\Component\Registry\ServiceRegistryInterface; - -interface SorterRegistryInterface extends ServiceRegistryInterface -{ -}