2017 © Pedro Peláez
 

library event-dispatcher

jQuery like event dispatcher for PHP

image

kovalevsky-projects/event-dispatcher

jQuery like event dispatcher for PHP

  • Tuesday, May 20, 2014
  • by kovalevskyartur
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

EventDispatcher

Simple PHP library that allow you to dispatch events in your system., (*1)

Installation

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

Usage

Here's the simple example of the usage:, (*3)

$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:, (*4)

$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., (*5)

You can specify single parameter or an array of parameters as second argument:, (*6)

$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., (*7)

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

Off

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

$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., (*9)

Bind

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

$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., (*11)

$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., (*12)

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

The Versions

20/05 2014

dev-master

9999999-dev

jQuery like event dispatcher for PHP

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Artur Kovalevsky

18/05 2014

v1.0

1.0.0.0

World's simplest event dispatcher

  Sources   Download

MIT

The Requires

  • php >=5.3.3

 

by Artur Kovalevsky