https://github.com/activecollab/eventsdispatcher
Simple events dispatcher
https://github.com/activecollab/eventsdispatcher
Last synced: about 1 year ago
JSON representation
Simple events dispatcher
- Host: GitHub
- URL: https://github.com/activecollab/eventsdispatcher
- Owner: activecollab
- License: mit
- Created: 2017-04-04T19:36:01.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2019-06-23T19:40:09.000Z (almost 7 years ago)
- Last Synced: 2025-03-30T13:03:28.314Z (about 1 year ago)
- Language: PHP
- Size: 16.6 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# EventsDispatcher
[](https://travis-ci.org/activecollab/eventsdispatcher)
This package offers simple events dispatcher, with listeners. Key concepts:
1. Events are not arbitrary strings, but instances that encapsulate all relevant event data,
1. When you specify a listener, you specify an event class (or interface) that you want to listen to,
1. You can listen to entire classes of events, by specifying general enough event class (or interface),
1. Listeners are callables, and event is always passed to it as the first (and only) argument.
General listener example:
```php
listen(LicenseRenewedEventInterface::class, function (LicenseRenewedEventInterface $event) {
print "License {$event->getLicenseKey()} has been renewed\n";
});
```
To specify a global listener, that handles all events, just go highly general with the specification:
```php
listen(EventInterface::class, function (EventInterface $event) {
print "Event " . get_class($event) . " handled\n";
});
```
Similar approach can be used to handle a class of events. Instead of using the base `EventInterface`, register listener to a class, or interface that all events of a targeted type extend, or implement.
To trigger an event, call `trigger()` method with event as the first (and only) argument:
```php
trigger(new LicenseRenewedEvent(
'123',
'2016-12-31',
'2017-12-31',
699.0
));
```