Download the PHP package webiny/event-manager without Composer
On this page you can find all versions of the php package webiny/event-manager. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download webiny/event-manager
More information about webiny/event-manager
Files in webiny/event-manager
Package event-manager
Short Description Webiny Event Manager Component
License MIT
Homepage http://www.webiny.com/
Informations about the package event-manager
Event Manager Component
EventManager
allows you to easily manage events throughout your application.
Install the component
The best way to install the component is using Composer.
For additional versions of the package, visit the Packagist page.
Usage
Accessing EventManager
can be done in 2 ways. The preferable way is using EventManagerTrait
, but you can also access it directly, using EventManager::getInstance()
. Let's see a simple example of subscribing to an event called some.event
with an instance of YourHandler
:
You're done! You have just subscribed to an event and the moment some.event
is fired, your handler will process it.
Event handlers
Now let's take a look at YourHandler
. An event handler can be any class. EventManager
will call handle(Event $event)
method on your handler object, by default. You can, however, specify a method you want EventManager
to call:
Besides using classes, you can also respond to an event using a callable:
Firing events
To fire a simple event use the following code:
You can also pass some data when firing an event, which will be passed to every event listener:
Any given data that is not an Event
object, will be converted to generic Event
object and your data will be accessible either by using array keys, or as object properties:
If you want to use custom Event
data types, refer to section Custom event classes
Firing events using a wildcard
You can also use wildcard to fire multiple events at once. The following code will fire all events starting with event.
and pass $data
to each one of them:
Execution priority
EventManager
allows you to specify an execution priority using priority()
method. Here's an example:
After firing an event, the event listeners will be ordered by priority in descending order. The higher the priority, the sooner the listener will be executed. In this example, the order of execution will be as follows: secondCustomHandler
, customHandler
, thirdCustomHandler
. Default priority is 101
, so thirdCustomHandler
is executed last.
Custom event classes
When firing events, you can also pass your own event classes, that extend generic Event
class. For example, you want to fire an event called cms.page_saved
and pass the Page
object. Of course, you could simply pass an array like ['page' => $pageObject]
, but for the sake of the example, let's pretend it's more complicated than that:
This is a simple example, but it shows the power of creating your own Event
classes and add as much functionality to your events as you need.
Event subscriber
Another cool feature of the EventManager
is the ability to subscribe to multiple events at once. You will need to create a subscriber class implementing EventSubscriberInterface
:
There are situations when you need to temporarily disable EventManager. For example, deleting a huge portion of files that are not directly related to the application (local cache files) does not require firing all of related events. In this case use the following methods:
Resources
To run unit tests, you need to use the following command:
$ cd path/to/Webiny/Component/EventManager/
$ composer.phar install
$ phpunit