Download the PHP package webino/event-emitter without Composer
On this page you can find all versions of the php package webino/event-emitter. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webino/event-emitter
More information about webino/event-emitter
Files in webino/event-emitter
Package event-emitter
Short Description Event Emitter implementation.
License BSD-3-Clause
Informations about the package event-emitter
Webino Event Emitter
Event Emitter implementation.
Recommended Usage
Use event emitter to decouple routine algorithm from an extended logic.
Setup
Quick Use
Emitting an event:
Removing an event handler:
Emitting an event until:
Event handling priority:
Event handler:
API
Event
-
const BEGIN
The beginning priority of the event. -
const BEFORE
Priority before main event. -
const MAIN
Main event priority. -
const AFTER
Priority after main event. -
const FINISH
Priority at the end of the event. -
const OFFSET
Event priority offset. -
string getName()
Get event name. -
EventEmitterInterface getTarget()
Get target object from which event was emitted. -
mixed getValue(string $name, mixed $default = null)
Get event value by name. -
void setValues(iterable $values)
Set event values. -
EventResults getResults()
Returns event results. -
void stop(bool $stop = true)
Indicate whether or not to stop this event. - bool isStopped()
Indicates should stop.
EventEmitter
-
void setEventDispatcher(EventDispatcherInterface $dispatcher)
Inject event dispatcher. -
void on(
string|EventInterface|EventHandlerInterface $event,
string|array<int, string>|callable $callback = null,
int $priority = 1)
Set event handler. -
void off(callable|EventHandlerInterface $callback = null, string|EventInterface $event = null)
Remove event handler. - EventInterface emit(string|EventInterface $event, callable $until = null)
Invoke handlers.
EventResults
-
mixed|null first()
Returns first response. - mixed|null last()
Returns last response.
EventHandler
-
void attachEventEmitter(EventDispatcherInterface $emitter)
Attach event emitter to handler. - void detachEventEmitter(EventDispatcherInterface $emitter)
Detach event emitter from handler.
Architecture
It is possible to have a global dispatcher to attach event handlers to.
Event Lifecycle
The basic idea around events is that we just trigger an event and every action happens in handlers, even the main action. Then we can listen to that event using priorities, if we want to act like a middleware. The event propagation could be stopped at any time.
Using events like someEvent.pre and someEvent.post or someEvent.before, someEvent.after, it doesn't matter, is messy and not recommended, don't do that. Give an event a unique name then attach handlers, main action including, using priorities. Convenient way to do that is to use an event priority constants.
Development
Static analysis:
Coding style check:
Coding style fix:
Testing:
Git pre-commit setup:
Addendum
Please, if you are interested in this library report any issues and don't hesitate to contribute. We will appreciate any contributions on development of this library.