定制 kovalevsky-projects/event-dispatcher 二次开发

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

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

kovalevsky-projects/event-dispatcher

Composer 安装命令:

composer require kovalevsky-projects/event-dispatcher

包简介

jQuery like event dispatcher for PHP

README 文档

README

Simple PHP library that allow you to dispatch events in your system.

Installation

Install it via Composer (kovalevsky-projects/event-dispatcher on Packagist)

Usage

Here's the simple example of the usage:

$dispatcher = new \KovalevskyProjects\EventDispatcher\EventDispatcher();

$dispatcher->on('hello.world', function() {
  echo 'Hello World!';
});

$dispatcher->trigger('hello.world');

Also you can use event parameters. It can be single parameter or an array of the parameters:

$dispatcher->on('hello.world', function($text) {
  echo $text;
});

$dispatcher->on('foo.bar', function($foo, $bar) {
  echo $foo + $bar;
});

$dispatcher->trigger('hello.world', 'Hello World!');

// You can use simple numeric arrays instead of associative
$dispatcher->trigger('foo.bar', array(
  'foo' => 2,
  'bar' => 2,
));

API

Trigger

trigger($event, $parameters = null) - Dispatches the specified event.

You can specify single parameter or an array of parameters as second argument:

$dispatcher->trigger('foo.bar', 'some parameter');
// or
$dispatcher->trigger('foo.bar', [
  'one' => 'parameter one',
  'two' => 'parameter two',
]);

On

on($event, $action) - Adds the action for the event.

$dispatcher->on('post.update', function($post, $author) {
  notify('The post ' . $post->title . ' updated by the ' . $author);
});

Off

off($event, $action) - Removes the action.

$dispatcher->on('some.action', $callableFunction);
// ...
$dispatcher->off('some.action', $callableFunction);

Please note: If your are using the closures, then you can't remove the action using Off() method.

Bind

bind($events, $action = null) - Attaches the action for the more than one event.

$dispatcher->bind('foo bar baz', function() {
  echo 'You will see this message when foo, bar and baz events will be triggered';
});

// or with array

$dispatcher->bind(['foo', 'bar', 'baz'], function() { ... });

// or you can specify different actions for the each event

$dispatcher->bind([
  'foo' => function() { ... },
  'bar' => function() { ... },
  'baz' => function() { ... },
]);

Unbind

unbind($events = null) - Removes all previously attached actions.

$dispatcher->unbind('foo bar baz');
// or
$dispatcher->unbind(['foo', 'bar', 'baz']);
// or you can remove ALL actions.
$dispatcher->unbind();

Has

has($event, $action = null) - Checks whether the specified event has action.

$dispatcher->has('foo');
// or you can check for the specific action
$dispatcher->has('foo', $someAction);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2014-05-18

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固