[DependencyInjection] when@dev does not inherit defaults + autoconfigure does not work
Hello
I faced something strange. I wanted to add a data collector on a service. So I made a decorator.
My initial file:
services:
_defaults:
autowire: true
autoconfigure: true
AppBundle\AgentProxy\:
resource: '../../../src/AgentProxy/*'
exclude:
- '../../../src/AgentProxy/**/Exception/*'
- '../../../src/AgentProxy/**/Model/*'
AppBundle\AgentProxy\ClientInterface: '@AppBundle\AgentProxy\Client'
So I blindly added
when@dev:
services:
AppBundle\AgentProxy\ClientCollector:
decorates: AppBundle\AgentProxy\ClientInterface
It does not work for 2 reasons
- Autowire does not work even if it's in the same file. I need to add
_defaults: { autowire: true}in thewhen@devsection - So I also added
autoconfigure: true, but does not work. I need to manually tag the service.
The final configuration that works
services:
_defaults:
autowire: true
autoconfigure: true
AppBundle\AgentProxy\:
resource: '../../../src/AgentProxy/*'
exclude:
- '../../../src/AgentProxy/**/Exception/*'
- '../../../src/AgentProxy/**/Model/*'
AppBundle\AgentProxy\ClientInterface: '@AppBundle\AgentProxy\Client'
when@dev:
services:
_defaults:
autowire: true
autoconfigure: true # useless
AppBundle\AgentProxy\ClientCollector:
decorates: AppBundle\AgentProxy\ClientInterface
tags:
-
name: data_collector
id: 'AppBundle\AgentProxy\ClientCollector'
In the first example, the items of the collection are defined with * instead of -, is it a typo?
@alexislefebvre Oh, this is a bad copy paste (via slack) I guess. I fixed it. Thanks
Autowire does not work even if it's in the same file. I need to add _defaults: { autowire: true} in the when@dev section
There is some rightfulness in the current behavior. when@foo defines another yaml tree and merging them from the pov of DI looks edgy. We could give it a try of course. The alternative is to merge this in yaml, using yaml references.
So I also added autoconfigure: true, but does not work. I need to manually tag the service.
That is strange. Can you create a small reproducing app so that we can inspect this behavior please?
Here you go:
https://github.com/lyrixx/test/tree/symfony-reproducer-46803-dic-tag
Thanks. I found #30417 which explains this behavior for decorators.
This should be the way to go:
services:
_defaults: &defaults
autowire: true
autoconfigure: true
AppBundle\AgentProxy\:
resource: '../../../src/AgentProxy/*'
exclude:
- '../../../src/AgentProxy/**/Exception/*'
- '../../../src/AgentProxy/**/Model/*'
AppBundle\AgentProxy\ClientInterface: '@AppBundle\AgentProxy\Client'
when@dev:
services:
_defaults: *defaults
AppBundle\AgentProxy\ClientCollector:
decorates: AppBundle\AgentProxy\ClientInterface
Alternatively:
services: &services
_defaults:
autowire: true
autoconfigure: true
AppBundle\AgentProxy\:
resource: '../../../src/AgentProxy/*'
exclude:
- '../../../src/AgentProxy/**/Exception/*'
- '../../../src/AgentProxy/**/Model/*'
AppBundle\AgentProxy\ClientInterface: '@AppBundle\AgentProxy\Client'
when@dev:
services:
<<: *services
AppBundle\AgentProxy\ClientCollector:
decorates: AppBundle\AgentProxy\ClientInterface
Closing as I doubt this can go anywhere.
Is this right?
when@dev:
_defaults: *defaults
services:
AppBundle\AgentProxy\ClientCollector:
decorates: AppBundle\AgentProxy\ClientInterface
Shouldn't it be like this instead?
when@dev:
services:
_defaults: *defaults
AppBundle\AgentProxy\ClientCollector:
decorates: AppBundle\AgentProxy\ClientInterface
indeed, updated thanks