Symfony 如何找到 AsEventListener 类?

问题描述 投票:0回答:1

我在

$container->registerAttributeForAutoconfiguration
调用这里,但据我所知,这不会向容器中添加类。然而文档

定义事件监听器的另一种方法是使用 AsEventListener PHP 属性。这允许在其类内配置侦听器,而无需在外部文件中添加任何配置。

我想也许它对 ContainerConfigurator 有作用?

grep -h -i eventlistener $(ag -l configurator)|grep -v '^use'

 我没有看到任何相关内容。

那么我在哪里可以找到将这些类添加到容器中的代码?

symfony
1个回答
0
投票
这些类是通过常规“服务”配置“找到”的。

例如,在默认的 Symfony 安装上:

# config/services.yaml services: # default configuration for services in *this* file _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. # makes classes in src/ available to be used as services # this creates a service per class whose id is the fully-qualified class name App\: resource: '../src/' exclude: - '../src/DependencyInjection/' - '../src/Entity/' - '../src/Kernel.php'
这告诉 Symfony 查找 

src

 下的所有内容,除了列出的三个排除项。

你在

FrameworkExtension

上找到的代码,它告诉Symfony每次找到带有
#[AsEventListener]
属性标记的类时要做什么:被标记为
kernel.event_listener
(除非该标签被用在方法上,在这种情况下它抛出异常)。

稍后,在

RegisterListenersPass

 中,之前标记为 kernel.event_listener
 的服务(通过此属性或任何其他方式)将注册为事件侦听器。

© www.soinside.com 2019 - 2024. All rights reserved.