activecollab/eventsdispatcher 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

activecollab/eventsdispatcher

最新稳定版本:1.0.0

Composer 安装命令:

composer require activecollab/eventsdispatcher

包简介

Dispatch events, and provide event handlers

README 文档

README

Build Status

This package offers simple events dispatcher, with listeners. Key concepts:

  1. Events are not arbitrary strings, but instances that encapsulate all relevant event data,
  2. When you specify a listener, you specify an event class (or interface) that you want to listen to,
  3. You can listen to entire classes of events, by specifying general enough event class (or interface),
  4. Listeners are callables, and event is always passed to it as the first (and only) argument.

General listener example:

<?php

namespace MyApp;

use ActiveCollab\EventsDispatcher\EventsDispatcher;
use ActiveCollab\EventsDispatcher\Test\Fixtures\LicenseRenewedEvent\LicenseRenewedEventInterface;

$dispatcher = new EventsDispatcher();
$dispatcher->listen(LicenseRenewedEventInterface::class, function (LicenseRenewedEventInterface $event) {
    print "License {$event->getLicenseKey()} has been renewed\n";
});

To specify a global listener, that handles all events, just go highly general with the specification:

<?php

namespace MyApp;

use ActiveCollab\EventsDispatcher\EventsDispatcher;
use ActiveCollab\EventsDispatcher\Events\EventInterface;

$dispatcher = new EventsDispatcher();
$dispatcher->listen(EventInterface::class, function (EventInterface $event) {
    print "Event " . get_class($event) . " handled\n";
});

Similar approach can be used to handle a class of events. Instead of using the base EventInterface, register listener to a class, or interface that all events of a targeted type extend, or implement.

To trigger an event, call trigger() method with event as the first (and only) argument:

<?php

namespace MyApp;

use ActiveCollab\EventsDispatcher\EventsDispatcher;
use ActiveCollab\EventsDispatcher\Test\Fixtures\LicenseRenewedEvent\LicenseRenewedEvent;

$dispatcher = new EventsDispatcher();
$dispatcher->trigger(new LicenseRenewedEvent(
    '123',
    '2016-12-31',
    '2017-12-31',
    699.0
));

统计信息

  • 总下载量: 54.94k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-04-04