Git Product home page Git Product logo

php-event-dispatcher's Introduction

PSR-14 Event Dispatcher

Quality Gate Status

Event Dispatching is a common and well-tested mechanism to allow developers to inject logic into an application easily and consistently. This library was developed according to PSR-14 and all interfaces in psr/event-dispatcher were implemented.

You can download this project as follows.

git clone [email protected]:pleets/php-event-dispatcher.git

Usage

Creating Events

An Event is a message produced by an Emitter. It may be any arbitrary PHP object. You can create an event extending the Event class.

use Pleets\EventDispatcher\Event;

class DepositEvent extends Event
{
    public string $text;
    protected string $amount;
    private string $currency = 'USD';

    public function __construct($amount)
    {
        $this->amount = $amount;
        $this->text = 'We are preparing your deposit: '.$this->amount.$this->currency;
    }
}

Creating Listeners

A Listener is any PHP callable that expects to be passed an Event. Zero or more Listeners may be passed the same Event. A Listener MAY enqueue some other asynchronous behavior if it so chooses. You can create a listener extending the Listener class.

use Pleets\EventDispatcher\Event;
use Pleets\EventDispatcher\Listener;

class SendDepositNotification extends Listener
{
    public function handle(Event $event): void
    {
        $event->text = 'Your deposit was done!';
    }
}

Creating Listener Providers and subscribing events

A Listener Provider is responsible for determining what Listeners are relevant for a given Event, but MUST NOT call the Listeners itself. A Listener Provider may specify zero or more relevant Listeners. You can create a listener provider as follows.

$provider = new ListenerProvider();

$deposit = new DepositEvent('127.00');
$provider->subscribe($deposit, new SendDepositNotification());

Dispatching your events

A Dispatcher is a service object that is given an Event object by an Emitter. The Dispatcher is responsible for ensuring that the Event is passed to all relevant Listeners, but MUST defer determining the responsible listeners to a Listener Provider. You can dispatch you events as follows.

$dispatcher = new Dispatcher($provider);
$dispatcher->dispatch($deposit);

All listeners will be informed about this event and will be executed.

php-event-dispatcher's People

Contributors

darioriverat avatar

Watchers

 avatar  avatar

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.