如何使用 class 常量作为绑定参数服务的标签?

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

我在我的配置中标记了一组实现相同接口的服务:

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

App\SomeInterface::TAG 的值为 very_unique_identifier

我想重用这个值来像这样绑定这个参数:

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

但是当我这样做时,我得到一个空的 RewindableGenerator

相反,我可以这样做,使用常量的字面值:

bind:
      $fooStrategies: !tagged 'very_unique_identifier'

...它按预期工作,RewindableGenerator 里面有所有必要的服务。

如何在两个地方都使用 PHP 常量,这样我就不必在不同的地方重复该值?显然,这没什么大不了的,但我想尽可能避免它。

您应该可以使用以下值配置参数 !php/const App\SomeInterface::TAG然后使用service.yml中的参数名。

parameters.yml

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

然后

services.yml

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