Skip to content

Commit 760ebe9

Browse files
Add registerAliasForArgument for Indexes and Finders (#1934)
1 parent 86fca8a commit 760ebe9

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

doc/usage.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -291,3 +291,27 @@ with version 7.7 and above. Rewriting your code as follows is a possible workaro
291291
$boolQuery->addShould($fieldQuery);
292292
$boolQuery->addShould($tagsQuery);
293293
```
294+
295+
Autowiring
296+
-----------
297+
298+
Indexes and Finders are setup to be used with named Autowiring. For example, if
299+
we have an index defined as `blog.post`, we can autowire a controller like:
300+
301+
```php
302+
namespace App\Controller;
303+
304+
use FOS\ElasticaBundle\Elastica\Index;
305+
use FOS\ElasticaBundle\Finder\TransformedFinder;
306+
use Symfony\Component\HttpFoundation\Response;
307+
use Symfony\Component\Routing\Annotation\Route;
308+
309+
#[Route('/default', name: 'app_default')]
310+
public function index(
311+
Index $blogPostIndex,
312+
TransformedFinder $blogPostFinder
313+
): Response {
314+
$results = $blogPostFinder->findPaginated('search text');
315+
$resultsPage = $results->getCurrentPageResults();
316+
}
317+
```

src/DependencyInjection/FOSElasticaExtension.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use Elastica\Client as ElasticaClient;
1515
use FOS\ElasticaBundle\Elastica\Client;
16+
use FOS\ElasticaBundle\Elastica\Index;
17+
use FOS\ElasticaBundle\Finder\TransformedFinder;
1618
use FOS\ElasticaBundle\Manager\RepositoryManagerInterface;
1719
use Symfony\Component\Config\FileLocator;
1820
use Symfony\Component\DependencyInjection\ChildDefinition;
@@ -207,6 +209,12 @@ private function loadIndexes(array $indexes, ContainerBuilder $container): void
207209
'name' => $name,
208210
]);
209211

212+
$container->registerAliasForArgument(
213+
$indexId,
214+
Index::class,
215+
\sprintf('%s.%s', $name, 'index')
216+
)->setPublic(true);
217+
210218
if (isset($index['client'])) {
211219
$client = $this->getClient($index['client']);
212220

@@ -652,6 +660,12 @@ private function loadTypeFinder(array $typeConfig, ContainerBuilder $container,
652660
$finderDef->replaceArgument(0, $indexRef);
653661
$finderDef->replaceArgument(1, new Reference($elasticaToModelId));
654662
$container->setDefinition($finderId, $finderDef);
663+
664+
$container->registerAliasForArgument(
665+
$finderId,
666+
TransformedFinder::class,
667+
\sprintf('%s.%s', $indexName, 'finder')
668+
)->setPublic(true);
655669
}
656670

657671
$arguments = [$indexName, new Reference($finderId)];

0 commit comments

Comments
 (0)