3

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.

yivi
  • 42,438
  • 18
  • 116
  • 138

1 Answers1

2

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%'
yivi
  • 42,438
  • 18
  • 116
  • 138
NorthernDev
  • 339
  • 1
  • 6
  • Minute after posting the question ended up doing this. Seems acceptable enough. Let’s see if someone else has a more direct approach. – yivi Nov 23 '19 at 22:23