mleko / event-bundle
事件库包
v0.6.2
2021-02-02 21:04 UTC
Requires
- php: ^5.6|^7.0
- narrator/narrator: ^0.4
- symfony/config: ^2.3|^3.0|^4.0
- symfony/dependency-injection: ^2.3|^3.0|^4.0
- symfony/http-kernel: ^2.3|^3.0|^4.0
Requires (Dev)
- mikey179/vfsstream: ^1.6
- phpunit/phpunit: ^5.6
- symfony/expression-language: ^3.1
README
安装
使用Composer添加项目依赖
$ composer require narrator/narrator-bundle
然后通过将其添加到app/AppKernel.php
文件中来注册包
<?php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Mleko\Narrator\Bundle\NarratorBundle(),
);
// ...
}
// ...
}
配置监听器
NarratorBundle从依赖容器中加载监听器。要使用服务作为监听器,您只需要添加标记服务为监听器的标签。
如果您有服务
<service class="Foo\BarBundle\UserInvitationSender">
// .. arguments, configuration
</service>
class UserInvitationSender {
...
public function handle(\Foo\BarBundle\UserRegistered $event){
...
}
}
要将其用作监听器,请添加标签narrator.listener
。
<service class="Foo\BarBundle\UserInvitationSender">
// .. arguments, configuration
<tag name="narrator.listener"/>
</service>
监听器不需要在handle方法中定义参数类型。
class UserInvitationSender {
...
public function handle($event){
...
}
}
事件类型可以作为标签属性event
传递,该属性应为事件的全限定名称。
<service class="Foo\BarBundle\UserInvitationSender">
// .. arguments, configuration
<tag name="narrator.listener" event="Foo\BarBundle\UserRegistered"/>
</service>
默认情况下,事件传递给已注册监听器的handle
方法。方法名称可以使用method
参数更改。
<service class="Foo\BarBundle\UserInvitationSender">
// .. arguments, configuration
<tag name="narrator.listener" event="Foo\BarBundle\UserRegistered" method="handleRegistration"/>
</service>
使用method
参数,可以使用单个服务处理不同的事件。
<service class="Foo\BarBundle\NotificationSender">
// .. arguments, configuration
<tag name="narrator.listener" event="Foo\BarBundle\UserRegistered" method="handleRegistration"/>
<tag name="narrator.listener" event="Foo\BarBundle\UserLoggedIn" method="handleLogin"/>
</service>
配置事件总线
此包预配置了一个名为"默认"的事件总线,别名为narrator.event_bus
。您可能想要使用更多的事件总线或重新配置"默认"事件总线。您可以通过配置来实现这一点
narrator:
event_bus:
default:
resolver:
type: instanceof
public: true
named: ~
此配置定义了两个事件总线:"默认"和"命名"。这些总线将注册为narrator.event_bus.default
和narrator.event_bus.named
。`narrator.event_bus.default`将使用`InstanceOf`解析器,因此它将支持事件继承;`narrator.event_bus.named`将使用基于严格事件名称比较的默认配置。默认情况下,所有事件总线都注册为私有服务,您可以在每个事件总线的基础上使用`public`参数更改这一点。