Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

symfony2 factory-service in yaml

Tags:

symfony

sylius

Looking at the code of the Sylius Bundle for Symfony I noticed the Resource Bundle has an interesting way of defining resource controllers as services. Here is the cart item controller service configuration in XML

<service id="sylius.controller.cart_item" class="%sylius.controller.cart_item.class%">
        <argument type="service">
            <service factory-service="sylius.controller.configuration_factory" factory-method="createConfiguration" class="Sylius\Bundle\ResourceBundle\Controller\Configuration">
                <argument>sylius</argument>
                <argument>cart_item</argument>
                <argument>SyliusCartBundle:CartItem</argument>
            </service>
        </argument>
        <call method="setContainer">
            <argument type="service" id="service_container" />
        </call>
    </service>

If I understand it correctly this code instantiates the controller class and passes as the constructor argument the result of a call to the factory-method "createConfiguration" in the factory-service class. Arguments are specified, so everything is fine.

My question is twofold: 1) Where is this documented? I could not find one example of this kind of arguments-as-a factory-callable in the docs. 2) What would be the YAML version of this?

Thanks...

like image 729
smarques Avatar asked Feb 13 '23 07:02

smarques


1 Answers

Here is the way:

<service id="sylius.controller.cart_item" class="%sylius.controller.cart_item.class%">
    <argument type="service">
        <service factory-service="sylius.controller.configuration_factory" factory-method="createConfiguration" class="Sylius\Bundle\ResourceBundle\Controller\Configuration">
            <argument>sylius</argument>
            <argument>cart_item</argument>
            <argument>SyliusCartBundle:CartItem</argument>
        </service>
    </argument>
    <call method="setContainer">
        <argument type="service" id="service_container" />
    </call>
</service>

Can be written as the following in yml

sylius.controller.cart_item:
    class: %sylius.controller.cart_item.class%
    arguments:
        - "@=service('sylius.controller.configuration_factory').createConfiguration('sylius', 'cart_item', 'SyliusCartBundle:CartItem')"
    calls:
        - [setContainer, ["@service_container"]]
like image 162
Alexei Tenitski Avatar answered Feb 24 '23 11:02

Alexei Tenitski