PHP code example of silverstripe / event-dispatcher
1. Go to this page and download the library: Download silverstripe/event-dispatcher 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/ */
silverstripe / event-dispatcher example snippets
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;
Dispatcher::singleton()->trigger('createOrder', Event::create(
null,
[
'id' => $order->ID,
'paymentMethod' => $order->PaymentMethod
]
));
use SilverStripe\EventDispatcher\Dispatch\EventHandlerInterface;
use SilverStripe\EventDispatcher\Event\EventContextInterface;
class OrderHandler implements EventHandlerInterface
{
public function fire(EventContextInterface $context): void
{
$orderID = $context->get('id');
$paymentMethod = $context->get('paymentMethod');
// Do something in response to the order being created
}
}
use SilverStripe\EventDispatcher\Dispatch\DispatcherLoaderInterface;
use SilverStripe\EventDispatcher\Dispatch\EventDispatcherInterface;
class MyLoader implements DispatcherLoaderInterface
{
public function addToDispatcher(EventDispatcherInterface $dispatcher) : void
{
$dispatcher->addListener('myEvent', $myHandler);
$dispatcher->removeListener('anotherEvent', $anotherHandler);
}
}
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;
Dispatcher::singleton()->trigger('formSubmitted', Event::create(
'contact',
[
'name' => $formData['Name'],
// etc..
]
));
public function trigger(string $event, EventContextInterface $context): void
{
error_log($event);
error_log($context->getAction());
// ...
use SilverStripe\EventDispatcher\Dispatch\Dispatcher;
use SilverStripe\EventDispatcher\Symfony\Event;
Dispatcher::singleton()->trigger('formSubmitted', Event::create(
'contact',
[
'name' => $formData['Name'],
// etc..
]
));
public function fire(EventContextInterface $context): void
{
$name = $context->get('Name');
// do more stuff...
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.