PHP Classes

ITE Event Manager: Manage events implementing PSR-14 interface

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 71%Total: 138 All time: 9,240 This week: 39Up
Version License PHP version Categories
ite-event-manager 1.2BSD License5.6PHP 5, Language, Design Patterns, Traits, P...
Description 

Author

This package can manage events implementing PSR-14 interface.

It implements the event manager interface so it can work with the publish and subscribe pattern for registering listeners and dispatching events.

It provides a trait for attaching, triggering and removing event listeners in any class so it works as an event manager.

Innovation Award
PHP Programming Innovation award nominee
November 2016
Number 4


Prize: One downloadable e-book of choice by O'Reilly
PHP Standards Recommendation number 14 (PSR-14) defines a specification for interfaces of classes that manage events using the publish and subscribe pattern.

This package provides an implementation of the PSR-14 specification.

It provides a trait that can be included in any class, so it can either register to listen to certain kind of events, or trigger the processing of any events and call registered function callbacks.

Manuel Lemos
Picture of Kiril Savchev
  Performance   Level  
Name: Kiril Savchev <contact>
Classes: 6 packages by
Country: Bulgaria Bulgaria
Age: 38
All time rank: 266518 in Bulgaria Bulgaria
Week rank: 227 Up2 in Bulgaria Bulgaria Up
Innovation award
Innovation award
Nominee: 5x

Example

<?php

use Ite\EventManager\Event;
use
Ite\EventManager\EventManager;

chdir(realpath(__DIR__ . '/..'));

require_once
'./vendor/autoload.php';

// load psr interfaces if they not exists
// (not part of this package, but included for convenience):
if (!interface_exists('Psr\\EventManager\\EventInterface')) {
    require_once
'./psr/event-manager/EventInterface.php';
}
if (!
interface_exists('Psr\\EventManager\\EventManagerInterface')) {
    require_once
'./psr/event-manager/EventManagerInterface.php';
}

# Create some callbacks:
$callback1 = function(Event $e) {
    echo
$e->getName() . PHP_EOL;
};

$callback2 = function (Event $e) {
   
var_dump($e->getParams());
   
// return result to the trigger
   
return 1;
};
$callback3 = function (Event $e, $result) {
   
// get the result, returned by the events and dump it:
   
var_dump($result);
   
// This callback will stop the event triggering
   
$e->stopPropagation(1);
};

$callback4 = function (Event $e) {
   
var_dump($e);
};

// initiate the event manager
$eventManager = new EventManager();
// attach the callbacks:
$eventManager->attach('TestEvent', $callback1);
$eventManager->attach('TestEvent', $callback2);
$eventManager->attach('TestEvent', $callback3);
$eventManager->attach('TestEvent', $callback4);
// trigger the TestEvent. After $callback3 the triggering will be stopped
// and $callback4 won't be called:
$eventManager->trigger('TestEvent', null, ['a' => 1, 'b' => 2]);

// remove $callback3
$eventManager->detach('TestEvent', $callback3);
// trigger the TestEvent again. This time $callback4 will be called:
$eventManager->trigger('TestEvent', null, ['a' => 1, 'b' => 2]);
// attach to second event:
$eventManager->attach("Event2", $callback1);
$eventManager->attach("Event2", $callback4);
// trigger the second event:
$eventManager->trigger("Event2");


Details

If-Then-Else Event Manager package

Description

Set of classes the can be used for managing events (publish/subscribe pattern). It also provides a trait for attaching, triggering and removing event listeners within a custom class more in javascript style, turning this class into event manager itself.

NOTE: The psr interfaces are not part of this package, but are included for tests purposes. If you want to use them anyway, you must add their namespace and path to composer.json's autoload section.

This package implements PSR-14 (DRAFT) standards. See https://github.com/php-fig/fig-standards/blob/master/proposed/event-manager.md for more information.

Usage

You can see the examples in the 'tests' folder

Event manager example:

<?php

use Ite\EventManager\Event;
use Ite\EventManager\EventManager;

