PHP Classes
elePHPant
Icontem

ITE Event Manager: Manage events implementing PSR-14 interface

Recommend this page to a friend!
  Info   View files Example   View files View files (11)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2017-01-06 (14 hours ago) RSS 2.0 feedStarStarStarStar 71%Total: 96 This week: 14All time: 8,549 This week: 60Up
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
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
  Performance   Level  
Name: Kiril Savchev <contact>
Classes: 6 packages by
Country: Bulgaria Bulgaria
Innovation award
Innovation award
Nominee: 5x

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  
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  /  psr  
File Role Description
Files folder imageevent-manager (2 files)

  Files folder image Files  /  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  /  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  /  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

 Version Control Unique User Downloads Download Rankings  
 100%
Total:96
This week:14
All time:8,549
This week:60Up
 User Ratings  
 
 All time
Utility:91%StarStarStarStarStar
Consistency:91%StarStarStarStarStar
Documentation:75%StarStarStarStar
Examples:91%StarStarStarStarStar
Tests:-
Videos:-
Overall:71%StarStarStarStar
Rank:255