From 640428c415fe0d166b9cff1a086796467035d84c Mon Sep 17 00:00:00 2001 From: "Dylan K. Taylor" Date: Sat, 23 May 2020 10:02:09 +0100 Subject: [PATCH] Convert WorldProviderManager to singleton --- src/Server.php | 6 +-- src/world/WorldManager.php | 8 ++-- src/world/format/io/WorldProviderManager.php | 37 ++++++++++--------- tests/phpstan/configs/phpstan-bugs.neon | 2 +- .../phpstan/configs/phpunit-wiring-tests.neon | 2 +- .../format/io/LevelProviderManagerTest.php | 15 ++++++-- tools/convert-world.php | 5 +-- 7 files changed, 42 insertions(+), 33 deletions(-) diff --git a/src/Server.php b/src/Server.php index e1fad2d2d..4744a2c98 100644 --- a/src/Server.php +++ b/src/Server.php @@ -1007,12 +1007,12 @@ class Server{ $this->pluginManager->registerInterface(new PharPluginLoader($this->autoloader)); $this->pluginManager->registerInterface(new ScriptPluginLoader()); - WorldProviderManager::init(); + $providerManager = WorldProviderManager::getInstance(); if( - ($format = WorldProviderManager::getProviderByName($formatName = (string) $this->getProperty("level-settings.default-format"))) !== null and + ($format = $providerManager->getProviderByName($formatName = (string) $this->getProperty("level-settings.default-format"))) !== null and is_a($format, WritableWorldProvider::class, true) ){ - WorldProviderManager::setDefault($format); + $providerManager->setDefault($format); }elseif($formatName !== ""){ $this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName])); } diff --git a/src/world/WorldManager.php b/src/world/WorldManager.php index 5aa991ceb..d641598fb 100644 --- a/src/world/WorldManager.php +++ b/src/world/WorldManager.php @@ -179,7 +179,7 @@ class WorldManager{ $path = $this->getWorldPath($name); - $providers = WorldProviderManager::getMatchingProviders($path); + $providers = WorldProviderManager::getInstance()->getMatchingProviders($path); if(count($providers) !== 1){ $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [ $name, @@ -216,7 +216,7 @@ class WorldManager{ } $this->server->getLogger()->notice("Upgrading world \"$name\" to new format. This may take a while."); - $converter = new FormatConverter($provider, WorldProviderManager::getDefault(), $this->server->getDataPath() . "world_conversion_backups", $this->server->getLogger()); + $converter = new FormatConverter($provider, WorldProviderManager::getInstance()->getDefault(), $this->server->getDataPath() . "world_conversion_backups", $this->server->getLogger()); $provider = $converter->execute(); $this->server->getLogger()->notice("Upgraded world \"$name\" to new format successfully. Backed up pre-conversion world at " . $converter->getBackupPath()); @@ -251,7 +251,7 @@ class WorldManager{ Utils::testValidInstance($generator, Generator::class); - $providerClass = WorldProviderManager::getDefault(); + $providerClass = WorldProviderManager::getInstance()->getDefault(); $path = $this->getWorldPath($name); /** @var WritableWorldProvider $providerClass */ @@ -307,7 +307,7 @@ class WorldManager{ } $path = $this->getWorldPath($name); if(!($this->getWorldByName($name) instanceof World)){ - return count(WorldProviderManager::getMatchingProviders($path)) > 0; + return count(WorldProviderManager::getInstance()->getMatchingProviders($path)) > 0; } return true; diff --git a/src/world/format/io/WorldProviderManager.php b/src/world/format/io/WorldProviderManager.php index 37e489709..123293599 100644 --- a/src/world/format/io/WorldProviderManager.php +++ b/src/world/format/io/WorldProviderManager.php @@ -23,6 +23,7 @@ declare(strict_types=1); namespace pocketmine\world\format\io; +use pocketmine\utils\SingletonTrait; use pocketmine\utils\Utils; use pocketmine\world\format\io\leveldb\LevelDB; use pocketmine\world\format\io\region\Anvil; @@ -31,20 +32,22 @@ use pocketmine\world\format\io\region\PMAnvil; use function strtolower; use function trim; -abstract class WorldProviderManager{ +final class WorldProviderManager{ + use SingletonTrait; + /** * @var string[] * @phpstan-var array> */ - protected static $providers = []; + protected $providers = []; /** * @var string * @phpstan-var class-string */ - private static $default = LevelDB::class; + private $default = LevelDB::class; - public static function init() : void{ + public function __construct(){ self::addProvider(Anvil::class, "anvil"); self::addProvider(McRegion::class, "mcregion"); self::addProvider(PMAnvil::class, "pmanvil"); @@ -56,8 +59,8 @@ abstract class WorldProviderManager{ * * @phpstan-return class-string */ - public static function getDefault() : string{ - return self::$default; + public function getDefault() : string{ + return $this->default; } /** @@ -68,25 +71,25 @@ abstract class WorldProviderManager{ * * @throws \InvalidArgumentException */ - public static function setDefault(string $class) : void{ + public function setDefault(string $class) : void{ Utils::testValidInstance($class, WritableWorldProvider::class); - self::$default = $class; + $this->default = $class; } /** * @phpstan-param class-string $class */ - public static function addProvider(string $class, string $name, bool $overwrite = false) : void{ + public function addProvider(string $class, string $name, bool $overwrite = false) : void{ Utils::testValidInstance($class, WorldProvider::class); $name = strtolower($name); - if(!$overwrite and isset(self::$providers[$name])){ + if(!$overwrite and isset($this->providers[$name])){ throw new \InvalidArgumentException("Alias \"$name\" is already assigned"); } /** @var WorldProvider $class */ - self::$providers[$name] = $class; + $this->providers[$name] = $class; } /** @@ -95,9 +98,9 @@ abstract class WorldProviderManager{ * @return string[] * @phpstan-return array> */ - public static function getMatchingProviders(string $path) : array{ + public function getMatchingProviders(string $path) : array{ $result = []; - foreach(self::$providers as $alias => $provider){ + foreach($this->providers as $alias => $provider){ if($provider::isValid($path)){ $result[$alias] = $provider; } @@ -109,8 +112,8 @@ abstract class WorldProviderManager{ * @return string[] * @phpstan-return array> */ - public static function getAvailableProviders() : array{ - return self::$providers; + public function getAvailableProviders() : array{ + return $this->providers; } /** @@ -118,7 +121,7 @@ abstract class WorldProviderManager{ * * @phpstan-return class-string|null */ - public static function getProviderByName(string $name) : ?string{ - return self::$providers[trim(strtolower($name))] ?? null; + public function getProviderByName(string $name) : ?string{ + return $this->providers[trim(strtolower($name))] ?? null; } } diff --git a/tests/phpstan/configs/phpstan-bugs.neon b/tests/phpstan/configs/phpstan-bugs.neon index 7d4858d65..e6712299b 100644 --- a/tests/phpstan/configs/phpstan-bugs.neon +++ b/tests/phpstan/configs/phpstan-bugs.neon @@ -1,7 +1,7 @@ parameters: ignoreErrors: - - message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\, class\\-string\\ given\\.$#" + message: "#^Parameter \\#1 \\$class of method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\, class\\-string\\ given\\.$#" count: 1 path: ../../../src/Server.php diff --git a/tests/phpstan/configs/phpunit-wiring-tests.neon b/tests/phpstan/configs/phpunit-wiring-tests.neon index d939ca84e..40cbef80d 100644 --- a/tests/phpstan/configs/phpunit-wiring-tests.neon +++ b/tests/phpstan/configs/phpunit-wiring-tests.neon @@ -1,7 +1,7 @@ parameters: ignoreErrors: - - message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:addProvider\\(\\) expects class\\-string\\, string given\\.$#" + message: "#^Parameter \\#1 \\$class of method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:addProvider\\(\\) expects class\\-string\\, string given\\.$#" count: 2 path: ../../phpunit/world/format/io/LevelProviderManagerTest.php diff --git a/tests/phpunit/world/format/io/LevelProviderManagerTest.php b/tests/phpunit/world/format/io/LevelProviderManagerTest.php index 738a16bc3..8cd9bb065 100644 --- a/tests/phpunit/world/format/io/LevelProviderManagerTest.php +++ b/tests/phpunit/world/format/io/LevelProviderManagerTest.php @@ -27,27 +27,34 @@ use PHPUnit\Framework\TestCase; class LevelProviderManagerTest extends TestCase{ + /** @var WorldProviderManager */ + private $providerManager; + + protected function setUp() : void{ + $this->providerManager = new WorldProviderManager(); + } + public function testAddNonClassProvider() : void{ $this->expectException(\InvalidArgumentException::class); - WorldProviderManager::addProvider("lol", "nope"); + $this->providerManager->addProvider("lol", "nope"); } public function testAddAbstractClassProvider() : void{ $this->expectException(\InvalidArgumentException::class); - WorldProviderManager::addProvider(AbstractWorldProvider::class, "abstract"); + $this->providerManager->addProvider(AbstractWorldProvider::class, "abstract"); } public function testAddInterfaceProvider() : void{ $this->expectException(\InvalidArgumentException::class); - WorldProviderManager::addProvider(InterfaceWorldProvider::class, "interface"); + $this->providerManager->addProvider(InterfaceWorldProvider::class, "interface"); } public function testAddWrongClassProvider() : void{ $this->expectException(\InvalidArgumentException::class); - WorldProviderManager::addProvider(LevelProviderManagerTest::class, "bad_class"); + $this->providerManager->addProvider(LevelProviderManagerTest::class, "bad_class"); } } diff --git a/tools/convert-world.php b/tools/convert-world.php index 8caab2692..f4e376853 100644 --- a/tools/convert-world.php +++ b/tools/convert-world.php @@ -29,10 +29,9 @@ use pocketmine\world\generator\GeneratorManager; require_once dirname(__DIR__) . '/vendor/autoload.php'; -WorldProviderManager::init(); GeneratorManager::registerDefaultGenerators(); -$writableFormats = array_filter(WorldProviderManager::getAvailableProviders(), function(string $class){ +$writableFormats = array_filter(WorldProviderManager::getInstance()->getAvailableProviders(), function(string $class){ return is_a($class, WritableWorldProvider::class, true); }); $requiredOpts = [ @@ -63,7 +62,7 @@ if((!@mkdir($backupPath, 0777, true) and !is_dir($backupPath)) or !is_writable($ die("Backup file path " . $backupPath . " is not writable (permission error or doesn't exist), aborting"); } -$oldProviderClasses = WorldProviderManager::getMatchingProviders($inputPath); +$oldProviderClasses = WorldProviderManager::getInstance()->getMatchingProviders($inputPath); if(count($oldProviderClasses) === 0){ die("Unknown input world format"); }