Skip to content

Commit

Permalink
Add default value for prefix and handle empty string as default value
Browse files Browse the repository at this point in the history
  • Loading branch information
loevgaard committed Oct 23, 2024
1 parent e1dc7e5 commit 7441968
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public function getConfigTreeBuilder(): TreeBuilder
->defaultNull()
->end()
->scalarNode('prefix')
->defaultNull()
->defaultValue('%env(MEILISEARCH_PREFIX)%')
->info('If you want to prepend a string to the index name, you can set it here. This can be useful in a development setup where each developer has their own prefix. Notice that the environment is already prefixed by default, so you do not have to prefix that.')
->cannotBeEmpty()
->end()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private static function registerIndexesConfiguration(array $config, ContainerBui
IndexUidResolverInterface::class => new Reference(IndexUidResolverInterface::class),
MetadataFactoryInterface::class => new Reference(MetadataFactoryInterface::class),
]),
$index['prefix'],
'' === $index['prefix'] ? null : $index['prefix'],
]));

$indexRegistry->addMethodCall('add', [new Reference($indexServiceId)]);
Expand Down
6 changes: 4 additions & 2 deletions src/Resolver/IndexUid/IndexUidResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ public function resolve(Index $index): string

public function resolveFromIndexScope(IndexScope $indexScope): string
{
return strtolower(implode($this->separator, array_filter([
return strtolower(
implode($this->separator, array_filter([
$indexScope->index->prefix,
$this->environment,
$indexScope->index->name,
$indexScope->channelCode,
$indexScope->localeCode,
$indexScope->currencyCode,
])));
], static fn (?string $part): bool => ((string) $part) !== '')),
);
}
}
30 changes: 30 additions & 0 deletions tests/Unit/Resolver/IndexUid/IndexUidResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,34 @@ public function it_resolves_from_index_scope(): void

self::assertSame('prefix__prod__products__fashion_web__en_us__usd', $resolver->resolveFromIndexScope($indexScope));
}

/**
* @test
*/
public function it_handles_empty_strings(): void
{
$index = new Index('products', Product::class, [], new Container(), '');
$indexScope = new IndexScope($index, 'FASHION_WEB', 'en_US', 'USD');
$resolver = new IndexUidResolver(
$this->prophesize(IndexScopeProviderInterface::class)->reveal(),
'prod',
);

self::assertSame('prod__products__fashion_web__en_us__usd', $resolver->resolveFromIndexScope($indexScope));
}

/**
* @test
*/
public function it_handles_null_values(): void
{
$index = new Index('products', Product::class, [], new Container(), null);
$indexScope = new IndexScope($index, 'FASHION_WEB', 'en_US', 'USD');
$resolver = new IndexUidResolver(
$this->prophesize(IndexScopeProviderInterface::class)->reveal(),
'prod',
);

self::assertSame('prod__products__fashion_web__en_us__usd', $resolver->resolveFromIndexScope($indexScope));
}
}

0 comments on commit 7441968

Please sign in to comment.