antidot-fw/event-dispatcher

Antidot 框架 PSR-14 事件分发器库

2.1.0 2021-12-06 12:10 UTC

This package is auto-updated.

Last update: 2024-08-30 01:32:10 UTC


README

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

实现PSR 14 事件分发器

安装

使用 Composer

composer require antidot-fw/event-dispatcher

使用 Laminas 配置聚合器

它会自动安装库

install

使用工厂

配置

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

工厂

<?php

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

$factory = new EventDispatcherFactory();

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

异步事件分发器工厂

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);

用法

发送事件

<?php

use Psr\EventDispatcher\EventDispatcherInterface;

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

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

异步模式发送事件

<?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()