yivi
yivi

Reputation: 47349

Mixing positional and keyed arguments in service definition, using YAML

When configuring a service using XML, we can do the following:

 <service id="foobar" class="App\Foobar" public="false" abstract="true">
       <argument type="service" id="doctrine" />
       <argument>null</argument>
       <argument type="service" id="logger" on-invalid="ignore" />
       <argument key="$bombastic" type="service"
            id="bombastic.service" on-invalid="ignore" />
</service>

The first three arguments are positional (the first three arguments in the constructor), and the last one is keyed to the parameter name. Since the actual service has 5 arguments, the fourth argument is left undefined so it can be defined by a service that extends the foobar service.

Which is very nice.

In YAML the documentation shows how to use keyed arguments like this:

App\Updates\SiteUpdateManager:
        arguments:
            $someService: '@manager'

and positional arguments like this:

App\Updates\SiteUpdateManager:
        arguments:
            - '@manager'

But I'd like to do the same as the above XML configuration, but using YAML (because all the service configuration for this application is already in YAML, and I would not want to add a single XML configuration file just for this service).

How can I combine the two styles with YAML configuration?

Upvotes: 1

Views: 234

Answers (1)

yceruto
yceruto

Reputation: 9575

Try combining indexed arguments with keyed ones, e.g:

App\Updates\SiteUpdateManager:
    arguments:
        0: '@doctrine'
        1: null
        2: '@?logger' 
        $bombastic: '@?bombastic.service'

Upvotes: 1

Related Questions