Giter VIP home page Giter VIP logo

event-dispatcher's Introduction

Yii Event Dispatcher


PSR-14 compatible event dispatcher provides an ability to dispatch events and listen to events dispatched.

Latest Stable Version Total Downloads Build Status Scrutinizer Code Quality

Features

  • PSR-14 compatible.
  • Simple and lightweight.
  • Encourages designing event hierarchy.
  • Can combine mutliple event listener providers.

General usage

The library consists of two parts: event dispatcher and event listener provider. Provider's job is to register listeners for a certain event type. Dispatcher's job is to take an event, get a listeners for it from a provider and call them sequentially.

$provider = new Yiisoft\EventDispatcher\Provider\Provider();
$dispatcher = new Yiisoft\EventDispatcher\Dispatcher($provider);

// adding some listeners
$provider->attach(function (AfterDocumentProcessed $event) {
    $document = $event->getDocument();
    // do something with document
});

The event dispatching may look like:

class DocumentProcessor
{
    public function process(Document $document)
    {
        // process the document
        $dispatcher->dispatch(new AfterDocumentProcessed($document));
    }
}

Stoppable events

Event could be made stoppable by implementing Psr\EventDispatcher\StoppableEventInterface:

class BusyEvent implements Psr\EventDispatcher\StoppableEventInterface
{
    // ...

    public function isPropagationStopped(): bool
    {
        return true;
    }
}

This way we can ensure that only first event listener will be able to handle the event. Another option is to allow stopping propagation in one of the listeners by providing corresponding event method.

Events hierarchy

Events do not have any name or wildcard matching on purpose. Event class names and class/interface hierarchy and composition could be used to achieve great flexibility:

interface DocumentEvent
{
}

class BeforeDocumentProcessed implements DocumentEvent
{
}

class AfterDocumentProcessed implements DocumentEvent
{
}

With the interface above listening to all document-related events could be done as:

$provider->attach(function (DocumentEvent $event) {
    // log events here
});

Combining multiple listener providers

In case you want to combine multiple listener providers, you can use Aggregate:

$aggregate = new Yiisoft\EventDispatcher\Provider\Aggregate();
$provider1 = new Yiisoft\EventDispatcher\Provider\Provider();
$aggregate->attach($provider1);
$aggregate->attach(new class implements ListenerProviderInterface {
    public function getListenersForEvent(object $event): iterable
    {
        yield function ($event) {
            // handle 
        };
    }
});

$dispatcher = new Yiisoft\EventDispatcher\Dispatcher($aggregate);

Credits

  • Larry Garfield (@crell) for initial implementation of deriving callable parameter type.

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.