The new Event-based system lends a lot of ideas from the rest
of the PHP world, gradually moving from Hook/Signal-Slot concepts
towards a new Event-based system. The event system is based on
the following components:
- Listeners: Which can be any PHP function/method/callable to hook
into a system
- EventManager: A singleton instance, setting up all listeners during
runtime at the very beginning of a request. The event manager
contains all listeners which are then called when an Event is triggered
- Event/EventInterface: A default event holds its name, parameters which can be read and modified. Interception can be done via "stopPropagation". This event is exposed to the listeners. Also,
the code which triggers an event can even create a custom event as long as it implements the EventInterface.
As there are so many "old" hooks, a full migration cannot be done
transparently, however, the Event system allows to set new events
on proper places where old hooks don't make sense anymore.
The ideas / advantages:
- Listeners can register once (later even on build-time) via a PHP
configuration file residing under 'Configuration/EventListeners.php',
it should be avoided that listeners can be set during runtime.
- A priority system allows to set when a listener should be falled.
- A base "Event" object which is the main place for listeners, no need
for interfaces, or method_exists() calls anymore.
- All completely cacheable. Event objects are only created if necessary.
- Due to the "early registration" (which is not bound to ext_localconf.php inclusion) it is possible to use Events even earlier
in the bootstrap.
- Low memory footprint, as the only object instantiated on each request is "EventManager".
Regarding "Naming things": Yes, pub/sub or "Observer" or "Signals"
can all be named as well. "Events" was chosen because this is
a) Currently very close to PSR-14, where I'm involved as well
b) Most other frameworks (Doctrine, Symfony, Composer, Laravel...)
use the same naming (JS as well), so it is common for people
understand what "Events" are.
Things that really need to be done:
- Tests
- Immutability EventManager could be built
- Extended ordering, also on first-hit caching functionality, which goes in hand with immutability
- Docs