Skip to content

Commit

Permalink
Add more code sample to readme
Browse files Browse the repository at this point in the history
  • Loading branch information
maxime-rainville committed Nov 10, 2024
1 parent 52345ec commit 925a61c
Showing 1 changed file with 63 additions and 8 deletions.
71 changes: 63 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@ common DataObject operations.
composer require archipro/silverstripe-revolt-event-dispatcher
```

## Running the Event Loop
## 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

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

Expand All @@ -31,16 +36,19 @@ try {
// operations like sending emails or doing API calls without delaying the response.
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();
}
```

## 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
### 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

Expand Down Expand Up @@ -78,7 +86,7 @@ use ArchiPro\Silverstripe\EventDispatcher\Service\EventService;
// Add a listener
$service = Injector::inst()->get(EventService::class);
$service->addListener(MyCustomEvent::class, function(MyCustomEvent $event) {
echo $event->getMessage();
error_log('MyCustomEventListener::handleEvent was called');
});
```

Expand All @@ -90,7 +98,54 @@ You can register listeners via YAML configuration:
ArchiPro\Silverstripe\EventDispatcher\Service\EventService:
listeners:
MyCustomEvent:
- ['MyApp\EventListener,handleEvent']
- ['MyApp\EventListener', 'handleEvent']
```
## Registering many listeners at once with loaders
You can use listeners loaders to register many listeners at once.
```php
<?php

use ArchiPro\EventDispatcher\ListenerProvider;
use ArchiPro\Silverstripe\EventDispatcher\Contract\ListenerLoaderInterface;
use ArchiPro\Silverstripe\EventDispatcher\Event\DataObjectEvent;
use ArchiPro\Silverstripe\EventDispatcher\Event\Operation;
use ArchiPro\Silverstripe\EventDispatcher\Listener\DataObjectEventListener;
use SilverStripe\Control\Email\Email;
use SilverStripe\Security\Member;

class MemberListenerLoader implements ListenerLoaderInterface
{
public function loadListeners(ListenerProvider $provider): void
{
DataObjectEventListener::create(
Closure::fromCallable([self::class, 'onMemberCreated']),
[Member::class],
[Operation::CREATE]
)->selfRegister($provider);
}

public static function onMemberCreated(DataObjectEvent $event): void
{
$member = $event->getObject();
error_log('Member created: ' . $member->ID);
Email::create()
->setTo($member->Email)
->setSubject('Welcome to our site')
->setFrom('[email protected]')
->setBody('Welcome to our site')
->send();
}
}
```

Loaders can be registered in your YAML configuration file:
```yaml
ArchiPro\Silverstripe\EventDispatcher\Service\EventService:
loaders:
- MemberListenerLoader
```
## DataObject Event Handling
Expand Down Expand Up @@ -180,4 +235,4 @@ Note that `DataObjectEvent` doesn't store the actual DataObject instance that ca
return `null` if the DataObject has been deleted.

`DataObjectEvent::getObject(true) will attempt to retrieve the exact version of the DataObject that fired the event,
assuming it was versioned.
assuming it was versioned.

0 comments on commit 925a61c

Please sign in to comment.