Skip to content

Commit

Permalink
refactor: use tagged_iterator for post filters and sorters
Browse files Browse the repository at this point in the history
  • Loading branch information
delyriand committed Jul 24, 2023
1 parent b8e1033 commit 4a423c8
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 141 deletions.
2 changes: 2 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
59 changes: 38 additions & 21 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down
20 changes: 0 additions & 20 deletions src/Search/Request/PostFilter/PostFilterRegistryInterface.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Search/Request/PostFilter/ProductTaxonRegistry.php

This file was deleted.

4 changes: 3 additions & 1 deletion src/Search/Request/ProductRequest/InstantSearch.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ final class InstantSearch implements RequestInterface

private ?RequestConfiguration $configuration;

/**
* @var iterable<QueryFilterInterface>
*/
private iterable $queryFilters;

private FunctionScoreRegistryInterface $functionScoreRegistry;
Expand Down Expand Up @@ -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);
}
Expand Down
30 changes: 19 additions & 11 deletions src/Search/Request/ProductRequest/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -39,11 +39,20 @@ final class Search implements RequestInterface

private AggregationBuilder $aggregationBuilder;

/**
* @var iterable<QueryFilterInterface>
*/
private iterable $queryFilters;

private PostFilterRegistryInterface $postFilterRegistry;
/**
* @var iterable<PostFilterInterface>
*/
private iterable $postFilters;

private SorterRegistryInterface $sorterRegistry;
/**
* @var iterable<SorterInterface>
*/
private iterable $sorters;

private FunctionScoreRegistryInterface $functionScoreRegistry;

Expand All @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand Down
32 changes: 20 additions & 12 deletions src/Search/Request/ProductRequest/Taxon.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,11 +43,20 @@ final class Taxon implements RequestInterface

private ?RequestConfiguration $configuration;

/**
* @var iterable<QueryFilterInterface>
*/
private iterable $queryFilters;

private PostFilterRegistryInterface $postFilterRegistry;
/**
* @var iterable<PostFilterInterface>
*/
private iterable $postFilters;

private SorterRegistryInterface $sorterRegistry;
/**
* @var iterable<SorterInterface>
*/
private iterable $sorters;

private FunctionScoreRegistryInterface $functionScoreRegistry;

Expand All @@ -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 */
Expand All @@ -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;
}

Expand All @@ -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);
}

Expand Down
28 changes: 0 additions & 28 deletions src/Search/Request/Sorting/ProductSorterRegistry.php

This file was deleted.

20 changes: 0 additions & 20 deletions src/Search/Request/Sorting/SorterRegistryInterface.php

This file was deleted.

0 comments on commit 4a423c8

Please sign in to comment.