Symfony YAML format conversion: “calls” with strin

2019-07-25 08:43发布

问题:

Service definition example I want to convert:

MyService:
    class: MyClass
    calls:
        - [add, ["%param1%", "%param2%"]]
        - [add, ["%param3%", "%param4%"]]

And this compile to:

$instance->add('param1', 'param2');
$instance->add('param3', 'param4');

According to my question #38822898 I try next:

MyService:
    class: MyClass
    calls:
        -
            - add
            -
                - "%param1%"
                - "%param2%"

            - add
            -
                - "%param3%"
                - "%param4%"

And this compile to:

$instance->add('param1', 'param2');

So, where is the problem in second YAML example?

UPD#1

According to my previous question, the next YAML example also do not compile to two calls of "add" method, only one:

MyAnotherService:
    class: MyAnotherClass
    factory:
        - MyFactoryClass
        - create
    calls:
        -
            - add
            -
                - >-
                    @=service('AnotherService1').create(
                        service('AnotherService2'),
                        service('AnotherService3')
                    )

            - add
            -
                - >-
                    @=service('AnotherService1').create(
                        service('AnotherService3'),
                        service('AnotherService4')
                    )

Compiles to:

$instance->add($this->get("AnotherService1")->create($this->get("AnotherService2"), $this->get("AnotherService3")));

回答1:

Your expansion is missing a -, it should be:

MyService:
    class: MyClass
    calls:
        -
            - add
            -
                - "%param1%"
                - "%param2%"
        -            # < this one is missing
            - add
            -
                - "%param3%"
                - "%param4%"

In your "rewrite" the two add scalars are part of the same sequence, but in your original they are the first elements of different elements of the parent sequence.

The same problem occurs with the second example.