PHP Classes

PHP Event based Inheritance Model: Register handlers to process events in a chain

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
Not yet rated by the usersTotal: 39 All time: 10,911 This week: 660Up
Version License PHP version Categories
event-based-inherita 1.0MIT/X Consortium ...5PHP 5, Language
Description 

Author

This package can register handlers to process events in a chain.

It allows the applications to register callback functions that will be called when an event with a given name is triggered.

Multiple callback functions may be registered for the same event.

When an event is triggered, the package will call the last callback function registered to handle that event.

The event handler function that was called may call other callback functions that were registered before to handle the same event.

Innovation Award
PHP Programming Innovation award nominee
March 2023
Number 8
Event processing is an approach that developers can use to connect different parts of an application.

This way, a change in one part can trigger actions in other application parts.

For instance, if a user can push a button on a Web page, the application can use event processing to trigger the actions associated with the application button that the user pushed.

This package implements an inheritance model to process registered events.

This way, the package allows applications to implement event handlers that can process the events or pass the events to another event handling function registered to handle the same event.

Manuel Lemos
Picture of Smoren  Freelight
  Performance   Level  
Name: Smoren Freelight <contact>
Classes: 38 packages by
Country: Russian Federation Russian Federation
Age: 35
All time rank: 280878 in Russian Federation Russian Federation
Week rank: 22 Up3 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 16x

Example

Event-based inheritance model

????????? ????????? ?????????? ??????: - ?? ?????? ??????? ????? ???????? ?????????????? ?????????? ????????????, ??????? ?????????? ? ???? ??????? ???????; - ??? ????????? ??????? ??????????? ?????? ??????? ?????????? ?? ?????, ?????? ?? ????? ?????? ? ??????????? ??????????? ? ????? ??? ?????????, ???? ??? ??????????.

????? ??????? ??????????? ?????? "??????????? ????????????", ?? ???? ?????? ????????? ?????????? ????? ????????? ?????????? ???? ???????? ???-?? ????, ?????? ????????? ??? ??? ???????? ? ????? ???????.

?????????

composer require smoren/event-based-inheritance-model

???????????? ???????? ??????:

<?php

use Smoren\EventBasedInheritanceModel\EventBus;
use Smoren\EventBasedInheritanceModel\Listener;

// ??????? ????
$bus = new EventBus([
    // ?????? ??????? onTest ? ????????? ?? ???? ??????????
    'onTest' => function(&$params, Listener $listener) {
        $params['a']++;
        echo "onTest (0): ".json_encode($params)."\n";
    },
]);

// ????????? ? ???? ???????????? ??????? onTest ??? ???? ??????????
$bus->addListener('onTest', function(&$params, Listener $listener) {
    $listener->handlePrevious($params); // ? ?????? ?????? ?????????? ?? ????????? ??????????
    $params['a']++;
    echo "onTest (1): ".json_encode($params)."\n";
});

// ????????? ? ???? ???????????? ??????? onTest ??? ???? ??????????
$bus->addListener('onTest', function(&$params, Listener $listener) {
    $listener->handlePrevious($params); // ? ?????? ?????? ?????????? ?? ????????? ??????????
    $params['a']++;
    echo "onTest (2): ".json_encode($params)."\n";
    return array_merge($params, ['b' => 0]);
});

// ?????? ??????? onTest1 ? ????????? ?? ???? ??????????
$bus->addListener('onMyTest', function(&$params, Listener $listener) {
    // ? ?????? ?????? ?????????? ?? ????????? ??????????, ?? ??????????? ???, ??????? ?????? ?? ????? ??????
    $listener->handlePrevious($params);
    $params->a++;
    echo "onMyTest (0): ".json_encode($params)."\n";
});
// ????????? ? ???? ???????????? ??????? onTest1 ??? ???? ??????????
$bus->addListener('onMyTest', function(&$params, Listener $listener) {
    $listener->handlePrevious($params);
    $params->a++;
    echo "onMyTest (1): ".json_encode($params)."\n";
    return $params;
});

// ?????????? ??????? onTest
$params = ['a' => 1];
$result = $bus->trigger('onTest', $params);
print_r($result); // ? ?????????? ??????? ??, ??? ?????? ??????? ? ????? ??????????
/*
onTest (0): {"a":2}
onTest (1): {"a":3}
onTest (2): {"a":4}
Array
(
    [a] => 4
    [b] => 0
)
*/

echo "\n";

// ?????????? ??????? onMyTest
$params = new \stdClass();
$params->a = 1;
$result = $bus->trigger('onMyTest', $params);
print_r($result); // ? ?????????? ??????? ??, ??? ?????? ??????? ? ????? ??????????
/*
onMyTest (0): {"a":2}
onMyTest (1): {"a":3}
stdClass Object
(
    [a] => 3
)
*/

  Files folder image Files (7)  
File Role Description
Files folder imagesrc (4 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 Example Example script

  Files folder image Files (7)  /  src  
File Role Description
  Plain text file EventBus.php Class Class source
  Plain text file EventBusException.php Class Class source
  Plain text file ExampleFactory.php Class Class source
  Plain text file Listener.php Class Class source

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:39
This week:0
All time:10,911
This week:660Up