lubobill1990/event-dispatcher

Yii Event Dispatcher

dev-master / 3.0.x-dev 2020-08-24 17:02 UTC

This package is auto-updated.

Last update: 2024-03-25 01:24:30 UTC


README

993323

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 Code Coverage Scrutinizer Code Quality Mutation testing badge static analysis

Features

  • PSR-14 compatible.
  • Simple and lightweight.
  • Encourages designing event hierarchy.
  • Can combine multiple 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 listeners for it from a provider and call them sequentially.

// add some listeners
$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(function (AfterDocumentProcessed $event) {
        $document = $event->getDocument();
        // do something with document
    });

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

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:

$listeners->add(function (DocumentEvent $event) {
    // log events here
});

Combining multiple listener providers

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

$compositeProvider = new Yiisoft\EventDispatcher\Provider\CompositeProvider();
$provider = new Yiisoft\EventDispatcher\Provider\Provider();
$compositeProvider->add($provider);
$compositeProvider->add(new class implements ListenerProviderInterface {
    public function getListenersForEvent(object $event): iterable
    {
        yield function ($event) {
            // handle 
        };
    }
});

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

Register listeners with concrete event names

You may use a more simple listener provider, which allows you to specify which event they can provide.

It can be useful in some specific cases, for instance if one of your listeners does not need the event object passed as a parameter (can happen if the listener only needs to run at a specific stage during runtime, but does not need event data).

In that case, it is advised to use the aggregate (see above) if you need features from both providers included in this library.

$listeners = (new \Yiisoft\EventDispatcher\Provider\ListenerCollection())
    ->add(static function () {
    // this function does not need an event object as argument
}, SomeEvent::class);

Unit testing

The package is tested with PHPUnit. To run tests:

./vendor/bin/phpunit

Mutation testing

The package tests are checked with Infection mutation framework. To run it:

./vendor/bin/infection

Static analysis

The code is statically analyzed with Phan. To run static analysis:

./vendor/bin/phan

Credits

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