PHPackages                             archipro/silverstripe-revolt-event-dispatcher - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. archipro/silverstripe-revolt-event-dispatcher

ActiveSilverstripe-vendormodule[Utility &amp; Helpers](/categories/utility)

archipro/silverstripe-revolt-event-dispatcher
=============================================

A Revolt Event Dispatcher integration for Silverstripe CMS

0.2.0(1y ago)181.5k↓14.7%[2 issues](https://github.com/archiprocode/silverstripe-revolt-event-dispatcher/issues)MITPHPPHP ^8.1CI passing

Since Nov 17Pushed 1y ago1 watchersCompare

[ Source](https://github.com/archiprocode/silverstripe-revolt-event-dispatcher)[ Packagist](https://packagist.org/packages/archipro/silverstripe-revolt-event-dispatcher)[ RSS](/packages/archipro-silverstripe-revolt-event-dispatcher/feed)WikiDiscussions 0 Synced 1mo ago

READMEChangelog (3)Dependencies (11)Versions (4)Used By (0)

Silverstripe CMS RevoltEvent Dispatcher Module (experimental)
=============================================================

[](#silverstripe-cms-revoltevent-dispatcher-module-experimental)

[![CI](https://github.com/archiprocode/silverstripe-revolt-event-dispatcher/actions/workflows/ci.yml/badge.svg)](https://github.com/archiprocode/silverstripe-revolt-event-dispatcher/actions/workflows/ci.yml)

This module adds the ability to dispatch and listen for events in Silverstripe CMS. It's built around Revolt PHP and AMPHP. It aims to process events asynchronously. It also provides some abstraction to help managing event around common DataObject operations.

Installation
------------

[](#installation)

```
composer require archipro/silverstripe-revolt-event-dispatcher
```

Features
--------

[](#features)

- Automatic event dispatching for DataObject operations (create, update, delete)
- Support for versioned operations (publish, unpublish, archive, restore)
- Asynchronous event handling using Revolt Event Loop

Setting up the Event Loop
-------------------------

[](#setting-up-the-event-loop)

Because we are using Revolt PHP, you need to run the event loop to process the events.

Somewhere in your code you need to start the event loop by running `\Revolt\EventLoop::run()`. This will process all the events up to that point.

A simple approach is to put it at the end of your `public/index.php` file in a `try-finally` block. You can also add a `fastcgi_finish_request()` call to ensure all output is sent before processing the events.

```
try {
    $kernel = new CoreKernel(BASE_PATH);
    $app = new HTTPApplication($kernel);
    $response = $app->handle($request);
    $response->output();
} finally {
    // This call will complete the request without closing the PHP worker. A nice side effect of this is that your
    // event listeners won't block your request from being sent to the client. So you can use them to run slow
    // operations like sending emails or doing API calls without delaying the response.
    session_write_close();
    fastcgi_finish_request();

    // Many methods in Silverstripe CMS rely on having a current controller with a request.
    $controller = new Controller();
    $controller->setRequest($request);
    $controller->pushCurrent();

    // Now we can process the events in the event loop
    \Revolt\EventLoop::run();
}
```

### TODO

[](#todo)

- Need to find a an elegant way to run the event loop on `sake` commands. This won't hit `public/index.php`.

Basic Usage
-----------

[](#basic-usage)

### Firing a Custom Event

[](#firing-a-custom-event)

```
use SilverStripe\Core\Injector\Injector;
use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;

// Create your event class
class MyCustomEvent
{
    public function __construct(
        private readonly string $message
    ) {}

    public function getMessage(): string
    {
        return $this->message;
    }
}

// Dispatch the event
$event = new MyCustomEvent('Hello World');
$service = Injector::inst()->get(EventService::class);
$service->dispatch($event);
```

### Adding a Simple Event Listener

[](#adding-a-simple-event-listener)

```
use SilverStripe\Core\Injector\Injector;
use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;

// Add a listener
$service = Injector::inst()->get(EventService::class);
$service->addListener(MyCustomEvent::class, function(MyCustomEvent $event) {
    error_log('MyCustomEventListener::handleEvent was called');
});
```

### Configuration-based Listeners

[](#configuration-based-listeners)

You can register listeners via YAML configuration:

```
ArchiPro\Silverstripe\EventDispatcher\Service\EventService:
  listeners:
    MyCustomEvent:
      - ['MyApp\EventListener', 'handleEvent']
```

Registering many listeners at once with loaders
-----------------------------------------------

[](#registering-many-listeners-at-once-with-loaders)

You can use listeners loaders to register many listeners at once.

```
