1

I am trying to figure out if I can use a similar construct in the YAML syntax for a service definition as in the XML syntax. Specifically the "construct" where, in XML, an argument can be resolved by factory "in-line".

I have the following XML definition:

<service id="my_service_id" class="App\Some\Service">
    <argument type="service">
        <service class="Gaufrette\Filesystem">
            <argument>%some_container_parameter%</argument>
            <factory service="knp_gaufrette.filesystem_map" method="get" />
        </service>
    </argument>
    <argument type="service" id="request_stack"/>
    <argument>%some_other_container_parameter%</argument>
</service>

I can't figure out how to represent this (or if it's even possible) in yaml.

I have tried the following:

my_service_id:
    class: App\Some\Service
    arguments:
        - { class: Gaufrette\Filesystem, factory: ['@knp_gaufrette.filesystem_map','get'], arguments: ['%some_container_parameter%'] }
        - '@request_stack'
        - '%some_other_container_parameter%'

but that gives me the following error message:

Argument 1 passed to App\Some\Service::__construct() must implement interface Gaufrette\FilesystemInterface, array given

yivi
  • 42,438
  • 18
  • 116
  • 138
leflings
  • 646
  • 3
  • 17

1 Answers1

1

You are defining two services within the same definition in the XML version.

While that is possible in XML, I don't really think it's a great idea. You can easily do the same by defining each service on its own:

gaufrette.filesystem:
    factory: ['@knp_gaufrette.filesystem_map','get']
    arguments: ['%some_container_parameter%']

my_service_id:
    class: App\Some\Service
    arguments:
        - '@gaufrette.filesystem'
        - '@request_stack'
        - '%some_other_container_parameter%'
yivi
  • 42,438
  • 18
  • 116
  • 138
  • Yeah, that's what I ended up doing. And I agree that this solution is probably a better idea, especially because it allows for easy reuse of the service. I was just curious as to whether that construct is even possible in yaml. – leflings Mar 25 '22 at 08:26
  • No, "nesting" service definitions is not possible in YAML. I'm moving all my configurations to PHP in any case, so I can avoid all the different YAML nuances. – yivi Mar 25 '22 at 08:31