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

Simplified mapping extended #167

Merged
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
2 changes: 2 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
- 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`
- The `MonsieurBiz\SyliusSearchPlugin\Mapping\YamlWithLocaleProvider` is no longer a decorator. Some constructor parameters are removed : `$decorated`, `$configurationDirectory` and `$attributeRepository`, and we have `$yamlProviderFactory`, `$fileLocator` and `$configurationDirectories`.
- New setting `monsieurbiz_sylius_search.elastically_configuration_paths` to define paths of elasticsearch mapping files. By default it's `['@MonsieurBizSyliusSearchPlugin/Resources/config/elasticsearch']`.

# UPGRADE FROM v1.X.X TO v2.0.x

Expand Down
3 changes: 3 additions & 0 deletions dist/config/packages/monsieurbiz_sylius_search_plugin.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ monsieurbiz_sylius_search:
documents:
monsieurbiz_product:
prefix: 'myprefix' # define a custom index prefix

elastically_configuration_paths:
- '%kernel.project_dir%/src/Resources/config/elasticsearch'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
mappings:
properties:
short_description:
type: text
44 changes: 0 additions & 44 deletions dist/src/Search/EventListener/AppendProductMappingSubscriber.php

This file was deleted.

3 changes: 2 additions & 1 deletion docs/add_custom_values.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

In our example we will add the `short_description` product field to the indexed content.

- [Use event listener to change the product mapping](../dist/src/Search/EventListener/AppendProductMappingSubscriber.php)
- [Add your elasticsearch config path in `monsieurbiz_sylius_search.elastically_configuration_paths`](../dist/config/packages/monsieurbiz_sylius_search_plugin.yaml#L9)
- [Extends the product mapping to add the field](../dist/src/Resources/config/elasticsearch/monsieurbiz_product_mapping.yaml)
- [Add a decorator for ProductMapperConfiguration](../dist/src/Resources/config/services.yaml)
- [Create DecorateProductMapperConfiguration class](../dist/src/Search/Automapper/DecorateProductMapperConfiguration.php)

Expand Down
4 changes: 4 additions & 0 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ public function getConfigTreeBuilder(): TreeBuilder
->end()
->end()
->end()
->arrayNode('elastically_configuration_paths')
->defaultValue([])
->prototype('scalar')->end()
->end()
->end()
;

Expand Down
25 changes: 25 additions & 0 deletions src/Factory/YamlProviderFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

/*
* This file is part of Monsieur Biz' Search plugin for Sylius.
*
* (c) Monsieur Biz <[email protected]>
*
* 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\Factory;

use JoliCode\Elastically\Mapping\YamlProvider;
use Symfony\Component\Yaml\Parser;

class YamlProviderFactory
{
public function create(string $configurationDirectory, Parser $parser): YamlProvider
{
return new YamlProvider($configurationDirectory, $parser);
}
}
72 changes: 51 additions & 21 deletions src/Mapping/YamlWithLocaleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,47 +14,55 @@
namespace MonsieurBiz\SyliusSearchPlugin\Mapping;

use ArrayObject;
use Elastica\Exception\InvalidException;
use JoliCode\Elastically\Mapping\MappingProviderInterface;
use JoliCode\Elastically\Mapping\YamlProvider;
use MonsieurBiz\SyliusSearchPlugin\Event\MappingProviderEvent;
use MonsieurBiz\SyliusSearchPlugin\Repository\ProductAttributeRepositoryInterface;
use MonsieurBiz\SyliusSearchPlugin\Factory\YamlProviderFactory;
use Symfony\Component\Config\FileLocatorInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Yaml\Exception\ParseException;
use Symfony\Component\Yaml\Parser;

class YamlWithLocaleProvider implements MappingProviderInterface
{
private YamlProvider $decorated;
private EventDispatcherInterface $eventDispatcher;

private string $configurationDirectory;
private YamlProviderFactory $yamlProviderFactory;

private Parser $parser;
private FileLocatorInterface $fileLocator;

private ProductAttributeRepositoryInterface $attributeRepository;
/**
* @var array<string>
*/
private iterable $configurationDirectories;

private EventDispatcherInterface $eventDispatcher;
private Parser $parser;

public function __construct(
YamlProvider $decorated,
string $configurationDirectory,
EventDispatcherInterface $eventDispatcher,
ProductAttributeRepositoryInterface $attributeRepository,
YamlProviderFactory $yamlProviderFactory,
FileLocatorInterface $fileLocator,
iterable $configurationDirectories = [],
?Parser $parser = null
) {
$this->decorated = $decorated;
$this->configurationDirectory = $configurationDirectory;
$this->parser = $parser ?? new Parser();
$this->attributeRepository = $attributeRepository;
$this->eventDispatcher = $eventDispatcher;
$this->yamlProviderFactory = $yamlProviderFactory;
$this->fileLocator = $fileLocator;
$this->configurationDirectories = $configurationDirectories;
$this->parser = $parser ?? new Parser();
}

public function provideMapping(string $indexName, array $context = []): ?array
{
$mapping = $this->decorated->provideMapping($context['index_code'] ?? $indexName, $context) ?? [];

$mapping = [];
$locale = $context['locale'] ?? null;
if (null !== $locale) {
$mapping = $this->appendLocaleAnalyzers($mapping, $locale);
foreach ($this->configurationDirectories as $configurationDirectory) {
$configurationDirectory = $this->fileLocator->locate($configurationDirectory);
if (!\is_string($configurationDirectory)) {
continue;
}
$mapping = $this->appendMapping($configurationDirectory, $mapping, $indexName, $context);
$mapping = $this->appendLocaleAnalyzers($configurationDirectory, $mapping, $locale);
}

$mappingProviderEvent = new MappingProviderEvent($context['index_code'] ?? $indexName, new ArrayObject($mapping));
Expand All @@ -63,13 +71,35 @@ public function provideMapping(string $indexName, array $context = []): ?array
MappingProviderEvent::EVENT_NAME
);

return (array) $mappingProviderEvent->getMapping();
$mapping = (array) $mappingProviderEvent->getMapping();
if (empty($mapping['mappings'] ?? [])) {
throw new InvalidException(sprintf('Mapping no found for "%s" not found. Please check your configuration.', $indexName));
}

return $mapping;
}

private function appendMapping(string $configurationDirectory, array $mapping, string $indexName, array $context): array
{
$yamlProvider = $this->yamlProviderFactory->create($configurationDirectory, $this->parser);

try {
$mapping = array_merge_recursive($mapping, $yamlProvider->provideMapping($context['index_code'] ?? $indexName, $context) ?? []);
} catch (InvalidException $exception) {
// the mapping yaml file does not exist.
}

return $mapping;
}

private function appendLocaleAnalyzers(array $mapping, string $locale): array
private function appendLocaleAnalyzers(string $configurationDirectory, array $mapping, ?string $locale): array
{
if (null === $locale) {
return $mapping;
}

foreach ($this->getLocaleCode($locale) as $localeCode) {
$analyzerFilePath = $this->configurationDirectory . \DIRECTORY_SEPARATOR . 'analyzers_' . $localeCode . '.yaml';
$analyzerFilePath = $configurationDirectory . \DIRECTORY_SEPARATOR . 'analyzers_' . $localeCode . '.yaml';

try {
$analyzer = $this->parser->parseFile($analyzerFilePath) ?? [];
Expand Down
2 changes: 2 additions & 0 deletions src/Resources/config/monsieurbiz_search.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ monsieurbiz_sylius_search:
product_variant: 'MonsieurBiz\SyliusSearchPlugin\Model\Product\VariantDTO'
pricing: 'MonsieurBiz\SyliusSearchPlugin\Generated\Model\PricingDTO'

elastically_configuration_paths:
- '@MonsieurBizSyliusSearchPlugin/Resources/config/elasticsearch'
7 changes: 2 additions & 5 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,10 @@ services:
$attributeRepository: '@sylius.repository.product_attribute'

# Define our mapping provider
JoliCode\Elastically\Mapping\YamlProvider:
arguments:
$configurationDirectory: '@=service("file_locator").locate("@MonsieurBizSyliusSearchPlugin/Resources/config/elasticsearch")'
MonsieurBiz\SyliusSearchPlugin\Mapping\YamlWithLocaleProvider:
decorates: JoliCode\Elastically\Mapping\YamlProvider
arguments:
$decorated: '@.inner'
$fileLocator: '@file_locator'
$configurationDirectories: '%monsieurbiz.search.config.elastically_configuration_paths%'

# Automapper configuration
MonsieurBiz\SyliusSearchPlugin\AutoMapper\Configuration:
Expand Down
Loading