kovalevsky-projects/event-dispatcher

jQuery like event dispatcher for PHP

v1.0 2014-05-18 10:26 UTC

This package is not auto-updated.

Last update: 2024-04-13 13:25:36 UTC


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);