Extract a HandlerListManager unit from HandlerList

This commit is contained in:
Dylan K. Taylor 2019-06-30 14:49:09 +01:00
parent 68bff6cf69
commit 5e5f43242e
5 changed files with 110 additions and 72 deletions

View File

@ -34,7 +34,7 @@ use pocketmine\command\ConsoleCommandSender;
use pocketmine\command\PluginIdentifiableCommand;
use pocketmine\command\SimpleCommandMap;
use pocketmine\entity\EntityFactory;
use pocketmine\event\HandlerList;
use pocketmine\event\HandlerListManager;
use pocketmine\event\player\PlayerDataSaveEvent;
use pocketmine\event\server\CommandEvent;
use pocketmine\event\server\DataPacketBroadcastEvent;
@ -1600,7 +1600,7 @@ class Server{
}
$this->getLogger()->debug("Removing event handlers");
HandlerList::unregisterAll();
HandlerListManager::global()->unregisterAll();
if($this->asyncPool instanceof AsyncPool){
$this->getLogger()->debug("Shutting down async task worker pool");

View File

@ -55,7 +55,7 @@ abstract class Event{
throw new \RuntimeException("Recursive event call detected (reached max depth of " . self::MAX_EVENT_CALL_DEPTH . " calls)");
}
$handlerList = HandlerList::getHandlerListFor(get_class($this));
$handlerList = HandlerListManager::global()->getListFor(get_class($this));
assert($handlerList !== null, "Called event should have a valid HandlerList");
++self::$eventCallDepth;

View File

@ -24,77 +24,11 @@ declare(strict_types=1);
namespace pocketmine\event;
use pocketmine\plugin\Plugin;
use pocketmine\utils\Utils;
use function array_fill_keys;
use function in_array;
use function spl_object_id;
class HandlerList{
/**
* @var HandlerList[] classname => HandlerList
*/
private static $allLists = [];
/**
* Unregisters all the listeners
* If a Plugin or Listener is passed, all the listeners with that object will be removed
*
* @param Plugin|Listener|null $object
*/
public static function unregisterAll($object = null) : void{
if($object instanceof Listener or $object instanceof Plugin){
foreach(self::$allLists as $h){
$h->unregister($object);
}
}else{
foreach(self::$allLists as $h){
foreach($h->handlerSlots as $key => $list){
$h->handlerSlots[$key] = [];
}
}
}
}
/**
* Returns the HandlerList for listeners that explicitly handle this event.
*
* Calling this method also lazily initializes the $classMap inheritance tree of handler lists.
*
* @param string $event
*
* @return null|HandlerList
* @throws \ReflectionException
*/
public static function getHandlerListFor(string $event) : ?HandlerList{
if(isset(self::$allLists[$event])){
return self::$allLists[$event];
}
$class = new \ReflectionClass($event);
$tags = Utils::parseDocComment((string) $class->getDocComment());
if($class->isAbstract() && !isset($tags["allowHandle"])){
return null;
}
$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 = self::getHandlerListFor($super->getName());
}
return new HandlerList($event, $parentList);
}
/**
* @return HandlerList[]
*/
public static function getHandlerLists() : array{
return self::$allLists;
}
/** @var string */
private $class;
@ -107,7 +41,6 @@ class HandlerList{
$this->class = $class;
$this->handlerSlots = array_fill_keys(EventPriority::ALL, []);
$this->parentList = $parentList;
self::$allLists[$this->class] = $this;
}
/**
@ -155,6 +88,10 @@ class HandlerList{
}
}
public function clear() : void{
$this->handlerSlots = array_fill_keys(EventPriority::ALL, []);
}
/**
* @param int $priority
*

View File

@ -0,0 +1,100 @@
<?php
/*
*
* ____ _ _ __ __ _ __ __ ____
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* @author PocketMine Team
* @link http://www.pocketmine.net/
*
*
*/
declare(strict_types=1);
namespace pocketmine\event;
use pocketmine\plugin\Plugin;
use pocketmine\utils\Utils;
class HandlerListManager{
/** @var HandlerListManager */
private static $globalInstance = null;
public static function global() : self{
return self::$globalInstance ?? (self::$globalInstance = new self);
}
/**
* @var HandlerList[] classname => HandlerList
*/
private $allLists = [];
/**
* Unregisters all the listeners
* If a Plugin or Listener is passed, all the listeners with that object will be removed
*
* @param Plugin|Listener|null $object
*/
public function unregisterAll($object = null) : void{
if($object instanceof Listener or $object instanceof Plugin){
foreach($this->allLists as $h){
$h->unregister($object);
}
}else{
foreach($this->allLists as $h){
$h->clear();
}
}
}
/**
* Returns the HandlerList for listeners that explicitly handle this event.
*
* Calling this method also lazily initializes the $classMap inheritance tree of handler lists.
*
* @param string $event
*
* @return null|HandlerList
* @throws \ReflectionException
*/
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;
}
$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);
}
/**
* @return HandlerList[]
*/
public function getAll() : array{
return $this->allLists;
}
}

View File

@ -26,6 +26,7 @@ namespace pocketmine\plugin;
use pocketmine\event\Event;
use pocketmine\event\EventPriority;
use pocketmine\event\HandlerList;
use pocketmine\event\HandlerListManager;
use pocketmine\event\Listener;
use pocketmine\event\plugin\PluginDisableEvent;
use pocketmine\event\plugin\PluginEnableEvent;
@ -465,7 +466,7 @@ class PluginManager{
$plugin->onEnableStateChange(false);
$plugin->getScheduler()->shutdown();
HandlerList::unregisterAll($plugin);
HandlerListManager::global()->unregisterAll($plugin);
$permManager = PermissionManager::getInstance();
foreach($plugin->getDescription()->getPermissions() as $perm){
$permManager->removePermission($perm);
@ -594,7 +595,7 @@ class PluginManager{
* @return HandlerList
*/
private function getEventListeners(string $event) : HandlerList{
$list = HandlerList::getHandlerListFor($event);
$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)");
}