codememory / event-dispatcher
di
v1.0
2021-09-05 11:25 UTC
Requires
- php: >=8.0
- codememory/config: 2.0
- codememory/console: ^1.0
- codememory/date-time: ^1.0
- codememory/finder: ^1.1
- codememory/fs: ^1.1
- codememory/global-config: ^1.1
- codememory/profiling: ^2.2
- codememory/support: ^1.2
- phpunit/phpunit: ^9.5
Requires (Dev)
- symfony/var-dumper: ^5.3
This package is auto-updated.
Last update: 2024-09-05 17:53:49 UTC
README
安装
composer require codememory/event-dispatcher
配置概览
# configs/event.yaml event: # Settings events pathWithEvents: App/Events # Path with all events eventNamespace: App\Events # Namespace events eventSuffix: Event # Suffix for filename event # Settings listeners pathWithListeners: App/Listeners # Path with all event listeners listenerNamespace: App\Listeners # Namespace event listeners listenerSuffix: Listener # Suffix for filename listener
如果使用了配置,请执行以下命令
- 如果没有全局配置,则创建它
php vendor/bin/gc-cdm g-config:init
- 合并所有配置
php vendor/bin/gc-cdm g-config:merge --all
使用示例
事件类
<?php namespace App\Events; use Codememory\Components\Event\Interfaces\EventInterface; class OutputTextEvent implements EventInterface { /** * @inheritDoc */ public function getListeners() : array { return []; } }
添加事件和监听器
<?php use Codememory\Components\Event\EventDispatcher; use App\Events\OutputTextEvent; require_once 'vendor/autoload.php'; $eventDispatcher = new EventDispatcher(); $eventDispatcher->addEvent(OutputTextEvent::class) ->addListener(function (OutputTextEvent $event) { echo 123; });
抛出事件
use Codememory\Components\Event\Dispatcher; $dispatcher = new Dispatcher(); $dispatcher->dispatch(OutputTextEvent::class); // 123
Event(EventBuilderInterface) 方法
-
setAlias(): EventBuilderInterface
向事件添加别名- string $alias
-
addListener(): EventBuilderInterface
向事件添加监听器- string|callable $listener - 命名空间或监听器回调
-
setParameters(): EventBuilderInterface
添加将被传递到事件构造函数中的参数- array $parameters
Event(EventDataInterface) 方法
-
getNamespace(): string
返回事件的命名空间 -
getReflector(): ReflectionClass
返回事件的反射器 -
getListeners(): array
返回事件的监听器数组 -
getParameters(): array
返回事件的参数
EventDispatcher 方法
-
addEvent(): EventBuilderInterface
添加新事件 -
addEventListener(): EventDispatcherInterface
为事件添加监听器- string $eventName - 事件命名空间或别名
- string|callable $listener
-
existEvent(): bool
检查事件是否存在- string $eventName - 事件命名空间或别名
-
getEvent(): EventInterface
获取事件- string $eventName - 事件命名空间或别名
-
getEventListeners(): array
返回事件的监听器数组- string $eventName - 事件命名空间或别名