csarrazi / CsaGuzzleBundle

A bundle integrating Guzzle >=4.0 in Symfony
250 stars 76 forks source link

Add documentation for symfony service injection in the guzzle client #52

Open csarrazi opened 9 years ago

csarrazi commented 9 years ago

With release 1.3.2, symfony services can now be injected in a client's configuration.

This should be documented in the 1.3 branch.

rvanlaak commented 6 years ago

Another use case to document; changing the client's class, and how to autowire that to another own service definition.

We use the bundle as "a factory" for our own autowireable class:

<?php

namespace Acme\Infra\Shopping;

class Client extends \GuzzleHttp\Client
{
}

csa_guzzle:
    clients:
        shopping:
            class: 'Acme\Infra\Shopping\Client'
            config:
                base_uri: '%shopping.base_uri%'

... and we want to autowire that class in our own service definition:

services:
    Acme\Application\Shopping\Cart:
        arguments: ['@Acme\Infra\Shopping\Client']   

For controller action argument autowiring this works perfectly, but for resolving service arguments on container compilation this unfortunately gives a "service not found" exception because the bundle registers the client to the container after it resolves the rest of the services.

Probably the solution is somewhere in compilation priorities. It would be great to have documentation on this, as the above allows everyone to make use of all the great middleware the bundle can register with the clients.

A way the container can resolve / autowire is by aliasing the service this bundle creates:

services:
    Acme\Infra\Shopping\Client:
        alias: 'csa_guzzle.client.shopping'
csarrazi commented 6 years ago

Good point, however that would be in another issue. Do you mind creating a new issue from your comment? That would be great! :)

rvanlaak commented 6 years ago

Of course 👍 see #236