|Classes of Michael Cummings||>||PHP Event Mediator||>||docs/UnderstandingEventMediator.md||>||Download|
Before getting into any of the details on using Event-Mediator itself it's probably useful to go over some of the questions you might have about the need for it.
Why use a mediator?
Many of you might be wondering why you should care about using a mediator when you could just as easily use something like an observer. While it is true that observer works well when you have a small number of subscribers and objects being observed it starts to break down quickly when you have larger numbers of either or both. Additionally it makes the objects being observed do an addition task which normally requires several methods that really have nothing to do with it primary purpose. So the observer pattern tends to cause tighter coupling between classes than SOLID principals seem to allow since both the observer and objects have to have knowledge about each other to interact.
If instead you use a mediator all the subscribers (observers) need to do is be registered with it. The subscribers themselves don't even need to know how to register or that they are a subscriber really they only need to perform a function when they receive the event. The objects only need to know how to trigger the event but don't need to know anything about the subscribers or what they do with the event. The coupling between the subscribers and objects is very loose now and even the event mediator needs very little knowledge of the subscribers and zero knowledge of the objects triggering the events.
Listeners and Subscribers
In the above I used subscribers to refer to anything that wanted to receive the
event but another term used is listeners. In Event-Mediator both are possible
and they have some differences in how they and the mediator interact during the
add(attach) and delete(detach) process but the actual event related parts is the
same. To make it clearer what the difference is between them subscribers have to
Service Listeners and Subscribers
The only noticeable difference between a service version and base listeners or subscribers is the service versions are lazy loading meaning that they only get initialized if the event is triggered and not at the time they are added to the mediator. This can make for a more responsive application when some events have a chance of not being triggered every time the application runs. Better responsiveness will be most noticeable when the listener or subscriber is a complex class which has a slow initialization process (large DB or filesystem data records, etc) or you have a large number of listeners or subscribers for one or more events which are only sometimes triggered.
So if you look at the
you'll notice it only has a couple of methods (
To start out I'm going to list the steps needed to make a working logging event system using code examples from my Yapeal-ng project which is a re-write of another long time project with many improvements including switching to events for logging and the main processing loop among others. I'll show how the logging event stuff is done as I believe it's more general and probably easier to understand so you might find you can adept it to your own projects with few changes.
1 Get new instance of Mediator. The
extends from the
class and is initialized in a common initialization class in the
2 Next get the event log class and register it with the event system.
Yapeal-ng adds an
around an initialized instance of Monolog
but should work with any
You'll find the initialization and registering of the class for the log events in the
3 Start sending log event as needed. For an example of this we'll look at Yapeal::autoMagic() which kicks off the main processing loop. After a couple setup lines the method sends Yapeal-ng's first log message on line 83.
4 More great and wonderful description here is on the todo list but the update of Event-Mediator to version 2.0.*-dev with many changes and the changes in Yapeal-ng as well will probably require a rewrite here so I've put a hold on this document until after everything is updated.;)