Parable Events is a simple event system

1.0.0 2021-03-12 08:47 UTC

This package is auto-updated.

Last update: 2024-12-12 23:57:30 UTC


README

Workflow Status Latest Stable Version Latest Unstable Version License

Parable Event is a straightforward event system that gets the job done.

Install

Php 8.0+ and composer are required.

$ composer require parable-php/event

Usage

Events are very simple. You add listeners to events (string values) and then trigger an update with those events. You can pass payloads into the trigger calls, which will get passed to all relevant listeners.

use \Parable\Event\Events;

$eventManager = new Events();

$eventManager->listen('event_number_one', function (string $event, string &$payload) {
    $payload .= '-updated!';
});

$payload = 'event';

$eventManager->trigger('event_number_one', $payload);

echo $payload;

// output: 'event-updated!'

The above example handily shows how to make scalar values modifiable by defining the parameter to the callable as a reference. Passing objects is generally advisable, but sometimes it's the in-place alteration of string values you need.

It's also possible to have a listener trigger on every single event.

$eventManager->listenAll(function (string $event, $payload) {
    echo $event . PHP_EOL;
});

The above example would simply log all events being updated. This can be handy for debugging, but can also be handy to listen to a specific subset of events by matching the event, rather than adding a single listener to all individual events.

API

  • listen(string $event, callable $$listener): void - add listener to an event
  • listenAll(callable $$listener): void - add listener for all events
  • trigger(string $event, $payload): void - trigger an update for an event

Contributing

Any suggestions, bug reports or general feedback is welcome. Use github issues and pull requests, or find me over at devvoh.com.

License

All Parable components are open-source software, licensed under the MIT license.