Download the PHP package joomla-x/event without Composer
On this page you can find all versions of the php package joomla-x/event. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download joomla-x/event
More information about joomla-x/event
Files in joomla-x/event
Package event
Short Description Joomla Event Package
License GPL-2.0+
Homepage https://github.com/joomla-framework/event
Informations about the package event
The Event Package 
The event package provides foundations to build event systems and an implementation supporting prioritized listeners.
Events
Example
An event has a name and can transport arguments.
Its propagation can be stopped
Event Listeners
An event listener can listen to one or more Events.
You can create two types of listeners : using a class or a closure (anonymous function).
The functions MUST take an EventInterface (or children) as unique parameter.
Classes
The listener listens to events having names matching its method names.
Closures
Closures can listen to any Event, it must be declared when adding them to the Dispatcher (see below).
The Dispatcher
The Dispatcher is the central point of the Event system, it manages the registration of Events, listeners and the triggering of Events.
Registering Object Listeners
Following the example above, you can register the ContentListener
to the dispatcher :
If the object contains other methods that should not be registered, you will need to explicitly list the events to be registered. For example:
Registering Closure Listeners
As you noticed, it is possible to specify a listener's priority for a given Event. It is also possible to do so with "object" Listeners.
Filtering Listeners
DEPRECATED
Listeners class can become quite complex, and may support public methods other than those required for event handling. The setListenerFilter
method can be used to set a regular expression that is used to check the method names of objects being added as listeners.
Registration with Priority
The default priority is the Priority::NORMAL
.
When you add an "object" Listener without specifying the event names, it is registered with a NORMAL priority to all events.
If some listeners have the same priority for a given event, they will be called in the order they were added to the Dispatcher.
Registering Events
You can register Events to the Dispatcher, if you need custom ones.
By default, an Event
object is created with no arguments, when triggering the Event.
Triggering Events
Once you registered your listeners (and eventually events to the Dispatcher), you can trigger the events.
The listeners will be called in a queue according to their priority for that Event.
If you registered an Event object having the onAfterSomething
name, then it will be passed to all listeners instead of the default one.
You can also pass a custom Event when triggering it
If you already added an Event with the onAfterSomething name using addEvent
, then the event passed to the triggerEvent
method will be chosen instead.
Stopping the Propagation
As said above, you can stop the Event propagation if you are listening to an Event supporting it, it is the case for the Event
class.
When stopping the Event propagation, the next listeners in the queue won't be called.
Observable classes
Observable classes depend on a Dispatcher, and they may implement the DispatcherAwareInterface
interface.
Example of a Model class :
Immutable Events
An immutable event cannot be modified after its instantiation:
- its arguments cannot be modified
- its propagation can't be stopped
It is useful when you don't want the listeners to manipulate it (they can only inspect it).
The Delegating Dispatcher
A dispatcher that delegates its method to an other Dispatcher. It is an easy way to achieve immutability for a Dispatcher.
This is useful when you want to make sure that 3rd party applications, won't register or remove listeners from the Dispatcher.
Installation via Composer
Add "joomla-x/event": "3.0.*@dev"
to the require block in your composer.json and then run composer install
.
Alternatively, you can simply run the following from the command line: