fyre/event

An event library.

v4.0 2024-12-24 06:35 UTC

This package is auto-updated.

Last update: 2024-12-24 06:36:09 UTC


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.

$eventManager->addListener($eventListener);

Clear

Clear all events.

$eventManager->clear();

Dispatch

Dispatch 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.

$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);