From 7ef31cebb1987c7c53273cb7d2e02f86f978c56d Mon Sep 17 00:00:00 2001 From: Chris Hemmings Date: Mon, 11 Dec 2023 16:40:28 +0000 Subject: [PATCH] Add registerAliasForArgument for Indexes and Finders --- 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 906be6db0..5911c2d7f 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; @@ -205,6 +207,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']); @@ -650,6 +658,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)];