From 127b57048c920c5e99addeb117f5d4e9b9c23133 Mon Sep 17 00:00:00 2001 From: Dylan T Date: Fri, 23 Apr 2021 19:34:46 +0100 Subject: [PATCH] Allow plugins to use PSR-4 namespace mapping (#4188) * Allow plugins to use PSR-4 namespace mapping this is a reduced implementation which serves the 99% use case without being horribly breakable. Plugins may now specify a `src-namespace-prefix`, which should be set to the namespace of the classes in `src`. If the old system is used, `src-namespace-prefix` can be omitted, or set to an empty string. Examples: - If `src-namespace-prefix` is `dktapps\test`, `dktapps\test\Main` will be searched for in `src/Main.php`, instead of `src/dktapps/test/Main.php`. * Migrate TesterPlugin to PSR-4 --- composer.lock | 10 +++++----- src/plugin/PharPluginLoader.php | 5 ++++- src/plugin/PluginDescription.php | 5 +++++ .../phpstan/configs/check-explicit-mixed-baseline.neon | 5 +++++ tests/plugins/DevTools | 2 +- tests/plugins/TesterPlugin/plugin.yml | 1 + .../TesterPlugin/src/{pmmp/TesterPlugin => }/Main.php | 0 .../TesterPlugin/src/{pmmp/TesterPlugin => }/Test.php | 0 .../{pmmp/TesterPlugin => }/TestFailedException.php | 0 9 files changed, 21 insertions(+), 7 deletions(-) rename tests/plugins/TesterPlugin/src/{pmmp/TesterPlugin => }/Main.php (100%) rename tests/plugins/TesterPlugin/src/{pmmp/TesterPlugin => }/Test.php (100%) rename tests/plugins/TesterPlugin/src/{pmmp/TesterPlugin => }/TestFailedException.php (100%) diff --git a/composer.lock b/composer.lock index 87b4f7b31..c476cc86d 100644 --- a/composer.lock +++ b/composer.lock @@ -415,12 +415,12 @@ "source": { "type": "git", "url": "https://github.com/pmmp/ClassLoader.git", - "reference": "c0133050639cd4a8baefc56a4f334f269c66a7e0" + "reference": "9fb3957cbfe5b37dae8952e072b35540fb4d9aa7" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/pmmp/ClassLoader/zipball/c0133050639cd4a8baefc56a4f334f269c66a7e0", - "reference": "c0133050639cd4a8baefc56a4f334f269c66a7e0", + "url": "https://api.github.com/repos/pmmp/ClassLoader/zipball/9fb3957cbfe5b37dae8952e072b35540fb4d9aa7", + "reference": "9fb3957cbfe5b37dae8952e072b35540fb4d9aa7", "shasum": "" }, "require": { @@ -433,7 +433,7 @@ }, "require-dev": { "phpstan/extension-installer": "^1.0", - "phpstan/phpstan": "0.12.66", + "phpstan/phpstan": "0.12.80", "phpstan/phpstan-strict-rules": "^0.12.4" }, "type": "library", @@ -451,7 +451,7 @@ "issues": "https://github.com/pmmp/ClassLoader/issues", "source": "https://github.com/pmmp/ClassLoader/tree/master" }, - "time": "2021-01-15T00:41:29+00:00" + "time": "2021-04-19T14:36:23+00:00" }, { "name": "pocketmine/color", diff --git a/src/plugin/PharPluginLoader.php b/src/plugin/PharPluginLoader.php index 2907a0e60..d42fa0b5e 100644 --- a/src/plugin/PharPluginLoader.php +++ b/src/plugin/PharPluginLoader.php @@ -48,7 +48,10 @@ class PharPluginLoader implements PluginLoader{ * Loads the plugin contained in $file */ public function loadPlugin(string $file) : void{ - $this->loader->addPath("$file/src"); + $description = $this->getPluginDescription($file); + if($description !== null){ + $this->loader->addPath($description->getSrcNamespacePrefix(), "$file/src"); + } } /** diff --git a/src/plugin/PluginDescription.php b/src/plugin/PluginDescription.php index 506fe8154..02a81da48 100644 --- a/src/plugin/PluginDescription.php +++ b/src/plugin/PluginDescription.php @@ -48,6 +48,7 @@ class PluginDescription{ private $name; /** @var string */ private $main; + private string $srcNamespacePrefix = ""; /** @var string[] */ private $api; /** @var int[] */ @@ -114,6 +115,8 @@ class PluginDescription{ throw new PluginException("Invalid Plugin main, cannot start within the PocketMine namespace"); } + $this->srcNamespacePrefix = $plugin["src-namespace-prefix"] ?? ""; + $this->api = array_map("\strval", (array) ($plugin["api"] ?? [])); $this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin["mcpe-protocol"] ?? [])); $this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin["os"] ?? [])); @@ -284,6 +287,8 @@ class PluginDescription{ return $this->main; } + public function getSrcNamespacePrefix() : string{ return $this->srcNamespacePrefix; } + public function getName() : string{ return $this->name; } diff --git a/tests/phpstan/configs/check-explicit-mixed-baseline.neon b/tests/phpstan/configs/check-explicit-mixed-baseline.neon index 0fe171efd..d746d6d60 100644 --- a/tests/phpstan/configs/check-explicit-mixed-baseline.neon +++ b/tests/phpstan/configs/check-explicit-mixed-baseline.neon @@ -210,6 +210,11 @@ parameters: count: 1 path: ../../../src/plugin/PluginDescription.php + - + message: "#^Property pocketmine\\\\plugin\\\\PluginDescription\\:\\:\\$srcNamespacePrefix \\(string\\) does not accept mixed\\.$#" + count: 1 + path: ../../../src/plugin/PluginDescription.php + - message: "#^Parameter \\#1 \\$plugins of class pocketmine\\\\plugin\\\\PluginGraylist constructor expects array\\, mixed given\\.$#" count: 1 diff --git a/tests/plugins/DevTools b/tests/plugins/DevTools index 888d02126..dfbea943e 160000 --- a/tests/plugins/DevTools +++ b/tests/plugins/DevTools @@ -1 +1 @@ -Subproject commit 888d021260efa5456cd7cc0174fb3a2663960811 +Subproject commit dfbea943e1c64094358acac56013b89065884657 diff --git a/tests/plugins/TesterPlugin/plugin.yml b/tests/plugins/TesterPlugin/plugin.yml index 1cb9f9a1f..dc00d97c7 100644 --- a/tests/plugins/TesterPlugin/plugin.yml +++ b/tests/plugins/TesterPlugin/plugin.yml @@ -1,5 +1,6 @@ name: TesterPlugin main: pmmp\TesterPlugin\Main +src-namespace-prefix: pmmp\TesterPlugin version: 0.1.0 api: [3.2.0, 4.0.0] load: POSTWORLD diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php b/tests/plugins/TesterPlugin/src/Main.php similarity index 100% rename from tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Main.php rename to tests/plugins/TesterPlugin/src/Main.php diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Test.php b/tests/plugins/TesterPlugin/src/Test.php similarity index 100% rename from tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/Test.php rename to tests/plugins/TesterPlugin/src/Test.php diff --git a/tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/TestFailedException.php b/tests/plugins/TesterPlugin/src/TestFailedException.php similarity index 100% rename from tests/plugins/TesterPlugin/src/pmmp/TesterPlugin/TestFailedException.php rename to tests/plugins/TesterPlugin/src/TestFailedException.php