https://github.com/slince/event-dispatcher
:fire: A flexible event dispatching library for PHP
https://github.com/slince/event-dispatcher
event event-dispatcher event-listener events listener observer-pattern subscriber
Last synced: 2 days ago
JSON representation
:fire: A flexible event dispatching library for PHP
- Host: GitHub
- URL: https://github.com/slince/event-dispatcher
- Owner: slince
- Created: 2014-09-12T01:47:37.000Z (about 11 years ago)
- Default Branch: master
- Last Pushed: 2018-06-24T13:33:56.000Z (over 7 years ago)
- Last Synced: 2025-09-17T04:55:09.700Z (22 days ago)
- Topics: event, event-dispatcher, event-listener, events, listener, observer-pattern, subscriber
- Language: PHP
- Homepage:
- Size: 52.7 KB
- Stars: 9
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Event Dispatcher
[](https://travis-ci.org/slince/event-dispatcher)
[](https://codecov.io/github/slince/event-dispatcher)
[](https://packagist.org/packages/slince/event-dispatcher)
[](https://scrutinizer-ci.com/g/slince/event-dispatcher/?branch=master)### Installation
Install via composer.
```bash
$ composer require slince/event-dispatcher
```### Usage
#### Creates a event dispatcher
```php
$dispatcher = new Slince\EventDispatcher\Dispatcher();
```#### Adds a listener for the specified event
There are two types of listeners: `callable` and `Slince\EventDispatcher\Listener`
- `Slince\EventDispatcher\Listener````php
use Slince\EventDispatcher\ListenerInterface;class FooListener implements ListenerInterface
{
public function handle(Event $event)
{
//do something
}
}$dispatcher->addListener('foo-event-name', new FooListener());
```- `callable`
```php
$dispatcher->addListener('foo-event-name', function(Event $event){
//do something
});
```#### Add a subscriber
```php
use Slince\EventDispatcher\SubscriberInterface;class FooSubscriber implements SubscriberInterface
{
public static function getSubscribedEvents(Event $event)
{
return [
'foo' => 'onFoo',
'bar' => 'onBar'
];
}
public function onFoo(Event $event)
{
//do something
}
public function onBar(Event $event)
{
//do something
}
}$dispatcher->addSubscriber(new FooSubscriber());
```#### Dispatches the event to the registered listeners
Just provides the event name.
```php
$dispatcher->dispatch('foo-event-name');
```You can also dispatch with an event instance.
```php
$dispatcher->dispatch(new Event('foo-event-name'));
```#### Propagation
You can call `stopPropagation` to stop event propagation on the event instance.
```php
$dispatcher->addListener('foo-event-name', function(Event $event){
$event->stopPropagation();
});$emitter->addListener('foo-event-name', function ($event) {
// This will not be triggered
});$dispatcher->dispatch('foo-event-name');
```Checks whether propagation is stopped
```php
$event = new Event('foo-event-name');
$dispatcher->dispatch($event);
$event->isPropagationStopped();
```
### License
The MIT license. See [MIT](https://opensource.org/licenses/MIT)