macs / rad-domain-event
提供 RAD Domain Event 组件
v3.0.0
2020-03-18 01:47 UTC
Requires
- php: *
- doctrine/orm: @stable
- symfony/config: @stable
- symfony/dependency-injection: @stable
- symfony/http-kernel: @stable
Requires (Dev)
- phpspec/phpspec: @stable
This package is not auto-updated.
Last update: 2024-09-12 21:54:15 UTC
README
Doctrine2 的轻量级域事件模式实现。
官方维护者
安装
使用 composer
$ composer require knplabs/rad-domain-event
如果您使用的是 Symfony,您可以通过更新 app/AppKernel.php
文件来更新
public function registerBundles() { $bundles = array( // bundles here ... new Knp\Rad\DomainEvent\Bundle\DomainEventBundle(); ); }
使用方法
设置您的实体
首先,确保您的实体实现了 Provider 接口,并使用 ProviderTrait。
use Knp\Rad\DomainEvent; class MyEntity implements DomainEvent\Provider { use DomainEvent\ProviderTrait; }
触发事件
通过实体的 raise
方法触发任何事件。在实体被提交后,它将被转换为 Knp\Rad\DomainEvent\Event 对象并分发。
use Knp\Rad\DomainEvent; class MyEntity implements DomainEvent\Provider { // ... public function myFunction($arg) { // your function behavior $this->raise('myEventName', ['anyKey' => $anyValue]); } }
监听此事件
use Knp\Rad\DomainEvent\Event; class MyListener { public function onMyEventName(Event $event) { // your function behavior } }
然后,当然,注册您的监听器。
app.event_listener.my_event_listener: class: App\EventListener\MyEventListener tags: - { name: kernel.event_listener, event: myEventName, method: 'onMyEventName' }