Event dispatcher
Observer for events, (*1)
Usage Examples
Installation
You can install this package via packagist.org with composer., (*2)
composer require systream/event-dispatcher
, (*3)
composer.json:, (*4)
"require": {
"systream/event-dispatcher": "1.*"
}
This library requires php 5.6
or higher, but also works on php 5.4., (*5)
Subscribe to event
$eventDispatcher = new EventDispatcher();
$subscription = new EventDispatcher\Subscription('nameOfEvent', function (EventInterface $event) {
// do stuff here
});
$eventDispatcher->subscribe($subscription);
Fire an event
$eventDispatcher = new EventDispatcher();
$eventDispatcher->emit(new EventDispatcher\Event('nameOfEvent'));
Stop Propagation
$eventDispatcher = new EventDispatcher();
$subscription = new EventDispatcher\Subscription('fooBar', function (EventDispatcher\EventInterface $event) {
$event->stopPropagation();
});
$eventDispatcher->subscribe($subscription);
$subscription2 = new EventDispatcher\Subscription('fooBar', function (EventDispatcher\EventInterface $event) {
// do stuff here
});
$eventDispatcher->subscribe($subscription2);
$eventDispatcher->emit(new EventDispatcher\Event('fooBar'));
In this case the second subscription won't be invoked., (*6)
Custom events
The only thing you need to do is that implementing the EventInterface
, (*7)
class MyCustomEvent extends AbstractEvent implements EventInterface
{
/**
* @var string
*/
protected $key = 'order.save';
/**
* @var Order
*/
protected $order;
/**
* @param string $key
*/
public function __construct(Order $order)
{
$this->order = $order;
}
/**
* @return Order
*/
public function getOrder()
{
return $this->order;
}
}
...
$eventDispatcher = new EventDispatcher();
$subscription = new EventDispatcher\Subscription('nameOfEvent', function (OrderEvent $event) {
$order = $orderEvent->getOrder();
// do stuff
});
$eventDispatcher->subscribe($subscription);
Custom Subscriptions
The only thing you need to do is that implementing the SubscriptionInterface
, (*8)
Test
, (*9)