Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add registerAliasForArgument for Indexes and Finders #1934

Merged
merged 1 commit into from
May 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,8 @@
},
"config": {
"allow-plugins": {
"phpstan/extension-installer": true,
"ergebnis/composer-normalize": true
"ergebnis/composer-normalize": true,
"phpstan/extension-installer": true
}
},
"extra": {
Expand Down
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 @@ -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
Loading