chdir(realpath(__DIR__.'/..'));

require_once './vendor/autoload.php';

// load psr interfaces if they not exists
// (not part of this package, but included for convenience):
if (!interface_exists('Psr\\EventManager\\EventInterface')) {
        require_once './psr/event-manager/EventInterface.php';
}
if (!interface_exists('Psr\\EventManager\\EventManagerInterface')) {
        require_once './psr/event-manager/EventManagerInterface.php';
}

# Create some callbacks:
$callback1 = function(Event $e) {
        echo $e->getName().PHP_EOL;
};

$callback2 = function (Event $e) {
        var_dump($e->getParams());
        // return result to the trigger
        return 1;
};
$callback3 = function (Event $e, $result) {
        // get the result, returned by the events and dump it:
        var_dump($result);
        // This callback will stop the event triggering
        $e->stopPropagation(1);
};

$callback4 = function (Event $e) {
        var_dump($e);
};

// initiate the event manager
$eventManager = new EventManager();
// attach the callbacks:
$eventManager->attach('TestEvent', $callback1);
$eventManager->attach('TestEvent', $callback2);
$eventManager->attach('TestEvent', $callback3);
$eventManager->attach('TestEvent', $callback4);
// trigger the TestEvent. After $callback3 the triggering will be stopped
// and $callback4 won't be called:
$eventManager->trigger('TestEvent', null, ['a' => 1, 'b' => 2]);

// remove $callback3
$eventManager->detach('TestEvent', $callback3);
// trigger the TestEvent again. This time $callback4 will be called:
$eventManager->trigger('TestEvent', null, ['a' => 1, 'b' => 2]);
// attach to second event:
$eventManager->attach("Event2", $callback1);
$eventManager->attach("Event2", $callback4);
// trigger the second event:
$eventManager->trigger("Event2");

Event listener manager example:

<?php

use Ite\EventManager\EventListenerManagerInterface;
use Ite\EventManager\EventListenerManagerTrait;
use Ite\EventManager\EventManager;
use Psr\EventManager\EventInterface;

chdir(realpath(__DIR__.'/..'));

require_once './vendor/autoload.php';

// load psr interfaces if they not exists
// (not part of this package, but included for convenience):
if (!interface_exists('Psr\\EventManager\\EventInterface')) {
        require_once './psr/event-manager/EventInterface.php';
}
if (!interface_exists('Psr\\EventManager\\EventManagerInterface')) {
        require_once './psr/event-manager/EventManagerInterface.php';
}

class EventManagerAwareTests implements EventListenerManagerInterface {

        use EventListenerManagerTrait;

        protected $checks = 'asd';

}

$manager = new EventManagerAwareTests();
$manager->setEventManager(new EventManager());

$manager->addEventListener('TestEvent', function (EventInterface $e) {
        var_dump($e, $this->checks); // will dump the event object and 'asd'
}, 0, true);

$manager->fire('TestEvent', $manager);

  Files folder image Files (11)  
File Role Description
Files folder imagepsr (1 directory)
Files folder imagesrc (4 files)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file license Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (11)  /  psr  
File Role Description
Files folder imageevent-manager (2 files)

  Files folder image Files (11)  /  psr  /  event-manager  
File Role Description
  Plain text file EventInterface.php Class Class source
  Plain text file EventManagerInterface.php Class Class source

  Files folder image Files (11)  /  src  
File Role Description
  Plain text file Event.php Class Class source
  Plain text file EventListenerManagerInterface.php Class Class source
  Plain text file EventListenerManagerTrait.php Class Class source
  Plain text file EventManager.php Class Class source

  Files folder image Files (11)  /  tests  
File Role Description
  Accessible without login Plain text file event_listeners_tests.php Example Example script
  Accessible without login Plain text file event_manager_tests.php Example Example script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads Download Rankings  
 100%
Total:138
This week:0
All time:9,240
This week:39Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:71%StarStarStarStar
Rank:212