Download the PHP package mattferris/events without Composer
On this page you can find all versions of the php package mattferris/events. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package events
events
This is an events library with an included event logger. It's built with Udi Dahan's Domain Events pattern in mind.
Event Handling
Implementation starts with creating a DomainEvents
class in your domain that
extends MattFerris\Events\AbstractDomainEvents
.
Then you can create individual event classes either by extending
MattFerris\Events\Event
or implementing MattFerris\Events\EventInterface
.
Event
is an empty class so your event class will still need to provide it's
own logic.
To raise/dispatch an event from within your domain entities, call
DomainEvents::dispatch()
.
At the moment, there is no dispatcher configured so the event won't be handled by anything. Configuring a dispatcher can be done like so:
And now you can add events listeners to the dispatcher. Listeners can be any
callable
, and can listen for a specific event or pattern matched event. To
match a specific event, use the fully qualified class name.
It's also possible to listen for events based on a prefix or suffix.
Finally, you can listen for all events using an asterisk.
Event Names
For flexiblity, listeners can also be assigned to listen for arbitrary event names. An event name can be passed to the dispatch method when the event is dispatched.
Listener Priority
By default, all listeners are given the same priority, and are called based on
the order they were added. Listeners can also be assigned a priority to ensure
they are called sooner or later then other listeners. The priority is an integer
that is passed when adding the listener. 0
is the highest priority.
Priorities can also be assigned using the priority constants:
PRIORITY_HIGH
= 0PRIORITY_NORMAL
= 50PRIORITY_LOW
= 100
Event Providers
Events can be added using providers. Simply create a class that implements
MattFerris\Provider\ProviderInterface
and create a method called provides()
.
When passed to the register()
method on the dispatcher, the provides()
method will be passed an instance of the dispatcher, and you can then add
listeners.
Event Logging
Event logging is accomplished by using MattFerris\Events\Logger
. The
constructor takes an instance of Dispatcher
and a callable that is passed the
resulting log message and can then write it to a log file, stderr, etc.
That's it! All dispatched events will be logged to PHP's error log. By default,
log messages are just the name of the event prefixed with event:
. For example:
You can change the prefix using setPrefix()
.
You can use logging helpers to customize the messages for certain events. Create
a class in your domain called DomainEventLoggerHelpers
and have it extend
MattFerris\Events\AbstractDomainEventLoggerHelpers
. Then simply create static
methods to be called for domain events, where each method returns the string to
be logged. These methods should start with on
followed by the event name e.g.
onSomeEvent
.
To register the helpers with the logger:
Now, when SomeEvent
is dispatched, the above helper will be called and the
returned string will be logged.
In my opinion, it makes sense to have these logging helpers defined in
MyDomain\DomainEventLoggerHelpers
as the helpers are directly related to the
events in the domain. When domain events are updated, the logging helpers can be
updated and committed all together.