phpsymfonysymfony4symfony-dependency-injection

How can I use a class constant as the tag for bound parameter service?


I have tagged a group of services that implement the same interface like this in my configuration:

// services.yml
services:
  _instanceof:
     App\SomeInterface:
       tags: [ !php/const App\SomeInterface::TAG ]

(Where the value for App\SomeInterface::TAG is very_unique_identifier)

I would like to reuse this value to bind this parameter like this:

// services.yaml
 services:
  _defaults:
    autowire: true      
    autoconfigure: true 
    public: false
    bind:
      $fooStrategies: !tagged !php/const App\SomeInterface::TAG 

But when I do it like this, I get an empty RewindableGenerator.

Instead I can do it like this, using the literal value for the constant:

bind:
      $fooStrategies: !tagged 'very_unique_identifier'

... and it works as expected, and the RewindableGenerator has all the necessary services inside.

How can I use the PHP constant in both places, so I do not have to repeat the value in different places? Obviously, not a huge deal, but I'd like to avoid it if possible.


Solution

  • You should be able to configure a parameter with the value !php/const App\SomeInterface::TAG and then use the parameter name in service.yml.

    parameters.yml
    
    parameters:
        interfaceTag: !php/const App\SomeInterface::TAG
    

    and then

    services.yml
    
    services:
      _defaults:
         autowire: true      
         autoconfigure: true 
         public: false
         bind:
           $fooStrategies: !tagged '%interfaceTag%'