Skip to content

Commit 5ad205c

Browse files
committed
Allow to use services for settings
1 parent e4411ca commit 5ad205c

File tree

10 files changed

+223
-2
lines changed

10 files changed

+223
-2
lines changed

src/Command/MeilisearchCreateCommand.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ protected function configure(): void
4242
->addOption('indices', 'i', InputOption::VALUE_OPTIONAL, 'Comma-separated list of index names');
4343
}
4444

45-
private function entitiesToIndex($indexes)
45+
private function entitiesToIndex($indexes): array
4646
{
4747
foreach ($indexes as $key => $index) {
4848
$entityClassName = $index['class'];
@@ -89,11 +89,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8989
throw new InvalidSettingName(sprintf('Invalid setting name: "%s"', $variable));
9090
}
9191

92+
if (isset($value['_service']) && is_callable($value['_service'])) {
93+
$value = $value['_service']();
94+
}
95+
9296
$task = $indexInstance->{$method}($value);
93-
$task = $indexInstance->getTask($task['taskUid']);
97+
$indexInstance->waitForTask($task['taskUid']);
9498

9599
if ('failed' === $task['status']) {
96100
throw new TaskException($task['error']);
101+
} else {
102+
$output->writeln('<info>Settings updated.</info>');
97103
}
98104
}
99105
}

src/Command/MeilisearchImportCommand.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,10 +155,15 @@ protected function execute(InputInterface $input, OutputInterface $output): int
155155
$indexInstance = $this->searchClient->index($index['name']);
156156
foreach ($index['settings'] as $variable => $value) {
157157
$method = sprintf('update%s', ucfirst($variable));
158+
158159
if (false === method_exists($indexInstance, $method)) {
159160
throw new InvalidSettingName(sprintf('Invalid setting name: "%s"', $variable));
160161
}
161162

163+
if (isset($value['_service']) && is_callable($value['_service'])) {
164+
$value = $value['_service']();
165+
}
166+
162167
// Update
163168
$task = $indexInstance->{$method}($value);
164169

src/DependencyInjection/MeilisearchExtension.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ public function load(array $configs, ContainerBuilder $container): void
3434
$config['prefix'] = $container->getParameter('kernel.environment').'_';
3535
}
3636

37+
foreach ($config['indices'] as $index => $indice) {
38+
$config['indices'][$index]['settings'] = $this->findReferences($config['indices'][$index]['settings']);
39+
}
40+
3741
$container->setParameter('meili_url', $config['url'] ?? null);
3842
$container->setParameter('meili_api_key', $config['api_key'] ?? null);
3943
$container->setParameter('meili_symfony_version', MeilisearchBundle::qualifiedVersion());
@@ -53,4 +57,22 @@ public function load(array $configs, ContainerBuilder $container): void
5357

5458
$container->setDefinition('search.service', $searchDefinition->setPublic(true));
5559
}
60+
61+
/**
62+
* @param array<mixed, mixed> $settings
63+
*
64+
* @return array<mixed, mixed>
65+
*/
66+
private function findReferences(array $settings): array
67+
{
68+
foreach ($settings as $key => $value) {
69+
if (is_array($value)) {
70+
$settings[$key] = $this->findReferences($value);
71+
} elseif ('_service' === substr((string) $key, -8) || str_starts_with((string) $value, '@') || 'service' === $key) {
72+
$settings[$key] = new Reference(ltrim($value, '@'));
73+
}
74+
}
75+
76+
return $settings;
77+
}
5678
}

tests/Entity/DynamicSettings.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Bundle\Tests\Entity;
6+
7+
use Doctrine\DBAL\Types\Types;
8+
use Doctrine\ORM\Mapping as ORM;
9+
10+
/**
11+
* @ORM\Entity
12+
*/
13+
#[ORM\Entity]
14+
class DynamicSettings
15+
{
16+
/**
17+
* @ORM\Id
18+
*
19+
* @ORM\Column(type="integer")
20+
*/
21+
#[ORM\Id]
22+
#[ORM\Column(type: Types::INTEGER)]
23+
private int $id;
24+
25+
/**
26+
* @ORM\Column(type="string")
27+
*/
28+
#[ORM\Column(type: Types::STRING)]
29+
private string $name;
30+
31+
public function __construct(int $id, string $name)
32+
{
33+
$this->id = $id;
34+
$this->name = $name;
35+
}
36+
37+
public function getId(): int
38+
{
39+
return $this->id;
40+
}
41+
42+
public function getName(): string
43+
{
44+
return $this->name;
45+
}
46+
}

