harp-orm/event-listeners

Simple event listeners with Closures

dev-master 2014-09-09 08:47 UTC

This package is auto-updated.

Last update: 2020-07-21 11:48:24 UTC


README

Build Status Scrutinizer Code Quality Code Coverage Latest Stable Version

Simple event listeners with Closures.

Usage

$listeners = new EventListeners();

$listeners->addBefore('save', function ($target) {
    // ...
});

$listeners->addAfter('delete', function ($target) {
    // ...
});

$listeners->dispatchEvent('save', $target);
$listeners->dispatchEvent('delete', $target);

A very simple manager object that holds all the appropriate events, and allows you to dispatch these events later.

EventListenersTrait

This trait gives you the ability to easily add eventlisteners to another object.

class TestConfig {
    use EventListenersTrait;
}

$config = new TestConfig();

$config
    ->addEventBefore('delete', function () {
        // ...
    })
    ->addEventAfter('validate', function () {

    });

// Return the EventListeners object
$config->getEventListeners();

Here are all the methods added by this trait.

Method Description
getEventListeners() Get the EventListeners object
addEventBefore($name, $closure) Add a "before" listener
addEventAfter($name, $closure) Add a "after" listener

License

Copyright (c) 2014, Clippings Ltd. Developed by Ivan Kerin

Under BSD-3-Clause license, read LICENSE file.