damasco/event-dispatcher

PSR-14 EventDispatcher

dev-master 2020-04-11 08:53 UTC

This package is auto-updated.

Last update: 2024-10-11 19:54:54 UTC


README

Installation

$ composer require damasco/event-dispatcher

Providers

1. Simple provider

Example:

$provider = new Damasco\EventDispatcher\Provider\ListenerProvider();
$provider->addListener(ExampleEvent::class, function (ExampleEvent $event) {
    // code...
});
$provider->addListener(ExampleEvent::class, new class {
    public function __invoke(ExampleEvent $event)
    {
        // code..
    }
});
$eventDispatcher = new Damasco\EventDispatcher\EventDispatcher($provider);
$eventDispatcher->dispatch(new ExampleEvent(...));

2. Container aware provider

Example:

/** @var \Psr\Container\ContainerInterface $container */
$provider = new Damasco\EventDispatcher\Provider\ContainerAwareListenerProvider($container);
$provider->addListener(ExampleEvent::class, function (ExampleEvent $event) {
    // code...
});
$provider->addListener(ExampleEvent::class, new class {
    public function __invoke(ExampleEvent $event)
    {
        // code..
    }
});
// `ExampleEventListener` must be invokable class
$provider->addListener(ExampleEvent::class, ExampleEventListener::class);
$eventDispatcher = new Damasco\EventDispatcher\EventDispatcher($provider);
$eventDispatcher->dispatch(new ExampleEvent(...));

Stoppable event

class StoppableEvent implements Psr\EventDispatcher\StoppableEventInterface
{
    // code...

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