Elgg’s event systems are based on registering callables to handle certain event names. As the codebase is still heavily procedural, most event handlers in the core and 3rd party plugins are global functions. Since I’m firmly in the static-code-is-evil camp, I’d much prefer handlers be dynamic method calls, but this introduces some complexity:
- Nice plugins allow their event handlers to be unregistered, but dynamic callbacks make this a bit awkward; any plugin that wanted to unregister another plugin’s handler would have to first get a reference to the other plugin’s object. Kind of messy.
- This requires proactively creating lots of objects—and loading lots of code—you don’t need in order to create the callbacks.
One way to work around the second issue would be to register callbacks on a proxy objects that lazy-load the real objects on the first method call. This adds complexity, could lead to typehint failures if the proxy gets accessed directly, and doesn’t solve the first issue at all. So I think the event system needs to be extended in a simple way:
We could allow callback strings of the form
service is the name of a pre-registered (and lazy-loading) service on Elgg’s DI container. Essentially the event trigger would call
This would allow trivially (un)registering these handlers, as well as efficiently lazy-loading the objects without the complexity of proxies.
I’d also suggest never actually binding to the service. This way you could replace a whole set of handlers by replacing the service on the DI container.