Skip to content

Commit

Permalink
Add registerAliasForArgument for Indexes and Finders
Browse files Browse the repository at this point in the history
  • Loading branch information
chrishemmings committed Dec 11, 2023
1 parent f44abba commit 7ef31ce
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
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
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 @@ -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']);

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

0 comments on commit 7ef31ce

Please sign in to comment.