tests/Integration/CommandsTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Meilisearch\Bundle\Tests\BaseKernelTestCase;
88
use Meilisearch\Bundle\Tests\Entity\DummyCustomGroups;
9+
use Meilisearch\Bundle\Tests\Entity\DynamicSettings;
910
use Meilisearch\Bundle\Tests\Entity\SelfNormalizable;
1011
use Meilisearch\Client;
1112
use Meilisearch\Endpoints\Indexes;
@@ -89,6 +90,11 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
8990
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Page entities into sf_phpunit__pages index (6 indexed since start)
9091
Importing for index Meilisearch\Bundle\Tests\Entity\SelfNormalizable
9192
Importing for index Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
93+
Importing for index Meilisearch\Bundle\Tests\Entity\DynamicSettings
94+
Settings updated.
95+
Settings updated.
96+
Settings updated.
97+
Settings updated.
9298
Importing for index Meilisearch\Bundle\Tests\Entity\Post
9399
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__posts index (6 indexed since start)
94100
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\Post entities into sf_phpunit__aggregated index (6 indexed since start)
@@ -114,6 +120,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
114120
Cleared sf_phpunit__pages index of Meilisearch\Bundle\Tests\Entity\Page
115121
Cleared sf_phpunit__self_normalizable index of Meilisearch\Bundle\Tests\Entity\SelfNormalizable
116122
Cleared sf_phpunit__dummy_custom_groups index of Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
123+
Cleared sf_phpunit__dynamic_settings index of Meilisearch\Bundle\Tests\Entity\DynamicSettings
117124
Done!
118125

119126
EOD, $clearOutput);
@@ -132,6 +139,7 @@ public function testSearchImportAndClearAndDeleteWithoutIndices(): void
132139
Deleted sf_phpunit__pages
133140
Deleted sf_phpunit__self_normalizable
134141
Deleted sf_phpunit__dummy_custom_groups
142+
Deleted sf_phpunit__dynamic_settings
135143
Done!
136144

137145
EOD, $clearOutput);
@@ -325,12 +333,20 @@ public function testSearchCreateWithoutIndices(): void
325333

