Skip to content

antidot-framework/antidot-event-dispatcher

Antidot event dispatcher

Scrutinizer Code Quality Code Coverage Infection MSI Type Coverage Build Status Maintainability

Psr 14 Event dispatcher implementation.

Installation

Using composer

composer require antidot-fw/event-dispatcher

it install the library automatically

install

Using factory:

Config

<?php
/** @var \Psr\Container\ContainerInterface $container */
$container->set('config', [
    'app-events' => [
        'event-listeners' => [
//            SomeEvent::class => [
            'some.event' => [
                SomeEventListener::class,
                SomeEventOtherListener::class,
            ]
        ]
    ]
]);

factory

<?php

use Antidot\Event\Container\EventDispatcherFactory;
use Psr\EventDispatcher\EventDispatcherInterface;

$factory = new EventDispatcherFactory();

$eventDispatcher = $factory->__invoke($container);
$container->set(EventDispatcherInterface::class, $eventDispatcher);

Async Event Dispatcher Factory

composer require react/event-loop
<?php

use Antidot\Event\Container\AsyncEventDispatcherFactory;
use Psr\EventDispatcher\EventDispatcherInterface;

$factory = new AsyncEventDispatcherFactory();

$eventDispatcher = $factory->__invoke($container);
$container->set(EventDispatcherInterface::class, $eventDispatcher);

Usage

Send events

<?php

use Psr\EventDispatcher\EventDispatcherInterface;

/** @var \Psr\Container\ContainerInterface $container */
$eventDispatcher = $container->get(EventDispatcherInterface::class);

$eventDispatcher->dispatch(SomeEvent::occur());

Send events Async mode

<?php

use Psr\EventDispatcher\EventDispatcherInterface;
use React\EventLoop\Loop;

/** @var \Psr\Container\ContainerInterface $container */
$eventDispatcher = $container->get(EventDispatcherInterface::class);

$eventDispatcher->dispatch(SomeEvent::occur());

Loop::run()