/**
* Registers a legacy event listener, i.e. a method which accepts individual arguments instead of an AbstractEvent
* in its arguments. This provides backwards compatibility to Joomla! 3.x-style plugins.
*
* This method will register lambda functions (closures) which try to unwrap the arguments of the dispatched Event
* into old style method arguments and call your on<Something> method with them. The result will be passed back to
* the Event, as an element into an array argument called 'result'.
*
* @param string $methodName The method name to register
*
* @return void
*
* @since 4.0.0
*/
protected final function registerLegacyListener(string $methodName)
{
$this->getDispatcher()->addListener($methodName, function (AbstractEvent $event) use($methodName) {
// Get the event arguments
$arguments = $event->getArguments();
// Extract any old results; they must not be part of the method call.
$allResults = [];
if (isset($arguments['result'])) {
$allResults = $arguments['result'];
unset($arguments['result']);
}
// Convert to indexed array for unpacking.
$arguments = \array_values($arguments);
$result = $this->{$methodName}(...$arguments);
// Ignore null results
if ($result === null) {
return;
}
// Restore the old results and add the new result from our method call
$allResults[] = $result;
$event['result'] = $allResults;
});
}