326334
$this->assertSame(<<<'EOD'
327335
Creating index sf_phpunit__posts for Meilisearch\Bundle\Tests\Entity\Post
336+
Settings updated.
337+
Settings updated.
338+
Settings updated.
328339
Creating index sf_phpunit__comments for Meilisearch\Bundle\Tests\Entity\Comment
329340
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Tag
330341
Creating index sf_phpunit__tags for Meilisearch\Bundle\Tests\Entity\Link
331342
Creating index sf_phpunit__pages for Meilisearch\Bundle\Tests\Entity\Page
332343
Creating index sf_phpunit__self_normalizable for Meilisearch\Bundle\Tests\Entity\SelfNormalizable
333344
Creating index sf_phpunit__dummy_custom_groups for Meilisearch\Bundle\Tests\Entity\DummyCustomGroups
345+
Creating index sf_phpunit__dynamic_settings for Meilisearch\Bundle\Tests\Entity\DynamicSettings
346+
Settings updated.
347+
Settings updated.
348+
Settings updated.
349+
Settings updated.
334350
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Post
335351
Creating index sf_phpunit__aggregated for Meilisearch\Bundle\Tests\Entity\Tag
336352
Done!
@@ -350,6 +366,9 @@ public function testSearchCreateWithIndices(): void
350366

351367
$this->assertSame(<<<'EOD'
352368
Creating index sf_phpunit__posts for Meilisearch\Bundle\Tests\Entity\Post
369+
Settings updated.
370+
Settings updated.
371+
Settings updated.
353372
Done!
354373

355374
EOD, $createOutput);
@@ -437,4 +456,57 @@ public function testImportsDummyWithCustomGroups(): void
437456
],
438457
], $this->client->index('sf_phpunit__dummy_custom_groups')->getDocuments()->getResults());
439458
}
459+
460+
/**
461+
* @testWith ["meili:create"]
462+
* ["meili:import"]
463+
*/
464+
public function testImportWithDynamicSettings(string $command): void
465+
{
466+
for ($i = 0; $i <= 5; ++$i) {
467+
$this->entityManager->persist(new DynamicSettings($i, "Dynamic $i"));
468+
}
469+
470+
$this->entityManager->flush();
471+
472+
$importCommand = $this->application->find($command);
473+
$importCommandTester = new CommandTester($importCommand);
474+
$importCommandTester->execute(['--indices' => 'dynamic_settings']);
475+
476+
$importOutput = $importCommandTester->getDisplay();
477+
478+
if ('meili:import' === $command) {
479+
$this->assertSame(<<<'EOD'
480+
Importing for index Meilisearch\Bundle\Tests\Entity\DynamicSettings
481+
Indexed a batch of 6 / 6 Meilisearch\Bundle\Tests\Entity\DynamicSettings entities into sf_phpunit__dynamic_settings index (6 indexed since start)
482+
Settings updated.
483+
Settings updated.
484+
Settings updated.
485+
Settings updated.
486+
Done!
487+
488+
EOD, $importOutput);
489+
} else {
490+
$this->assertSame(<<<'EOD'
491+
Creating index sf_phpunit__dynamic_settings for Meilisearch\Bundle\Tests\Entity\DynamicSettings
492+
Settings updated.
493+
Settings updated.
494+
Settings updated.
495+
Settings updated.
496+
Done!
497+
498+
EOD, $importOutput);
499+
}
500+
501+
$settings = $this->get('search.client')->index('sf_phpunit__dynamic_settings')->getSettings();
502+
503+
$getSetting = static function ($value) {
504+
return $value instanceof \IteratorAggregate ? iterator_to_array($value) : $value;
505+
};
506+
507+
self::assertSame(['publishedAt', 'title'], $getSetting($settings['filterableAttributes']));
508+
self::assertSame(['title'], $getSetting($settings['searchableAttributes']));
509+
self::assertSame(['a', 'n', 'the'], $getSetting($settings['stopWords']));
510+
self::assertSame(['fantastic' => ['great'], 'great' => ['fantastic']], $getSetting($settings['synonyms']));
511+
}
440512
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Bundle\Tests\Integration\Fixtures;
6+
7+
class FilterableAttributes
8+
{
9+
public function __invoke(): array
10+
{
11+
return ['title', 'publishedAt'];
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Bundle\Tests\Integration\Fixtures;
6+
7+
class SearchableAttributes
8+
{
9+
public function __invoke(): array
10+
{
11+
return ['title'];
12+
}
13+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Bundle\Tests\Integration\Fixtures;
6+
7+
class StopWords
8+
{
9+
public function __invoke(): array
10+
{
11+
return ['the', 'a', 'n'];
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Meilisearch\Bundle\Tests\Integration\Fixtures;
6+
7+
class Synonyms
8+
{
9+
public function __invoke(): array
10+
{
11+
return [
12+
'great' => ['fantastic'],
13+
'fantastic' => ['great'],
14+
];
15+
}
16+
}

tests/config/meilisearch.yaml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,18 @@ meilisearch:
4040
class: 'Meilisearch\Bundle\Tests\Entity\DummyCustomGroups'
4141
enable_serializer_groups: true
4242
serializer_groups: ['public', 'private']
43+
- name: dynamic_settings
44+
class: 'Meilisearch\Bundle\Tests\Entity\DynamicSettings'
45+
settings:
46+
filterableAttributes:
47+
_service: '@Meilisearch\Bundle\Tests\Integration\Fixtures\FilterableAttributes'
48+
searchableAttributes:
49+
_service: '@Meilisearch\Bundle\Tests\Integration\Fixtures\SearchableAttributes'
50+
stopWords:
51+
_service: '@Meilisearch\Bundle\Tests\Integration\Fixtures\StopWords'
52+
synonyms:
53+
_service: '@Meilisearch\Bundle\Tests\Integration\Fixtures\Synonyms'
54+
55+
services:
56+
Meilisearch\Bundle\Tests\Integration\Fixtures\:
57+
resource: '../Integration/Fixtures/'

0 commit comments

Comments
 (0)