make logic for fetching handler lists more consistent

This commit is contained in:
Dylan K. Taylor
2019-08-23 17:16:07 +01:00
parent 7e5193355e
commit e5b02ee5e0
10 changed files with 272 additions and 30 deletions

View File

@ -56,7 +56,6 @@ abstract class Event{
}
$handlerList = HandlerListManager::global()->getListFor(get_class($this));
assert($handlerList !== null, "Called event should have a valid HandlerList");
++self::$eventCallDepth;
try{

View File

@ -58,6 +58,18 @@ class HandlerListManager{
}
}
private static function isValidClass(\ReflectionClass $class) : bool{
$tags = Utils::parseDocComment((string) $class->getDocComment());
return !$class->isAbstract() || isset($tags["allowHandle"]);
}
private static function resolveNearestHandleableParent(\ReflectionClass $class) : ?\ReflectionClass{
for($parent = $class->getParentClass(); $parent !== false && !self::isValidClass($parent); $parent = $parent->getParentClass()){
//NOOP
}
return $parent ?: null;
}
/**
* Returns the HandlerList for listeners that explicitly handle this event.
*
@ -65,30 +77,22 @@ class HandlerListManager{
*
* @param string $event
*
* @return null|HandlerList
* @return HandlerList
* @throws \ReflectionException
* @throws \InvalidArgumentException
*/
public function getListFor(string $event) : ?HandlerList{
public function getListFor(string $event) : HandlerList{
if(isset($this->allLists[$event])){
return $this->allLists[$event];
}
$class = new \ReflectionClass($event);
$tags = Utils::parseDocComment((string) $class->getDocComment());
if($class->isAbstract() && !isset($tags["allowHandle"])){
return null;
if(!self::isValidClass($class)){
throw new \InvalidArgumentException("Event must be non-abstract or have the @allowHandle annotation");
}
$super = $class;
$parentList = null;
while($parentList === null && ($super = $super->getParentClass()) !== false){
// skip $noHandle events in the inheritance tree to go to the nearest ancestor
// while loop to allow skipping $noHandle events in the inheritance tree
$parentList = $this->getListFor($super->getName());
}
return $this->allLists[$event] = new HandlerList($event, $parentList);
$parent = self::resolveNearestHandleableParent($class);
return $this->allLists[$event] = new HandlerList($event, $parent !== null ? $this->getListFor($parent->getName()) : null);
}
/**

View File

@ -568,19 +568,6 @@ class PluginManager{
$timings = new TimingsHandler("Plugin: " . $plugin->getDescription()->getFullName() . " Event: " . $handlerName . "(" . (new \ReflectionClass($event))->getShortName() . ")");
$this->getEventListeners($event)->register(new RegisteredListener($handler, $priority, $plugin, $handleCancelled, $timings));
}
/**
* @param string $event
*
* @return HandlerList
*/
private function getEventListeners(string $event) : HandlerList{
$list = HandlerListManager::global()->getListFor($event);
if($list === null){
throw new PluginException("Abstract events not declaring @allowHandle cannot be handled (tried to register listener for $event)");
}
return $list;
HandlerListManager::global()->getListFor($event)->register(new RegisteredListener($handler, $priority, $plugin, $handleCancelled, $timings));
}
}