Skip to content

Commit 932586e

Browse files
authored
Merge pull request #64 from dfeyer/task-dynamic-index-settings
FEATURE: Add support for dynamic index settings
2 parents 9c4cd0f + 48678ba commit 932586e

File tree

3 files changed

+130
-3
lines changed

3 files changed

+130
-3
lines changed

Classes/Domain/Model/Index.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@
1212
*/
1313

1414
use Flowpack\ElasticSearch\Exception as ElasticSearchException;
15+
use Flowpack\ElasticSearch\Service\DynamicIndexSettingService;
1516
use Flowpack\ElasticSearch\Transfer\Response;
1617
use Neos\Utility\Arrays;
18+
use Neos\Flow\Annotations as Flow;
1719

1820
/**
1921
* Representation of an Index
@@ -60,6 +62,12 @@ class Index
6062
'index.warmer.enabled',
6163
];
6264

65+
/**
66+
* @var DynamicIndexSettingService
67+
* @Flow\Inject
68+
*/
69+
protected $dynamicIndexSettingService;
70+
6371
/**
6472
* @var string
6573
*/
@@ -177,12 +185,13 @@ public function create()
177185
protected function getSettings()
178186
{
179187
if ($this->client instanceof Client) {
180-
$settings = Arrays::getValueByPath($this->settings, 'indexes.' . $this->client->getBundle() . '.' . $this->settingsKey) ?: Arrays::getValueByPath($this->settings, 'indexes.default' . '.' . $this->settingsKey);
188+
$path = 'indexes.' . $this->client->getBundle() . '.' . $this->settingsKey;
181189
} else {
182-
$settings = Arrays::getValueByPath($this->settings, 'indexes.default' . '.' . $this->settingsKey);
190+
$path = 'indexes.default' . '.' . $this->settingsKey;
183191
}
192+
$settings = Arrays::getValueByPath($this->settings, $path);
184193

185-
return $settings;
194+
return $this->dynamicIndexSettingService->process($settings, $path, $this->getName());
186195
}
187196

188197
/**
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
namespace Flowpack\ElasticSearch\Service;
3+
4+
/*
5+
* This file is part of the Flowpack.ElasticSearch package.
6+
*
7+
* (c) Contributors of the Flowpack Team - flowpack.org
8+
*
9+
* This package is Open Source Software. For the full copyright and license
10+
* information, please view the LICENSE file which was distributed with this
11+
* source code.
12+
*/
13+
14+
use Neos\Flow\ObjectManagement\ObjectManagerInterface;
15+
use Neos\Flow\Annotations as Flow;
16+
use Neos\Flow\Reflection\ReflectionService;
17+
use Neos\Utility\PositionalArraySorter;
18+
19+
/**
20+
* Transform indices settings dynamically
21+
*
22+
* @Flow\Scope("singleton")
23+
*/
24+
final class DynamicIndexSettingService
25+
{
26+
/**
27+
* @Flow\Inject
28+
* @var ObjectManagerInterface
29+
*/
30+
protected $objectManager;
31+
32+
/**
33+
* @param array $settings
34+
* @param string $path
35+
* @param string $indexName
36+
* @return array
37+
*/
38+
public function process(array $settings, $path, $indexName)
39+
{
40+
foreach (static::getAllProcessors($this->objectManager) as $configuration) {
41+
/** @var IndexSettingProcessorInterface $processor */
42+
$processor = $this->objectManager->get($configuration['className']);
43+
if ($processor->canProcess($settings, $path)) {
44+
$settings = $processor->process($settings, $path, $indexName);
45+
}
46+
}
47+
48+
return $settings;
49+
}
50+
51+
/**
52+
* Returns all class names implementing the IndexSettingProcessorInterface.
53+
*
54+
* @Flow\CompileStatic
55+
* @param ObjectManagerInterface $objectManager
56+
* @return array
57+
*/
58+
public static function getAllProcessors($objectManager)
59+
{
60+
/** @var ReflectionService $reflectionService */
61+
$reflectionService = $objectManager->get(ReflectionService::class);
62+
$processorClassNames = $reflectionService->getAllImplementationClassNamesForInterface(IndexSettingProcessorInterface::class);
63+
64+
$processors = [];
65+
foreach ($processorClassNames as $processorClassName) {
66+
$processors[$processorClassName] = [
67+
'priority' => $processorClassName::getPriority(),
68+
'className' => $processorClassName
69+
];
70+
}
71+
72+
return array_reverse(
73+
(new PositionalArraySorter($processors, 'priority'))->toArray()
74+
);
75+
}
76+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace Flowpack\ElasticSearch\Service;
3+
4+
/*
5+
* This file is part of the Flowpack.ElasticSearch package.
6+
*
7+
* (c) Contributors of the Flowpack Team - flowpack.org
8+
*
9+
* This package is Open Source Software. For the full copyright and license
10+
* information, please view the LICENSE file which was distributed with this
11+
* source code.
12+
*/
13+
14+
/**
15+
* Index setting processor
16+
*/
17+
interface IndexSettingProcessorInterface
18+
{
19+
/**
20+
* @return int
21+
*/
22+
public static function getPriority();
23+
24+
/**
25+
* Check if the given setting can be processed
26+
*
27+
* @param array $settings
28+
* @param string $path
29+
* @return array the processed settings passed to the next processor
30+
*/
31+
public function canProcess(array $settings, $path);
32+
33+
/**
34+
* Process index settings
35+
*
36+
* @param array $settings
37+
* @param string $path
38+
* @param string $indexName
39+
* @return array the processed settings passed to the next processor
40+
*/
41+
public function process(array $settings, $path, $indexName);
42+
}

0 commit comments

Comments
 (0)