/**
* Registers legacy Listeners to the Dispatcher, emulating how plugins worked under Joomla! 3.x and below.
*
* By default, this method will look for all public methods whose name starts with "on". It will register
* lambda functions (closures) which try to unwrap the arguments of the dispatched Event into method call
* arguments and call your on<Something> method. The result will be passed back to the Event into its 'result'
* argument.
*
* This method additionally supports Joomla\Event\SubscriberInterface and plugins implementing this will be
* registered to the dispatcher as a subscriber.
*
* @return void
*
* @since 4.0.0
*/
public function registerListeners()
{
// Plugins which are SubscriberInterface implementations are handled without legacy layer support
if ($this instanceof SubscriberInterface) {
$this->getDispatcher()->addSubscriber($this);
return;
}
$reflectedObject = new \ReflectionObject($this);
$methods = $reflectedObject->getMethods(\ReflectionMethod::IS_PUBLIC);
/** @var \ReflectionMethod $method */
foreach ($methods as $method) {
if (substr($method->name, 0, 2) !== 'on') {
continue;
}
// Save time if I'm not to detect legacy listeners
if (!$this->allowLegacyListeners) {
$this->registerListener($method->name);
continue;
}
/** @var \ReflectionParameter[] $parameters */
$parameters = $method->getParameters();
// If the parameter count is not 1 it is by definition a legacy listener
if (\count($parameters) !== 1) {
$this->registerLegacyListener($method->name);
continue;
}
/** @var \ReflectionParameter $param */
$param = array_shift($parameters);
$paramName = $param->getName();
// No type hint / type hint class not an event or parameter name is not "event"? It's a legacy listener.
if ($paramName !== 'event' || !$this->parameterImplementsEventInterface($param)) {
$this->registerLegacyListener($method->name);
continue;
}
// Everything checks out, this is a proper listener.
$this->registerListener($method->name);
}
}