https://github.com/yarcode/simple-events
Simple event dispatching library for PHP
https://github.com/yarcode/simple-events
event eventemitter php7 trait
Last synced: 4 months ago
JSON representation
Simple event dispatching library for PHP
- Host: GitHub
- URL: https://github.com/yarcode/simple-events
- Owner: yarcode
- License: mit
- Created: 2016-10-07T10:23:59.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-16T10:29:32.000Z (almost 7 years ago)
- Last Synced: 2025-04-22T11:14:46.075Z (6 months ago)
- Topics: event, eventemitter, php7, trait
- Language: PHP
- Homepage: https://packagist.org/packages/yarcode/simple-events
- Size: 13.7 KB
- Stars: 2
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Simple Events
Simple event dispatching library for PHP[](https://insight.sensiolabs.com/projects/67392651-70e1-43c7-b815-6cded0d98e32)
[](https://scrutinizer-ci.com/g/yarcode/simple-events/?branch=master)
[](https://scrutinizer-ci.com/g/yarcode/simple-events/?branch=master)
[](https://scrutinizer-ci.com/g/yarcode/simple-events/build-status/master)
[](https://github.com/yarcode/simple-events/blob/master/LICENSE)## Installation
### Composer
The preferred way to install this extension is through [Composer](http://getcomposer.org/).
Either run
```
composer require yarcode/simple-events
```or add
```
"yarcode/simple-events": "*"
```to the require section of your `composer.json`
## Usage
### Via EventEmitterTrait
Attach `\YarCode\Events\EventEmitterTrait` to your class to make it `EventEmitter`
```php
class MyClass {
use \YarCode\Events\EventEmitterTrait;
...
}
$emitter = new MyClass();
```### Standalone EventEmitter
Or create an instance of `\YarCode\Events\EventEmitter`.
```php
$emitter = new \YarCode\Events\EventEmitter();
```### Adding listeners
You can add `callable` listener for any string event name.
```
$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
echo "{$event->name} was emitted";
});
$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
echo "{$event->name} was emitted one more time";
});
```### Emitting events
You can emit named event with default event object.
```php
$emitter->emit('TestEvent');
// TestEvent was emitted
// TestEvent was emitted one more time
```
Or you can pass `\YarCode\Events\Event` object to the listeners.
```php
$event = new \YarCode\Events\Event();
$event->payload['key'] = 'value';
$emitter->emit('TestEvent', $event);
// TestEvent was emitted
// TestEvent was emitted one more time
```
You could pass any data as event payload.
It would be passed as a first parameter to a listener callable.```php
$data = ['foo', 'bar'];
$emitter->emit('MixedDataEvent', $data);
```### Removing listeners
You can remove one concrete listener.
```
$callback = function (\YarCode\Events\Event $event) {
echo "{$event->name} was emitted third time";
});
$emitter->addListener('TestEvent', $callback);
$emitter->removeListener('TestEvent', $callback);
```
Or remove all the listeners for the event.
```php
$emitter->removeAllListeners('TestEvent');
```
Or remove all the listeners for all events.
```php
$emitter->removeAllListeners();
```### Breaking the execution
Set the `$event->handled` property to `true` to stop the further listeners execution.
```php
$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
$event->handled = true;
});
$emitter->addListener('TestEvent', function (\YarCode\Events\Event $event) {
echo "This callback for {$event->name} would never run";
});
$emitter->emit('TestEvent');
```### Accessing the emitter from the listener
Emitter object is being passed as a second parameter to a listener.
```php
$emitter->addListener('TestEmitterAccessEvent', function ($data, $emitter) {
echo "Hello";
$emitter->emit('TestEmitterAccessEvent2', $data);
});
$emitter->addListener('TestEmitterAccessEvent2', function ($data) {
echo " World";
});
$emitter->emit('TestEmitterAccessEvent');
// Hello World
```
## LicenseMIT
## Links ##
* [GitHub repository](https://github.com/yarcode/simple-events)
* [Composer package](https://packagist.org/packages/yarcode/simple-events)