circli/event-dispatcher

PSR-14 event dispatcher implementation.

2.5.0 2021-11-23 11:22 UTC

This package is auto-updated.

Last update: 2021-11-23 11:25:47 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status

The package provides a standard event dispatcher, as well as one null dispatcher that can be used as default dispatcher in the EventDispatcherAwareInterface

Installation

Via Composer

$ composer require circli/event-dispatcher

Usage

The dispatcher accepts a Psr\EventDispatcher\ListenerProviderInterface to its constructor

Basic example:

use Circli\EventDispatcher\EventDispatcher;
use Circli\EventDispatcher\ListenerProvider\DefaultProvider;

$provider = new DefaultProvider();
$dispatcher = new EventDispatcher($provider);

$provider->listen(Event::class, function(Event $e) {
    // do stuff
});

$dispatcher->dispatch(new Event());

Providers

The package includes a couple of providers for ease of use. You can also find some useful provider in fig/event-dispatcher-util

DefaultProvider

use Circli\EventDispatcher\ListenerProvider\DefaultProvider;
$provider = new DefaultProvider();
$provider->listen(Event::class, $listener);

PriorityProvider

use Circli\EventDispatcher\ListenerProvider\PriorityProvider;
$provider = new PriorityProvider();

//Add listener with lower than default priority
$provider->listen(Event::class, $listener, 900);

//Add listener with higher than default priority
$provider->listen(Event::class, $listener, 1100);

//Add listener with normal priority
$provider->listen(Event::class, $listener);

ContainerListenerProvider

Use a Psr-11 container to lazy load the callbacks. This is cleaner why to do lazy loading then the LazyListenerFactory

use Circli\EventDispatcher\ListenerProvider\ContainerListenerProvider;
$container = new SomePsr11Container();

$provider = new ContainerListenerProvider($container);
$provider->addService(Event::class, EventListener::class);

PriorityAggregateProvider

use Circli\EventDispatcher\ListenerProvider\PriorityAggregateProvider;
use Circli\EventDispatcher\ListenerProvider\DefaultProvider;

$aggregateProvider = new PriorityAggregateProvider();
$aggregateProvider->addProvider(new DefaultProvider());

// Add with higher than default priority
$aggregateProvider->addProviderWithPriority(new DefaultProvider(), 1500);

// Add with lower than default priority
$aggregateProvider->addProviderWithPriority(new DefaultProvider(), 500);

FilterableProvider

use Circli\EventDispatcher\ListenerProvider\FilterableProvider;

$provider = new FilterableProvider();

$provider->listen(RandomEvent::class, $listener, function ($event) {
    return ifRandomExternalThingIsTrue();
});

LazyListenerFactory

We also include a factory class to create lazy loaded handlers.

use Circli\EventDispatcher\LazyListenerFactory;
use Circli\EventDispatcher\ListenerProvider\DefaultProvider;
use Psr\Container\ContainerInterface;

$lazyFactory = new LazyListenerFactory($psr11Container);

$listener = $lazyFactory->lazy('SomeService');
$provider = new DefaultProvider();
$provider->listen(Event::class, $listener);