symfony icon indicating copy to clipboard operation
symfony copied to clipboard

[DependencyInjection] when@dev does not inherit defaults + autoconfigure does not work

Open lyrixx opened this issue 3 years ago • 5 comments

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

  1. Autowire does not work even if it's in the same file. I need to add _defaults: { autowire: true} in the when@dev section
  2. 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'

lyrixx avatar Jun 29 '22 13:06 lyrixx

In the first example, the items of the collection are defined with * instead of -, is it a typo?

alexislefebvre avatar Jul 12 '22 18:07 alexislefebvre

@alexislefebvre Oh, this is a bad copy paste (via slack) I guess. I fixed it. Thanks

lyrixx avatar Jul 13 '22 08:07 lyrixx

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?

nicolas-grekas avatar Jul 27 '22 15:07 nicolas-grekas

Here you go:

https://github.com/lyrixx/test/tree/symfony-reproducer-46803-dic-tag

lyrixx avatar Aug 01 '22 09:08 lyrixx

Thanks. I found #30417 which explains this behavior for decorators.

nicolas-grekas avatar Aug 01 '22 10:08 nicolas-grekas

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.

nicolas-grekas avatar Jan 10 '23 12:01 nicolas-grekas

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

jdreesen avatar Jan 10 '23 12:01 jdreesen

indeed, updated thanks

nicolas-grekas avatar Jan 10 '23 12:01 nicolas-grekas