2017 © Pedro Peláez
 

library spiffy-event

Spiffy\Event is a light-weight, HHVM compatible, and dependency free event library.

image

spiffy/spiffy-event

Spiffy\Event is a light-weight, HHVM compatible, and dependency free event library.

  • Monday, January 19, 2015
  • by SpiffyJr
  • Repository
  • 0 Watchers
  • 0 Stars
  • 20,756 Installations
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 3 % Grown

The README.md

Spiffy\Event

Build Status Code Coverage Scrutinizer Code Quality, (*1)

Installation

Spiffy\Event can be installed using composer which will setup any autoloading for you., (*2)

composer require spiffy/spiffy-event, (*3)

Additionally, you can download or clone the repository and setup your own autoloading., (*4)

Create an event

use Spiffy\Event\Event;

// Create an event that fires on 'foo'
$event = new Event('foo');

// Creates an event with a target
$event = new Event('foo', 'target');
$event->getTarget(); // 'target'

// Event can have parameters too
$event = new Event('foo', 'target', ['foo' => 'bar']);
$event->getParams()['foo']; // 'bar'

Listening to events

use Spiffy\Event\EventManager;

$em = new EventManager();

// Listen with a priority of 1
$em->on('foo', function() { echo 'a'; }, 1);

// Listen with a higher priority
$em->on('foo', function() { echo 'b'; }, 10);

// Event default with priority 0
$em->on('foo', function() { echo 'c'; });
$em->on('foo', function() { echo 'd'; });

// echos 'bacd'

Firing events

use Spiffy\Event\Event;
use Spiffy\Event\EventManager;

$em = new EventManager();
$em->on('foo', function() { echo 'fired'; });

// Simplest form of fire requiring just the type
$em->fire('foo'); // fired

// You can also specify the target and params when using the type
$em->fire('foo', 'target', ['foo' => 'bar']);

// You can also provide your own event.
// This is identical to the fire above.
$event = new Event('foo', 'target', ['foo' => 'bar']);
$em->fire($event);

Handling responses

use Spiffy\Event\Event;
use Spiffy\Event\EventManager;

$em = new EventManager();

// Respones are returned as a SplQueue (FIFO).
$em->on('foo', function() { return 'a'; });
$em->on('foo', function() { return 'b'; });

// Outputs 'ab'
foreach ($em->fire('foo') as $response) {
    echo $response;
}

Plugins

Sometimes you may want to collect several on() calls in a single class. Spiffy\Event provides a Plugin interface you can implement and pass to the plug() method to prepare several events at a time. The name plugin is used because a collection of events is generally used to plugin additional functionality to an object., (*5)

use Spiffy\Event\Event;
use Spiffy\Event\Plugin;

class MyPlugin implements Plugin
{
    public function plug(Manager $events)
    {
        $events->on('foo', [$this, 'onFoo']);
    }

    public function onFoo(Event $e)
    {
        echo 'do foo';
    }
}

$em = new EventManager();
$em->attach(new MyPlugin());

// output is 'do foo'
$em->fire('foo');

The Versions

19/01 2015

dev-master

9999999-dev

Spiffy\Event is a light-weight, HHVM compatible, and dependency free event library.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

16/07 2014

1.0.1

1.0.1.0

Spiffy\Event is a light-weight, HHVM compatible, and dependency free event library.

  Sources   Download

BSD-3-Clause

The Requires

  • php >=5.4

 

The Development Requires

08/05 2014

1.0

1.0.0.0

Spiffy\Event is a light-weight, HHVM compatible, and dependency free event library.

  Sources   Download

The Requires

  • php >=5.4

 

The Development Requires