2017 © Pedro Peláez
 

library simple-events

Simple event dispatching library for PHP

image

yarcode/simple-events

Simple event dispatching library for PHP

  • Friday, August 18, 2017
  • by lagman
  • Repository
  • 1 Watchers
  • 0 Stars
  • 96 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

Simple Events

Simple event dispatching library for PHP, (*1)

SensioLabsInsight, (*2)

Scrutinizer Code Quality Code Coverage Build Status GitHub license, (*3)

Installation

Composer

The preferred way to install this extension is through Composer., (*4)

Either run, (*5)

composer require yarcode/simple-events

or add, (*6)

"yarcode/simple-events": "*"

to the require section of your composer.json, (*7)

Usage

Via EventEmitterTrait

Attach \YarCode\Events\EventEmitterTrait to your class to make it EventEmitter, (*8)

class MyClass {
    use \YarCode\Events\EventEmitterTrait;
    ...
}
$emitter = new MyClass();

Standalone EventEmitter

Or create an instance of \YarCode\Events\EventEmitter., (*9)

$emitter = new \YarCode\Events\EventEmitter();

Adding listeners

You can add callable listener for any string event name., (*10)

$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
    echo "{$event->name} was emitted";
});
$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
    echo "{$event->name} was emitted one more time";
});

Emitting events

You can emit named event with default event object., (*11)

$emitter->emit('TestEvent');
// TestEvent was emitted
// TestEvent was emitted one more time

Or you can pass \YarCode\Events\Event object to the listeners., (*12)

$event = new \YarCode\Events\Event();
$event->payload['key'] = 'value';
$emitter->emit('TestEvent', $event);
// TestEvent was emitted
// TestEvent was emitted one more time

You could pass any data as event payload. It would be passed as a first parameter to a listener callable., (*13)

$data = ['foo', 'bar'];
$emitter->emit('MixedDataEvent', $data);

Removing listeners

You can remove one concrete listener., (*14)

$callback = function (\YarCode\Events\Event $event) {
    echo "{$event->name} was emitted third time";
});
$emitter->addListener('TestEvent', $callback);
$emitter->removeListener('TestEvent', $callback);

Or remove all the listeners for the event., (*15)

$emitter->removeAllListeners('TestEvent');

Or remove all the listeners for all events., (*16)

$emitter->removeAllListeners();

Breaking the execution

Set the $event->handled property to true to stop the further listeners execution., (*17)

$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
    $event->handled = true;
});
$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
    echo "This callback for {$event->name} would never run";
});
$emitter->emit('TestEvent');

Accessing the emitter from the listener

Emitter object is being passed as a second parameter to a listener., (*18)

$emitter->addListener('TestEmitterAccessEvent', function ($data, $emitter) {
    echo "Hello"; 
    $emitter->emit('TestEmitterAccessEvent2', $data);
});
$emitter->addListener('TestEmitterAccessEvent2', function ($data) {
    echo " World";
});
$emitter->emit('TestEmitterAccessEvent');
// Hello World

License

MIT, (*19)

The Versions

18/08 2017

dev-master

9999999-dev

Simple event dispatching library for PHP

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

dispatcher bus emitter event event-bus event-dispatcher event-emitter

14/08 2017

1.0

1.0.0.0

Simple event dispatching library for PHP

  Sources   Download

MIT

The Requires

  • php ^7

 

The Development Requires

dispatcher bus emitter event

07/10 2016

0.1

0.1.0.0

Simple event dispatching library for PHP

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

dispatcher bus emitter event