Download the PHP package mario-legenda/php-event-emitter without Composer
On this page you can find all versions of the php package mario-legenda/php-event-emitter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download mario-legenda/php-event-emitter
More information about mario-legenda/php-event-emitter
Files in mario-legenda/php-event-emitter
Package php-event-emitter
Short Description EventEmitter port from node to PHP
License MIT
Informations about the package php-event-emitter
Introduction
This project is a light port of Nodes EventEmitter class. Although, it does not have all the features that Node EventEmitter has, it has all that you need to implement the Observer pattern right away in your classes with minimal code refactoring.
Installation
Supports PHP >7.0
composer require mario-legenda/php-event-emitter
Usage by extending the EventEmitter class
An alternative to calling EventEmitter::on() with a closure would be to
create a class that implements the EventEmitter\CallableInterface that exposes
a single method run()
. The drawback is that in PHP, we cannot set a variable
number of parameters for the run()
method. Therefor, if you want to use this interface,
you would have to nullify every one of the arguments that you expect to receive.
For example...
Multiple event handlers
It is possible to chain multiple identical events
Usage within an object
As you can see, the two examples are almost identical. Creating the EventEmitter instance inside the Task class is not proper dependency injection but it enables us to use the EventEmitter seamlessly. The client code does not now that Task is using (or is) an EventEmitter.
Exception handling
EventEmitter can handle internal exceptions thrown by your code.
If you chain multiple events, every event after the one where an exception occurred will not be called.
API
EventEmitter::__construct($handleErrors = false)
EventEmitter constructor. If provided true
as the first argument, exceptions will
be handled by the EventEmitter. Else, exceptions will be propagated to client code
EventEmitter::emit(string $eventName, ...args): void
Registers an event to be called with EventEmitter::on()
. Must be called before EventEmitter::on()
.
EventEmitter::on(string $eventName, $callback): EventEmitter
Executes an event. $callback
has to be an instance of \Closure
or a instance of EventEmitter\CallableInterface
EventEmitter::exception($callback)
Called only when $handleError
argument is set to true
in the constructor and an exception occurred in
executing one of the events.
EventEmitter::removeEvent(string $eventName, bool $strictCheck = false): bool
Removes an event and returns true
if successful or false
on failure. If $strictCheck
is true,
an exception is throw if an event cannot be removed (if it doesn't exist for example).
EventEmitter::removeAllEvents()
Removes all events.
EventEmitter::getEventNames(): ?array
Returns an array of currently registered event names or null
if there are none