Skip to content

Commit

Permalink
Add registerAliasForArgument for Indexes and Finders (#1934)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishemmings authored May 13, 2024
1 parent 86fca8a commit 760ebe9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
24 changes: 24 additions & 0 deletions doc/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
```
14 changes: 14 additions & 0 deletions src/DependencyInjection/FOSElasticaExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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']);

Expand Down Expand Up @@ -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)];
Expand Down

0 comments on commit 760ebe9

Please sign in to comment.