-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
Problem
The documentation regarding the autoconfigure option states the following regarding _defaults
:
Above, the services.yaml file has autoconfigure: true in the _defaults section so that it applies to all services defined in that file. With this setting, the container will automatically apply certain configuration to your services, based on your service's class. This is mostly used to auto-tag your services.
(this is also denoted in symfony/symfony#28326 (comment) that the defaults are file-based)
With the introduction of using when@<env>
tags that can be used within the same file, like services.yaml
file, this statement has become somewhat ambiguous, as _defaults
does not (literally) apply to all services defined in the file. When setting autowire
, it does only apply to the current services
, not to the ones defined under the services if when
is used in the same file.
Working example:
services.yaml
services:
# default configuration for services in *this* file; (this is the default comment from framework bundle recipe)
_defaults:
autowire: true
autoconfigure: true
App\My\Service: # has arbitrary amount of arguments
when@prod:
services:
_defaults:
autowire: true
autoconfigure: true
App\My\Service: # autowired arguments passed to service; works
Non-working example:
services.yaml
services:
# default configuration for services in *this* file ; (this is the default comment from framework bundle recipe)
_defaults:
autowire: true
autoconfigure: true
App\My\Service: # has arbitrary amount of arguments
when@prod:
services:
App\My\Service: # no explicit arguments given thus failing with "ArgumentCountError" as not autowired
The wording ...that it applies to all services defined in that file
or # default configuration for services in *this* file
(in the services.yaml
) makes it seems as if the "Non-working example" should have worked as everything is defined in the same file (but it doesn't).
Suggestion
Add a note/warning to the autoconfigure option section that the _defaults
need to be explicitely redefined when using when
in the same services.yaml
.