bee4/events

此包已被废弃,不再维护。未建议替代包。

基本事件调度器定义,实现基本中介者模式

v1.1.0 2016-06-06 09:55 UTC

This package is auto-updated.

Last update: 2023-02-22 15:51:44 UTC


README

Build Status Scrutinizer Code Quality Code Coverage SensiolabInsight

License

此代码的主要目标是允许使用不同流行实现的事件调度器模式

此库不打算提供每个适配器的所有可能性,而是一组标准的接口,允许不依赖于任何供应商。这样,您可以使用您首选的事件系统与 bee4/events 库的用户之一。

安装

Latest Stable Version Total Downloads

可以使用 Composer 安装此项目。将以下内容添加到您的 composer.json 中

{
    "require": {
        "bee4/events": "~1.1"
    }
}

或者运行此命令

composer require bee4/events:~1.1

事件系统

DispatcherInterface

定义一个对象如何触发一个事件。它包含 4 个方法

  • dispatch 触发一个 EventInterface 实例
  • on 添加一个监听器
  • once 添加一个监听器,执行后将移除
  • remove 移除指定的监听器
  • get 获取附加到一个事件名称的所有监听器

###DispatcherAwareInterface 定义一个对象如何依赖调度器来处理事件。它包含 4 个方法

  • setDispatcher 初始化当前的 DispatcherInterface
  • getDispatcher 获取当前的 DispatcherInterface
  • hasDispatcher 检查是否存在当前的 DispatcherInterface
  • dispatch 如果存在,则在关联的 DispatcherInterface 上分发一个 EventInterface

EventInterface

定义一个可触发的事件对象。此类对象没有默认行为,因为事件可能非常具体。

适配器

我希望您不想创建自己的调度器,因为这里有一些很酷的东西。位于 Bee4\Events\Adapters 命名空间中的适配器类。

<?php
$vendor = new \Symfony\Component\EventDispatcher\EventDispatcher;
$adapter = new \Bee4\Events\Adapters\SymfonyEventDispatcherAdapter($vendor);

$adapter->on('name', function(EventInterface $event) {
	echo "I have been triggered: ".PHP_EOL.print_r($event, true);
});

//EventImplementation must be defined in your project to suit your needs
//If must implements the `EventInterface` contract
$adapter->dispatch('name', new EventImplementation);