-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
52345ec
commit 925a61c
Showing
1 changed file
with
63 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
||
|
@@ -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 | ||
|
||
|
@@ -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'); | ||
}); | ||
``` | ||
|
||
|
@@ -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 | ||
|
@@ -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. |