internetpixels / event-manager
Handle events in your application with this PHP library
Installs: 1 550
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 1
Forks: 0
Open Issues: 0
Requires
- php: >=7.1
Requires (Dev)
- phpunit/phpunit: ^6.5
This package is auto-updated.
Last update: 2025-02-16 17:23:07 UTC
README
Handle events and triggers in your application with this PHP library. No hassle with the correct hooking time of listeners, simple set the priority.
This is a open-source library. Please consider a link to this repository when you're actively using it.
Installation
Install this PHP event manager by using composer:
composer require internetpixels/event-manager
Basic examples
Register an event
Before you're able to use a new event, you'll need to register it. Find a logical place in your application, preferably in the start of the runtime.
The event needs to be registered before you can add any listeners to it, otherwise this library will throw an exception!
$eventManager = new EventManager();
$event = new EventEntity();
$event->setKey( 'test.event.after.post' );
$eventManager->registerEvent( $event );
Register a listener
After the event is added, you can "hook" new listeners to the event. This means, when the event is triggered, it will call all triggers with their given priority.
Priority 1 is the most important and the default priority is 50. The event manager sorts the listeners automatically, so it doesn't matter in what order you add the listeners to the event manager.
A listener can only have 1, required, callback method. This method will be called once the event is executed.
$listener = new EventListenerEntity();
$listener->setEventKey( 'test.event.after.post' );
$listener->setPriority( 20 );
$listener->setCallback( [ $this, 'eventCallback' ] );
$eventManager->registerListener( $listener );
Callback example
Each listener has a callback method, a basic callback method may look like this in your application. You should receive the $params
array, and return them as an array for further usage.
public function eventCallback( $params = null ) {
// TODO: Do something with the parameters
var_dump($params);
return $params;
}
Execute the event
When your setup is completed with the event and at least one listener, you can execute the event in your application. This will trigger all listener(s) with their given priority.
$eventManager->executeEvent( 'test.event.after.post' )
Using the events as filters
You can use the events as filters. The registration process is nearly the same as a normal event, you only have to enable parameters on the event registration and add them in the executeEvent
method.
The executeEvent
method will return the parameters.
$event = new EventEntity();
$event->setParams( true );
$event->setKey( 'test.event.after.post' );
$eventManager->registerEvent( $event );
$executed = $eventManager->executeEvent( 'test.event.after.post', [
'first parameter value',
] );
var_dump( $executed );
The parameters are now available in the event callback $params
array.
Multiple params in execution
It is very easy to add more parameters in your execution. Just add new array values in the execute method.
$executed = $eventManager->executeEvent( 'test.event.after.post', [
'first parameter value',
'second parameter value',
'third parameter value',
] );