Cover of the book Symfony 5: The Fast Track

Symfony 5: The Fast Track is the best book to learn modern Symfony development, from zero to production. +300 pages showcasing Symfony with Docker, APIs, queues & async tasks, Webpack, SPAs, etc.

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_iterator 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_iterator app.handler, default_priority_method: 'calculateServicePriority']
Help the Symfony project!

As with any Open-Source project, contributing code or documentation is the most common way to help, but we also have a wide range of sponsoring opportunities.

New in Symfony 4.4: Dependency Injection Improvements (Part 2) symfony.com/blog/new-in-symfony-4-4-dependency-injection-improvements-part-2

Tweet this

Comments

Is it really equal in 'Calling to wither methods' example:
The 'returns_clone: true' in the previous syntax and 'withLogger: !returns_clone' in new syntax.
I mean I would expect this 'withLogger: returns_clone'.

Comments are closed.

To ensure that comments stay relevant, they are closed for old posts.