fyre / event
An event library.
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.59
- fyre/php-cs-fixer-config: ^1.0
- phpunit/phpunit: ^11
README
FyreEvent is a free, open-source events library for PHP.
Table Of Contents
Installation
Using Composer
composer require fyre/event
In PHP:
use Fyre\Event\EventManager;
Basic Usage
$parentEventManager
is an EventManager that will handle propagated events, and will default to null.
$eventManager = new EventManager($parentEventManager);
Methods
Add Listener
Add an EventListener.
$eventListener
is an EventListener.
$eventManager->addListener($eventListener);
Clear
Clear all events.
$eventManager->clear();
Dispatch
Dispatch an Event.
$event
is an Event.
$eventManager->dispatch($event);
Has
Check if an event exists.
$name
is a string representing the event name.
$hasEvent = $eventManager->has($name);
Off
Remove event(s).
$name
is a string representing the event name.$callback
is the callback to remove.
$eventManager->off($name, $callback);
If the $callback
argument is omitted, all events will be removed instead.
$eventManager->off($name);
On
Add an event.
$name
is a string representing the event name.$callback
is the callback to execute.$priority
is a number representing the callback priority, and will default to EventManager::PRIORITY_NORMAL.
$eventManager->on($name, $callback, $priority);
Remove Listener
Remove an EventListener.
$eventListener
is an EventListener.
$eventManager->removeListener($eventListener);
Trigger
Trigger an event.
$name
is a string representing the event name.
Any additional arguments supplied will be passed to the event callback.
$event = $eventManager->trigger($name, ...$args);
Events
use Fyre\Event\Event;
$name
is a string representing the name of the Event .$subject
is an object representing the Event subject, and will default to null.$data
is an array containing the Event data, and will default to [].
$event = new Event($name, $subject, $data);
Get Data
Get the Event data.
$data = $event->getData();
Get Name
Get the Event name.
$name = $event->getName();
Get Result
Get the Event result.
$result = $event->getResult();
Get Subject
Get the Event subject.
$subject = $event->getSubject();
Is Default Prevented
Determine whether the default Event should occur.
$isDefaultPrevented = $event->isDefaultPrevented();
Is Propagation Stopped
Determine whether the Event propagation was stopped.
$isPropagationStopped = $event->isPropagationStopped();
Is Stopped
Determine whether the Event was stopped.
$isStopped = $event->isStopped();
Prevent Default
Prevent the default Event.
$event->preventDefault();
Set Data
$data
is an array containing the Event data.
$event->setData($data);
Set Result
$result
is the Event result.
$event->setResult($result);
Stop Immediate Propagation
Stop the Event propagating immediately.
$event->stopImmediatePropagation();
Stop Propagation
Stop the Event propagating.
$event->stopPropagation();
Event Listeners
Custom event listeners can be created by implementing the Fyre\Event\EventListenerInterface
, ensuring all below methods are implemented.
use Fyre\Event\EventListenerInterface; class MyListener implements EventListenerInterface { }
Implemented Events
Get the implemented events.
$events = $listener->implementedEvents();
Event Dispatchers
Custom event dispatchers can be created by using the Fyre\Event\EventDispatcherTrait
.
use Fyre\Event\EventDispatcherTrait; class MyDispatcher { use EventDispatcherTrait; }
Dispatch Event
Dispatch an Event.
$name
is a string representing the event name.$data
is an array containing the Event data, and will default to [].$subject
is an object representing the Event subject, and will default to the event dispatcher.
$this->dispatchEvent($name, $data, $subject).
Get Event Manager
Get the EventManager.
$eventManager = $this->getEventManager();
Set Event Manager
Set the EventManager.
$eventManager
is an EventManager.
$this->setEventManager($eventManager);