Symfony News

New in Symfony 4.4: Dependency Injection Improvements (Part 2)

Improved YAML Syntax for Method Calls

Contributed by
Nicolas Grekas
in #33779.

Service method calls and setter injection are different ways to call some methods on your classes when building the services for them. In Symfony 4.4 we've improved the YAML syntax to make it more concise and easier to read/write:

1
2
3
4
5
6
7
8
9
# config/services.yaml
services:
    App\Service\MessageGenerator:
        # ...
        calls:
-            - method: setLogger
-              arguments:
-                  - '@logger'
+            - setLogger: ['@logger']

Calling to wither methods has been simplified too:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# config/services.yaml
services:
    App\Service\MessageGenerator:
        # ...
        calls:
-            - method: withLogger
-              arguments:
-                  - '@logger'
-              returns_clone: true
+            - withLogger: !returns_clone ['@logger']

Better Control of Service Decoration

Contributed by
Mathias Arlaud
in #33854.

Service decoration is one of the most popular and powerful features of the Symfony Dependency Injection. In Symfony 4.4, we improved it so you can control what happens when the decorated service does not exist:

1
2
3
4
5
# config/services.yaml
services:
    App\NewMailer:
        decorates: App\Mailer
        decoration_on_invalid: ignore

If the App\Mailer service does not exist, this configuration tells Symfony to ignore the error and remove the App\NewMailer service. The other possible values are exception and null.

The exception value tells Symfony to throw a ServiceNotFoundException and it's the default value because it matches the current behavior. The null value makes the decorated service equal to null, so the decorating service must be prepared to handle with null values.

Priorities for Tagged Services

Contributed by
Grégoire Pineau
in #33628.

When injecting tagged services into other services, you can apply some priority to those services as follows:

1
2
3
4
5
6
7
8
9
# config/services.yaml
services:
    _instanceof:
        App\Handler:
            tags:
                - { name: 'app.handler', priority: 20 }

    App\HandlerCollection:
        arguments: [!tagged app.handler]

However, the priority (20 in this example) is the same for all services. In Symfony 4.4 we've improved this feature to allow defining the priority per service. To do so, define a public static function getDefaultPriority(): int method in your service classes and return their priority. You can configure this method name with the default_priority_method attribute:

1
2
3
4
5
# config/services.yaml
services:
    # ...
    App\HandlerCollection:
        arguments: [!tagged app.handler, default_priority_method: 'calculateServicePriority']

Be trained by Symfony experts - 2019-12-2 Berlin - 2019-12-2 Berlin - 2019-12-2 Clichy


About us

What a Symfony developer should know about the framework: News, Jobs, Tweets, Events, Videos,...

Resources

Find us on Twitter

Find us on Facebook