Download the PHP package icanboogie/event without Composer
On this page you can find all versions of the php package icanboogie/event. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download icanboogie/event
More information about icanboogie/event
Files in icanboogie/event
Package event
Short Description Provides an event API
License BSD-3-Clause
Homepage https://icanboogie.org/
Informations about the package event
Event
The icanboogie/event allows you to provide hooks which other developers can attach to, to be notified when certain events occur inside the application and take action.
Inside ICanBoogie, events are often used to alter initial parameters, take action before/after an operation is processed or when it fails, take action before/after a request is dispatched or to rescue an exception.
Installation
Feature highlights
- Easily implementable.
- Events are typed.
- Events usually have a target object, but simpler event types can also be emitted.
- Event hooks are attached to classes rather than objects, and they are inherited.
- Event hooks can be attached to a finish chain that is executed after the event hooks chain.
- Execution of the event chain can be stopped.
A twist on the Observer pattern
The pattern used by the API is similar to the Observer pattern, although instead of attaching event hooks to objects they are attached to their class. When an event is fired upon a target object, the hierarchy of its class is used to filter event hooks.
Consider the following class hierarchy:
ICanBoogie\Operation
└─ ICanBoogie\Module\Operation\SaveOperation
└─ Icybee\Modules\Node\Operation\SaveOperation
└─ Icybee\Modules\Content\Operation\SaveOperation
└─ Icybee\Modules\News\Operation\SaveOperation
When a ProcessEvent
is emitted with a …\News\Operation\SaveOperation
instance, all event hooks
attached to the classes for this event are called, starting from the event hooks attached to the
instance class (…\News\Operation\SaveOperation
) all the way up to those attached to its root
class.
Thus, event hooks attached to the …\Node\Operation\SaveOperation
class are called when a
ProcessEvent
event is fired with …\News\Operation\SaveOperation
instance. One could consider
that event hooks are inherited.
Getting started
To be emitted, events need an event collection, which holds event hooks. Because a new event collection is created for you when required, you don't need to set one up yourself. Still, you might want to do so if you have a bunch of event hooks that you need to attach while creating the event collection. To do so, you need to define a provider that returns your event collection when required.
The following example demonstrates how to set up a provider that instantiates an event collection with event hooks provided by an app configuration:
Typed events
Events are subclasses of the Event class.
The following code demonstrates how a ProcessEvent
class may be defined:
Event types
If an event has a target, the event is obtained using the for()
method and the target class or
object. If an event doesn't have a target, the event type is the event class.
Namespacing and naming
Event classes should be defined in a namespace unique to their target object. Events targeting
ICanBoogie\Operation
instances should be defined in the ICanBoogie\Operation
namespace.
Firing events
Events are fired with the emit()
function.
Attaching event hooks
Event hooks are attached using the attach()
method of an event collection. The attach()
method
is smart enough to create the event type from the parameter types. This works with any callable:
closure, invokable objects, static class methods, functions.
The following example demonstrates how a closure may be attached to a BeforeProcessEvent
event.
The following example demonstrates how an invokable object may be attached to that same event type.
Attaching an event hook to a specific target
Using the attach_to()
method, an event hook can be attached to a specific target, and is only
invoked for that target.
Attaching a one-time event hook
The once()
method attaches an event hook that is automatically detached after it's been used.
Attaching event hooks using the events
config
When the package is bound to ICanBoogie by icanboogie/bind-event, event hooks may be
attached from the events
config. Have a look at the icanboogie/bind-event package for
further details.
Attaching event hooks to the finish chain
The finish chain is executed after the event chain was traversed without being stopped.
The following example demonstrates how an event hook may be attached to the finish chain of
the count
event to obtain the string "0123". If the third event hook was defined like the
others we would obtain "0312".
Breaking an event hook chain
The processing of an event hook chain can be broken by an event hook using the stop()
method:
Profiling events
The EventProfiler class is used to collect timing information about unused events and event hook calls. All time information is measured in floating microtime.
Helpers
get_events()
: Returns the current event collection. A new one is created if none exist.emit()
: Emit the specified event.
Continuous Integration
The project is continuously tested by GitHub actions.
Code of Conduct
This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.
Contributing
Please see CONTRIBUTING for details.
License
icanboogie/event is released under the BSD-3-Clause.
All versions of event with dependencies
icanboogie/accessor Version ^2.0|^3.0|^4.0
icanboogie/inflector Version ^1.3|^2.0