geocoder-php/BazingaGeocoderBundle

Option httplug_client resolved as string instead of HttpClient instance

Richtermeister opened this issue · 11 comments

I am trying to configure the httplug_client option for the GoogleMapsFactory provider and encounter the following exception:

Symfony\Component\OptionsResolver\Exception\InvalidOptionsException: The option "httplug_client" with value "@http_adapter" is expected to be of type "Http\Client\HttpClient" or "null", but is of type "string".
My config:

services:
    http_adapter:
        class: Http\Adapter\Guzzle6\Client

bazinga_geocoder:
    providers:
        google_maps:
            factory: Bazinga\GeocoderBundle\ProviderFactory\GoogleMapsFactory
            options:
                api_key: "%google.api_key%"
                httplug_client: "@http_adapter" // does not work, and neither does just "http_adapter"

Looking into this, the validation is executed before services are being resolved (https://github.com/geocoder-php/BazingaGeocoderBundle/blob/master/DependencyInjection/BazingaGeocoderExtension.php#L68-L71), however, swapping these steps fails as well, since the resolution results in Symfony\Component\DependencyInjection\Reference objects, which fail the test for Http\Client\HttpClient here: https://github.com/geocoder-php/BazingaGeocoderBundle/blob/master/ProviderFactory/GoogleMapsFactory.php#L42

Happy to help solve this issue. My recommendation is to move the Provider options validation to a compiler pass which runs after references have been resolved.
Please let me know if I am overlooking something obvious, and whether my recommendation is an acceptable fix, or whether there are better ways.

Thank you for your time.

Thank you.
Moving validation to a compiler pass will not fail at build as far as I know. It was a while since I looked into this.

Are we sure that we run the findReference function on the httplug_client option? We should run that on all options.

True, I just realized a compiler pass will always just deal with references, not the actual resolved class.

And yes, the findReferences function runs on the httplug_client option and turns it into an instance of Reference, but that fails the test for HttpClient.

this is broke for me too

Anyone made any progress on this? I'm attempting to use Httplug Bundle to do this and the optionhttplug_client: 'httplug.client' (as instructed in the docs) throws the same error and gets resolved as a string.

I’ll see if I can reproduce the error

Sorry, I made a mistake not submitting any PR. Anyhow. This issue is fixed in master

Pulled down master and attempted this again. Same results and same InvalidConfigurationException thrown except you changed the allowed type to an object. I changed it to allow a string in my vendor files and everything works fine. Not sure why that is.

Could you provide your config here please?

Sure thing:

# Http Plug Bundle for Guzzle6 and other http plugins https://github.com/php-http/HttplugBundle
httplug:
    plugins:
        logger: ~
    clients:
        guzzle6:
            factory: 'httplug.factory.guzzle6'
            plugins: ['httplug.plugin.logger']
            config:
                timeout: 2

# GeoCoder Bundle - https://github.com/geocoder-php/BazingaGeocoderBundle
bazinga_geocoder:
    providers:
        host_ip:
            factory: Bazinga\GeocoderBundle\ProviderFactory\HostIpFactory
            options:
                httplug_client: 'httplug.client'
        free_geo_ip:
            factory: Bazinga\GeocoderBundle\ProviderFactory\FreeGeoIpFactory
            options:
                httplug_client: 'httplug.client'
        chain:
            factory: Bazinga\GeocoderBundle\ProviderFactory\ChainFactory
            options:
                services: ["@bazinga_geocoder.provider.host_ip", "@bazinga_geocoder.provider.free_geo_ip"]

I'm not trying to do anything fancy with this, just switch from using the guzzle6 adapter to using httplug while upgrading from SF 3.1 to 3.4. Thank you.

Thank you. I've tried with your config. You should add an at-sign.

httplug_client: '@httplug.client'

I've added #186

Closed by #186