[英]Symfony 4 _instanceof in Bundle's services.yaml
I have a bundle which has interface Optimax\\HealthCheckBundle\\Service\\HealthInterface
I need set tags to all services which implement this interface. 我有一个具有
Optimax\\HealthCheckBundle\\Service\\HealthInterface
接口的捆绑软件,我需要为实现此接口的所有服务设置标签。 I do it with the following directive: 我使用以下指令进行操作:
_instanceof:
Optimax\HealthCheckBundle\Service\HealthInterface:
tags: ['health.service']
It works fine when I put this directive into config/services.yaml
. 当我将此指令放入
config/services.yaml
时,它可以正常工作。 But if I put this code into my bundle's config (which required via composer) vendor/optimax/health-check/src/Resources/config/services.yaml
it doesn't work. 但是,如果我将此代码放入包的配置中(通过作曲家需要),则
vendor/optimax/health-check/src/Resources/config/services.yaml
将无法正常工作。 I don't want copy-paste this directive into services.yaml
every time when I require this bundle to a new project. 当我需要将此捆绑包添加到新项目时,我不想每次都将此指令复制粘贴到
services.yaml
。
How can I move this directive into services.yaml
which is in my Bundle's directory or at least into another file in config/packages
folder of the project? 如何将该指令移至Bundle目录中的
services.yaml
或至少移至项目的config/packages
文件夹中的另一个文件中?
Did you try auto tagging all services with this interface in your Bundle extension like this: 您是否尝试过在Bundle扩展程序中使用此接口自动标记所有服务,如下所示:
$container->registerForAutoconfiguration(CustomInterface::class)
->addTag('app.custom_tag')
;
Taken from the Symfony docs: https://symfony.com/doc/current/service_container/tags.html 取自Symfony文档: https : //symfony.com/doc/current/service_container/tags.html
To expand on the issue for others. 为其他人扩展这个问题。
_instanceof
functions identically to the _defaults
definition. _instanceof
功能与_defaults
定义相同。 In that the _instanceof
definition only applies to the file it is used in. _instanceof
定义仅适用于使用它的文件。
This prevents third-party bundle definitions from affecting your entire application with definitions like: 这样可以防止第三方捆绑软件定义影响您的整个应用程序,例如:
_defaults:
public: true
autowire: false
_instanceof:
Psr\Log\LoggerAwareInterface:
- method: setLogger
arguments:
- '@custom_logger'
tags:
- { name: monologer.log, channel: 'custom_channel' }
Therefor if the service you are attempting to tag with _instanceof
is not declared in the same services.yml
file the tag will not be added. 因此,如果您尝试使用
_instanceof
标记的服务未在同一services.yml
文件中声明,则不会添加标记。
To tag services that implements an Interface in the entire application, you would need to use the method provided by @Jeroen 要标记在整个应用程序中实现接口的服务,您需要使用@Jeroen提供的方法
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.