Skip to content
This repository was archived by the owner on Jul 15, 2021. It is now read-only.
This repository was archived by the owner on Jul 15, 2021. It is now read-only.

Add documentation for making autowireable Guzzle client with custom class #236

@rvanlaak

Description

@rvanlaak

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

Possible content

The bundle can get used as "a factory" to create an autowireable class. That way the bundle puts the middleware (like logging / error reporting / timing) in place.

<?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%'

When someone wants to autowire that class in their own service definition:

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

Important note:

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

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'

Probably a solution that wouldn't need this additional service alias, is by somewhere changing compilation priorities. Can bundles do their work several steps earlier in the compilation process?

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions