Convert WorldProviderManager to singleton

This commit is contained in:
Dylan K. Taylor 2020-05-23 10:02:09 +01:00
parent e2232dd8d4
commit 640428c415
7 changed files with 42 additions and 33 deletions

View File

@ -1007,12 +1007,12 @@ class Server{
$this->pluginManager->registerInterface(new PharPluginLoader($this->autoloader)); $this->pluginManager->registerInterface(new PharPluginLoader($this->autoloader));
$this->pluginManager->registerInterface(new ScriptPluginLoader()); $this->pluginManager->registerInterface(new ScriptPluginLoader());
WorldProviderManager::init(); $providerManager = WorldProviderManager::getInstance();
if( 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) is_a($format, WritableWorldProvider::class, true)
){ ){
WorldProviderManager::setDefault($format); $providerManager->setDefault($format);
}elseif($formatName !== ""){ }elseif($formatName !== ""){
$this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName])); $this->logger->warning($this->language->translateString("pocketmine.level.badDefaultFormat", [$formatName]));
} }

View File

@ -179,7 +179,7 @@ class WorldManager{
$path = $this->getWorldPath($name); $path = $this->getWorldPath($name);
$providers = WorldProviderManager::getMatchingProviders($path); $providers = WorldProviderManager::getInstance()->getMatchingProviders($path);
if(count($providers) !== 1){ if(count($providers) !== 1){
$this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [ $this->server->getLogger()->error($this->server->getLanguage()->translateString("pocketmine.level.loadError", [
$name, $name,
@ -216,7 +216,7 @@ class WorldManager{
} }
$this->server->getLogger()->notice("Upgrading world \"$name\" to new format. This may take a while."); $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(); $provider = $converter->execute();
$this->server->getLogger()->notice("Upgraded world \"$name\" to new format successfully. Backed up pre-conversion world at " . $converter->getBackupPath()); $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); Utils::testValidInstance($generator, Generator::class);
$providerClass = WorldProviderManager::getDefault(); $providerClass = WorldProviderManager::getInstance()->getDefault();
$path = $this->getWorldPath($name); $path = $this->getWorldPath($name);
/** @var WritableWorldProvider $providerClass */ /** @var WritableWorldProvider $providerClass */
@ -307,7 +307,7 @@ class WorldManager{
} }
$path = $this->getWorldPath($name); $path = $this->getWorldPath($name);
if(!($this->getWorldByName($name) instanceof World)){ if(!($this->getWorldByName($name) instanceof World)){
return count(WorldProviderManager::getMatchingProviders($path)) > 0; return count(WorldProviderManager::getInstance()->getMatchingProviders($path)) > 0;
} }
return true; return true;

View File

@ -23,6 +23,7 @@ declare(strict_types=1);
namespace pocketmine\world\format\io; namespace pocketmine\world\format\io;
use pocketmine\utils\SingletonTrait;
use pocketmine\utils\Utils; use pocketmine\utils\Utils;
use pocketmine\world\format\io\leveldb\LevelDB; use pocketmine\world\format\io\leveldb\LevelDB;
use pocketmine\world\format\io\region\Anvil; use pocketmine\world\format\io\region\Anvil;
@ -31,20 +32,22 @@ use pocketmine\world\format\io\region\PMAnvil;
use function strtolower; use function strtolower;
use function trim; use function trim;
abstract class WorldProviderManager{ final class WorldProviderManager{
use SingletonTrait;
/** /**
* @var string[] * @var string[]
* @phpstan-var array<string, class-string<WorldProvider>> * @phpstan-var array<string, class-string<WorldProvider>>
*/ */
protected static $providers = []; protected $providers = [];
/** /**
* @var string * @var string
* @phpstan-var class-string<WritableWorldProvider> * @phpstan-var class-string<WritableWorldProvider>
*/ */
private static $default = LevelDB::class; private $default = LevelDB::class;
public static function init() : void{ public function __construct(){
self::addProvider(Anvil::class, "anvil"); self::addProvider(Anvil::class, "anvil");
self::addProvider(McRegion::class, "mcregion"); self::addProvider(McRegion::class, "mcregion");
self::addProvider(PMAnvil::class, "pmanvil"); self::addProvider(PMAnvil::class, "pmanvil");
@ -56,8 +59,8 @@ abstract class WorldProviderManager{
* *
* @phpstan-return class-string<WritableWorldProvider> * @phpstan-return class-string<WritableWorldProvider>
*/ */
public static function getDefault() : string{ public function getDefault() : string{
return self::$default; return $this->default;
} }
/** /**
@ -68,25 +71,25 @@ abstract class WorldProviderManager{
* *
* @throws \InvalidArgumentException * @throws \InvalidArgumentException
*/ */
public static function setDefault(string $class) : void{ public function setDefault(string $class) : void{
Utils::testValidInstance($class, WritableWorldProvider::class); Utils::testValidInstance($class, WritableWorldProvider::class);
self::$default = $class; $this->default = $class;
} }
/** /**
* @phpstan-param class-string<WorldProvider> $class * @phpstan-param class-string<WorldProvider> $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); Utils::testValidInstance($class, WorldProvider::class);
$name = strtolower($name); $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"); throw new \InvalidArgumentException("Alias \"$name\" is already assigned");
} }
/** @var WorldProvider $class */ /** @var WorldProvider $class */
self::$providers[$name] = $class; $this->providers[$name] = $class;
} }
/** /**
@ -95,9 +98,9 @@ abstract class WorldProviderManager{
* @return string[] * @return string[]
* @phpstan-return array<string, class-string<WorldProvider>> * @phpstan-return array<string, class-string<WorldProvider>>
*/ */
public static function getMatchingProviders(string $path) : array{ public function getMatchingProviders(string $path) : array{
$result = []; $result = [];
foreach(self::$providers as $alias => $provider){ foreach($this->providers as $alias => $provider){
if($provider::isValid($path)){ if($provider::isValid($path)){
$result[$alias] = $provider; $result[$alias] = $provider;
} }
@ -109,8 +112,8 @@ abstract class WorldProviderManager{
* @return string[] * @return string[]
* @phpstan-return array<string, class-string<WorldProvider>> * @phpstan-return array<string, class-string<WorldProvider>>
*/ */
public static function getAvailableProviders() : array{ public function getAvailableProviders() : array{
return self::$providers; return $this->providers;
} }
/** /**
@ -118,7 +121,7 @@ abstract class WorldProviderManager{
* *
* @phpstan-return class-string<WorldProvider>|null * @phpstan-return class-string<WorldProvider>|null
*/ */
public static function getProviderByName(string $name) : ?string{ public function getProviderByName(string $name) : ?string{
return self::$providers[trim(strtolower($name))] ?? null; return $this->providers[trim(strtolower($name))] ?? null;
} }
} }

View File

@ -1,7 +1,7 @@
parameters: parameters:
ignoreErrors: ignoreErrors:
- -
message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\>, class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\> given\\.$#" message: "#^Parameter \\#1 \\$class of method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:setDefault\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WritableWorldProvider\\>, class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\> given\\.$#"
count: 1 count: 1
path: ../../../src/Server.php path: ../../../src/Server.php

View File

@ -1,7 +1,7 @@
parameters: parameters:
ignoreErrors: ignoreErrors:
- -
message: "#^Parameter \\#1 \\$class of static method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:addProvider\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\>, string given\\.$#" message: "#^Parameter \\#1 \\$class of method pocketmine\\\\world\\\\format\\\\io\\\\WorldProviderManager\\:\\:addProvider\\(\\) expects class\\-string\\<pocketmine\\\\world\\\\format\\\\io\\\\WorldProvider\\>, string given\\.$#"
count: 2 count: 2
path: ../../phpunit/world/format/io/LevelProviderManagerTest.php path: ../../phpunit/world/format/io/LevelProviderManagerTest.php

View File

@ -27,27 +27,34 @@ use PHPUnit\Framework\TestCase;
class LevelProviderManagerTest extends TestCase{ class LevelProviderManagerTest extends TestCase{
/** @var WorldProviderManager */
private $providerManager;
protected function setUp() : void{
$this->providerManager = new WorldProviderManager();
}
public function testAddNonClassProvider() : void{ public function testAddNonClassProvider() : void{
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
WorldProviderManager::addProvider("lol", "nope"); $this->providerManager->addProvider("lol", "nope");
} }
public function testAddAbstractClassProvider() : void{ public function testAddAbstractClassProvider() : void{
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
WorldProviderManager::addProvider(AbstractWorldProvider::class, "abstract"); $this->providerManager->addProvider(AbstractWorldProvider::class, "abstract");
} }
public function testAddInterfaceProvider() : void{ public function testAddInterfaceProvider() : void{
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
WorldProviderManager::addProvider(InterfaceWorldProvider::class, "interface"); $this->providerManager->addProvider(InterfaceWorldProvider::class, "interface");
} }
public function testAddWrongClassProvider() : void{ public function testAddWrongClassProvider() : void{
$this->expectException(\InvalidArgumentException::class); $this->expectException(\InvalidArgumentException::class);
WorldProviderManager::addProvider(LevelProviderManagerTest::class, "bad_class"); $this->providerManager->addProvider(LevelProviderManagerTest::class, "bad_class");
} }
} }

View File

@ -29,10 +29,9 @@ use pocketmine\world\generator\GeneratorManager;
require_once dirname(__DIR__) . '/vendor/autoload.php'; require_once dirname(__DIR__) . '/vendor/autoload.php';
WorldProviderManager::init();
GeneratorManager::registerDefaultGenerators(); 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); return is_a($class, WritableWorldProvider::class, true);
}); });
$requiredOpts = [ $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"); 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){ if(count($oldProviderClasses) === 0){
die("Unknown input world format"); die("Unknown input world format");
} }