定制 sugiphp/events 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

sugiphp/events

Composer 安装命令:

composer require sugiphp/events

包简介

Observer design pattern-like events system

README 文档

README

Build Status

Observer design pattern-like events system.

Installation

composer require sugiphp/events ~1.0

Usage

Event

Event is a simple object identified by it's unique name. When an event is fired the Event Dispatcher notifies registered listeners for that particular event name.

Listener

Any function or method that takes no more than one argument can act as a listener. When an event is fired the dispatcher calls all registered listeners (functions) one by one.

Dispatcher

Dispatcher have most significant role in the events systems. All events are fired via the dispatcher. The dispatcher checks for any listeners that are registered with that event and notifies them.

// create a dispatcher
$dispatcher = new Dispatcher();
// register one or more listeners for one or more events
$dispatcher->addListener("user.login", function ($event) {
    // this function will be executed when an event with name "user.login" is fired
});

// fires an event
$dispatcher->dispatch(new Event("user.login"));

Passing data

All listeners should have only one parameter - the event. If we need to pass additional info to those functions we can transport the date with the Event.

$dispatcher->addListener("user.login", function ($event) {
    // get one property
    echo $event->getParam("id"); // 1
    // get a property as Array
    echo $event["username"]; // "demo"
    // fetch all data
    $event->getParams(); // array("id" => 1, "username" => "demo")
});
$event = new Event("user.login", array("id" => 1, "username" => "demo"));
$dispatcher->dispatch($event);

You might need to exchange data between one listener and another. You can do that by adding and altering the data in the event with setParam() method.

$dispatcher->addListener("user.login", function ($event) {
    if ("mike" == $event["username"]) {
        // array access
        $event["is_admin"] = true;
    } else {
        // using setParam() method
        $event->setParam("is_admin", false);
    }
});

$dispatcher->addListener("user.login", function ($event) {
    if ($event["is_admin"]) {
        echo "Hello Admin";
    }
});

$event = new Event("user.login", array("username" => "mike"));
$dispatcher->dispatch($event);

统计信息

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

GitHub 信息

  • Stars: 3
  • Watchers: 2
  • Forks: 0
  • 开发语言: PHP

其他信息

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

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固