damasco/event-dispatcher

PSR-14 事件调度器

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

This package is auto-updated.

Last update: 2024-09-11 19:38:10 UTC


README

安装

$ composer require damasco/event-dispatcher

提供者

1. 简单提供者

示例

$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. 容器感知提供者

示例

/** @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(...));

可停止事件

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

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