jolicode / elastically

🔍 JoliCode's Elastica wrapper to bootstrap Elasticsearch PHP integrations
248 stars 36 forks source link

Symfony config + HttpClient gives "Invalid transport" #139

Closed jmsche closed 1 year ago

jmsche commented 1 year ago

Hi,

I'm currently testing the new Symfony bundle, and I'm facing this issue when trying to index data:

In AbstractTransport.php line 129:

  [Elastica\Exception\InvalidException]  
  Invalid transport 

When I dump() the $transport before the if() statement, I get a string instead of a service: @JoliCode\Elastically\Transport\HttpClientTransport

Here is my (truncated) Elastically config:

services:
    _defaults:
        autowire: true
        autoconfigure: true

    JoliCode\Elastically\Messenger\DocumentExchangerInterface:
        alias: App\Elasticsearch\DocumentExchanger

    JoliCode\Elastically\Transport\HttpClientTransport: ~

elastically:
    connections:
        default:
            client:
                host: '%env(ELASTICSEARCH_HOST)%'
                port: '%env(ELASTICSEARCH_PORT)%'
                transport: '@JoliCode\Elastically\Transport\HttpClientTransport'
            mapping_directory: '%kernel.project_dir%/config/elasticsearch'
            bulk_size: 100
            prefix: formations_%kernel.environment%
            index_class_mapping:
                sector: App\Elasticsearch\Model\Sector

framework:
    messenger:
        transports:
            sync: 'sync://'
        routing:
            'JoliCode\Elastically\Messenger\IndexationRequest': sync

Did I miss something?

lyrixx commented 1 year ago

Hmm, the @ in front of the transport value is suspicious.

And I checked, and I totally forgot to replace the transport by a Reference if it exist. Can you try something like :

diff --git a/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php b/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php
index 9e4e216..1a8e3db 100644
--- a/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php
+++ b/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php
@@ -64,6 +64,9 @@ class ElasticallyExtension extends Extension
         $container->setDefinition("elastically.{$name}.result_set_builder", $resultSetBuilder);

         $client = new ChildDefinition('elastically.abstract.client');
+        if (array_key_exists('transport', $config)) {
+            $config['transport'] = new Reference($config['transport']);
+        }
         $client->replaceArgument('$config', $config['client']);
         $client->replaceArgument('$resultSetBuilder', new Reference("elastically.{$name}.result_set_builder"));
         $client->replaceArgument('$indexNameMapper', new Reference("elastically.{$name}.index_name_mapper"));

https://github.com/jolicode/elastically/blob/master/src/Bridge/Symfony/DependencyInjection/ElasticallyExtension.php#L66

Note: you must relove the leading @

Korbeil commented 1 year ago

I fixed the issue in #140 by using @lyrixx patch & recommendation. As he said, using @ won't work, but even by removing it, it wouldn't work without the patch he shared. If you have some time could you check / test the PR ?