From 760ebe9356364e6d1fff1c91f75e2c018dea923d Mon Sep 17 00:00:00 2001 From: Chris Hemmings Date: Mon, 13 May 2024 08:56:54 +0100 Subject: [PATCH] Add registerAliasForArgument for Indexes and Finders (#1934) --- doc/usage.md | 24 +++++++++++++++++++ .../FOSElasticaExtension.php | 14 +++++++++++ 2 files changed, 38 insertions(+) diff --git a/doc/usage.md b/doc/usage.md index 0ce447c47..6b524a5e9 100644 --- a/doc/usage.md +++ b/doc/usage.md @@ -291,3 +291,27 @@ with version 7.7 and above. Rewriting your code as follows is a possible workaro $boolQuery->addShould($fieldQuery); $boolQuery->addShould($tagsQuery); ``` + +Autowiring +----------- + +Indexes and Finders are setup to be used with named Autowiring. For example, if +we have an index defined as `blog.post`, we can autowire a controller like: + +```php +namespace App\Controller; + +use FOS\ElasticaBundle\Elastica\Index; +use FOS\ElasticaBundle\Finder\TransformedFinder; +use Symfony\Component\HttpFoundation\Response; +use Symfony\Component\Routing\Annotation\Route; + +#[Route('/default', name: 'app_default')] +public function index( + Index $blogPostIndex, + TransformedFinder $blogPostFinder +): Response { + $results = $blogPostFinder->findPaginated('search text'); + $resultsPage = $results->getCurrentPageResults(); +} +``` \ No newline at end of file diff --git a/src/DependencyInjection/FOSElasticaExtension.php b/src/DependencyInjection/FOSElasticaExtension.php index 0fd274092..24663bd20 100644 --- a/src/DependencyInjection/FOSElasticaExtension.php +++ b/src/DependencyInjection/FOSElasticaExtension.php @@ -13,6 +13,8 @@ use Elastica\Client as ElasticaClient; use FOS\ElasticaBundle\Elastica\Client; +use FOS\ElasticaBundle\Elastica\Index; +use FOS\ElasticaBundle\Finder\TransformedFinder; use FOS\ElasticaBundle\Manager\RepositoryManagerInterface; use Symfony\Component\Config\FileLocator; use Symfony\Component\DependencyInjection\ChildDefinition; @@ -207,6 +209,12 @@ private function loadIndexes(array $indexes, ContainerBuilder $container): void 'name' => $name, ]); + $container->registerAliasForArgument( + $indexId, + Index::class, + \sprintf('%s.%s', $name, 'index') + )->setPublic(true); + if (isset($index['client'])) { $client = $this->getClient($index['client']); @@ -652,6 +660,12 @@ private function loadTypeFinder(array $typeConfig, ContainerBuilder $container, $finderDef->replaceArgument(0, $indexRef); $finderDef->replaceArgument(1, new Reference($elasticaToModelId)); $container->setDefinition($finderId, $finderDef); + + $container->registerAliasForArgument( + $finderId, + TransformedFinder::class, + \sprintf('%s.%s', $indexName, 'finder') + )->setPublic(true); } $arguments = [$indexName, new Reference($finderId)];