kuick/event-dispatcher

Kuick Event is a slim PSR-14 Event Dispatcher implementation

v1.1.2 2025-01-14 20:17 UTC

This package is auto-updated.

Last update: 2025-01-15 08:39:27 UTC


README

Latest Version PHP Total Downloads GitHub Actions CI codecov Software License

PSR-14 Event Dispatcher lightweight implementation

Key features

  1. Full PSR-14 compatibility
  2. Easy to use listener registration
  3. Listener prioritization
  4. Support for wildcard listeners (ie. , Prefix)

Examples

  1. Registering listeners to the listener provider
<?php

use Kuick\Event\EventDispatcher;
use Kuick\Event\ListenerProvider;

$provider = new ListenerProvider();
$provider->registerListener(
    'some class name or pattern',
    function () {
        //handle the event
    }
);

$dispatcher = new EventDispatcher($provider);
// $dispatcher->dispatch(new SomeEvent());
  1. Listener prioritization (using stdClass as an event)
<?php

use stdClass;
use Kuick\Event\EventDispatcher;
use Kuick\Event\ListenerPriority;
use Kuick\Event\ListenerProvider;

$provider = new ListenerProvider();
$provider->registerListener(
    stdClass::class,
    function (stdClass $event) {
        //handle the event
    },
    ListenerPriority::HIGH
);
$provider->registerListener(
    stdClass::class,
    function (stdClass $event) {
        //handle the event
    },
    ListenerPriority::LOW
);
$dispatcher = new EventDispatcher($provider);
// it should handle the event with high priority listener first
$dispatcher->dispatch(new stdClass());
  1. Registering wildcard listeners (using stdClass as an event)
<?php

use stdClass;
use Kuick\Event\EventDispatcher;
use Kuick\Event\ListenerProvider;

$provider = new ListenerProvider();
$provider->registerListener(
    '*',
    function (object $event) {
        //handle the event
    }
);
$provider->registerListener(
    'std*',
    function (object $event) {
        //handle the event
    }
);
$dispatcher = new EventDispatcher($provider);
// it should match both listeners and run them sequentialy
$dispatcher->dispatch(new stdClass());