Giter VIP home page Giter VIP logo

event-interface's Introduction

Dhii - Event Interface

Continuous Integration Latest Stable Version Latest Unstable Version

Details

The PSR-14 standard provides interfaces used for dispatching events. But what of events themselves? How can a consumer of the dispatcher (i.e. the emitter) interoperate with the handler, if they have no agreement on what an event may look like? This package aims to provide interfaces that could facilitate the interoperability of events that are identified by event name.

Usage

Basic Features

In essence, an event is but a map of parameters associated with a name.

<?php
use Dhii\Events\Event\StoppableEventFactoryInterface;

/* @var $factory StoppableEventFactoryInterface */
$event = $factory->make('my_event', ['key' => 'value']);
echo $event->getName(); // 'my_event'
echo $event->getParam('key'); //  'value'

$params = $event->getParams();
var_dump($params); // ['key' => 'value']

$params['hello'] = 'world';
$event->setParams($params);
echo $event->getParam('hello'); // 'world'
?>

With PSR-14

Events cam be dispatched using a standards-compliant event dispatcher. This example requires the suggested dhii/event-dispatcher-interface package.

<?php
use Psr\EventDispatcher\EventDispatcherInterface;
use Dhii\Events\Event\EventInterface;
use Dhii\Events\Event\StoppableEventFactoryInterface;
use Dhii\Events\Listener\AddListenerCapableInterface;

/* @var $dispatcher EventDispatcherInterface */
/* @var $factory StoppableEventFactoryInterface */
/* @var $listenerMap AddListenerCapableInterface */

// First listener will change a value and stop propagation
$listenerMap->addListener('my_event', function (EventInterface $event) {
    $params = $event->getParams();
    $params['key'] = 'other_value';
    $event->setParams($params);
    $event->stopPropagation();
}, 1);

// Second listener is never run, therefore the value does not change again
$listenerMap->addListener('my_event', function (EventInterface $event) {
    $params = $event->getParams();
    $params['key'] = 'yet another value';
    $event->setParams($params);
}, 2);

$event = $factory->make('my_event', ['key' => 'value']);
$dispatcher->dispatch($event);
echo $event->getParam('key'); // 'other_value'
?>

event-interface's People

Contributors

mecha avatar xedinunknown avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

seldaek pierredup

event-interface's Issues

Add factories

There's nothing that can create events or event managers.

Add factory interfaces.

Add config key factory constants

Factory consumers cannot know which keys they should use when calling make().

Add constants to represent the name and params keys at least.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.