2017 © Pedro Peláez
 

library eventer

Simple event system.

image

weew/eventer

Simple event system.

  • Saturday, December 3, 2016
  • by weew
  • Repository
  • 1 Watchers
  • 0 Stars
  • 163 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Simple event system

Build Status Code Quality Test Coverage Version Licence, (*1)

Table of contents

Installation

composer install weew/eventer, (*2)

Usage

This event system allows you to easily subscribe to certain events and get notified as soon as one occurs. The most simple way to subscribe to an event is by using a string as the event name., (*3)

Simple subscription

The easiest way to create a subscription is to use callback function., (*4)

$eventer = new Eventer();
$eventer->subscribe('event.name', function(IEvent $event) {
    echo $event->getName();
    // event.name
});
$eventer->dispatch('event.name');

Attaching data to an event

Mostly you want to throw an event with particular data attached. The quickest way to achieve this is to use the generic events., (*5)

$eventer = new Eventer();
$eventer->subscribe('event.name', function(IEvent $event) {
    var_dump($event->getData());
    // ['secret' => 'secret value']
    echo $event->get('secret');
    // secret value
});

$event = new GenericEvent('event.name', ['secret' => 'secret value']);
// or
$event = new GenericEvent('event.name');
$event->set('secret', 'secret value');

$eventer->dispatch($event);

Creating custom events

In more complex applications I would suggest to roll your own events. This makes the code much more easier to understand since you'll never have to guess what the event name might be. It also allows you to extend your events with more complex behaviour., (*6)

class CustomEvent extends Event {
    public function getSecret() {
        return 'secret value';
    }
}

$eventer = new Eventer();
$eventer->subscribe(CustomEvent::class, function(CustomEvent $event) {
    echo $event->getSecret();
    // secret value
});

$eventer->dispatch(new CustomEvent());

Unsubscribing from events

To unsubscribe from an event you can simply pass the subscription object to the unsubscribe method on the event dispatcher., (*7)

$eventer = new Eventer();
$subscription = $eventer->subscribe('event.name', 'abstract.value');
$eventer->unsubscribe($subscription);

Event subscribers

Using callbacks in your events might not always be an optimal solution. Therefore you can create event subscriber classes that get called whenever an event occurs. The class must have the handle(IEvent $event) method, but there is no specific interface that you are forced to implement., (*8)

class CustomEvent extends Event {
    public function getSecret() {
        return 'secret value';
    }
}

class CustomEventSubscriber {
    public function handle(IEvent $event) {
        /** @var CustomEvent $event */
        echo $event->getSecret();
        // secret value
    }
}

$eventer = new Eventer();
$eventer->subscribe(CustomEvent::class, CustomEventSubscriber::class);
$eventer->dispatch(new CustomEvent());

Existing container integrations

There is an integration for the weew/container. See eventer-container-aware., (*9)

The Versions

03/12 2016

dev-master

9999999-dev

Simple event system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

21/07 2016

v2.2.2

2.2.2.0

Simple event system.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Maxim Kott

08/03 2016

v2.2.1

2.2.1.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array ^1.0.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

08/03 2016

v2.2.0

2.2.0.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array ^1.0.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

08/01 2016

v2.1.0

2.1.0.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array ^1.0.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

07/01 2016

v2.0.0

2.0.0.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array ^1.0.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

16/11 2015

v1.0.0

1.0.0.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array ^1.0.0
  • weew/php-helpers-string ^1.0.0

 

The Development Requires

by Maxim Kott

29/10 2015

v0.0.4

0.0.4.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott

10/09 2015

v0.0.3

0.0.3.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott

25/08 2015

v0.0.2

0.0.2.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott

21/08 2015

v0.0.1

0.0.1.0

Simple event system.

  Sources   Download

MIT

The Requires

  • weew/php-helpers-array 0.*
  • weew/php-helpers-string 0.*

 

The Development Requires

by Maxim Kott