PHP code example of milpa / events

1. Go to this page and download the library: Download milpa/events library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

milpa / events example snippets


use Milpa\Eventing\EventDispatcher;
use Psr\Log\NullLogger;

$dispatcher = new EventDispatcher(new NullLogger());

$dispatcher->subscribe('user.*', function (string $event, array $payload): void {
    // catches every one-segment event under `user.` — user.created, user.updated, ...
});

$dispatcher->subscribe('user.created', function (string $event, array $payload): void {
    // runs before the wildcard handler above: higher priority
}, priority: 10);

$dispatcher->dispatch('user.created', ['id' => 42]);

$dispatcher->hasSubscribers('user.created'); // true
$dispatcher->hasSubscribers('order.created'); // false — no exact or wildcard match

$dispatcher->subscribe('order.placed', fn () => throw new \RuntimeException('boom'));
$dispatcher->subscribe('order.placed', fn () => /* still runs */ null);

$dispatcher->dispatch('order.placed'); // both handlers ran; the exception was logged, not thrown

$dispatcher->setAsyncDispatcher(function (string $event, array $payload): void {
    // hand off to your queue (Symfony Messenger, a Doctrine-backed job table, ...)
});

$dispatcher->dispatch('order.placed', ['id' => 1], async: true); // -> queue, not inline