Improved YAML Syntax for Method Calls
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
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
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']
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'.