Skip to content

Commit

Permalink
feat: add new setting to define config paths to override mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
delyriand committed Jul 24, 2023
1 parent 70c83de commit db84eb7
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 70 deletions.
5 changes: 5 additions & 0 deletions UPGRADE-2.0.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# UPGRADE FROM v2.0.x TO v2.1.x

- 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

1. We've changed the `.env` variable to add a messenger transport DSN to manage reindex queue.
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.

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 <sylius@monsieurbiz.com>
*
* 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);
}
}
51 changes: 31 additions & 20 deletions src/Mapping/YamlWithLocaleProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,57 @@

use ArrayObject;
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 = [];
foreach ($this->configurationDirectories as $configurationDirectory) {
$configurationDirectory = $this->fileLocator->locate($configurationDirectory);
if (!\is_string($configurationDirectory)) {
continue;
}
$yamlProvider = $this->yamlProviderFactory->create($configurationDirectory, $this->parser);
$mapping = array_merge_recursive($mapping, $yamlProvider->provideMapping($context['index_code'] ?? $indexName, $context) ?? []);

$locale = $context['locale'] ?? null;
if (null !== $locale) {
$mapping = $this->appendLocaleAnalyzers($mapping, $locale);
$locale = $context['locale'] ?? null;
if (null !== $locale) {
$mapping = $this->appendLocaleAnalyzers($configurationDirectory, $mapping, $locale);
}
}

$mappingProviderEvent = new MappingProviderEvent($context['index_code'] ?? $indexName, new ArrayObject($mapping));
Expand All @@ -66,10 +77,10 @@ public function provideMapping(string $indexName, array $context = []): ?array
return (array) $mappingProviderEvent->getMapping();
}

private function appendLocaleAnalyzers(array $mapping, string $locale): array
private function appendLocaleAnalyzers(string $configurationDirectory, array $mapping, string $locale): array
{
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'
9 changes: 3 additions & 6 deletions src/Resources/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,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%'

# Defines our registries
monsieurbiz.search.registry.documentable:
Expand Down Expand Up @@ -223,7 +220,7 @@ services:
- { name: kernel.event_listener, event: sylius.product.post_update, method: dispatchProductReindexMessage }
- { name: kernel.event_listener, event: sylius.product.pre_delete, method: saveProductIdToDispatchReindexMessage }
- { name: kernel.event_listener, event: sylius.product.post_delete, method: dispatchDeleteProductReindexMessage }

MonsieurBiz\SyliusSearchPlugin\EventListener\ProductVariantEventListener:
tags:
- { name: kernel.event_listener, event: sylius.product_variant.post_create, method: dispatchProductVariantReindexMessage }
Expand Down

0 comments on commit db84eb7

Please sign in to comment.