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
This commit is contained in:
Dylan T 2021-04-23 19:34:46 +01:00 committed by GitHub
parent fc01735b6f
commit 127b57048c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 21 additions and 7 deletions

10
composer.lock generated
View File

@ -415,12 +415,12 @@
"source": { "source": {
"type": "git", "type": "git",
"url": "https://github.com/pmmp/ClassLoader.git", "url": "https://github.com/pmmp/ClassLoader.git",
"reference": "c0133050639cd4a8baefc56a4f334f269c66a7e0" "reference": "9fb3957cbfe5b37dae8952e072b35540fb4d9aa7"
}, },
"dist": { "dist": {
"type": "zip", "type": "zip",
"url": "https://api.github.com/repos/pmmp/ClassLoader/zipball/c0133050639cd4a8baefc56a4f334f269c66a7e0", "url": "https://api.github.com/repos/pmmp/ClassLoader/zipball/9fb3957cbfe5b37dae8952e072b35540fb4d9aa7",
"reference": "c0133050639cd4a8baefc56a4f334f269c66a7e0", "reference": "9fb3957cbfe5b37dae8952e072b35540fb4d9aa7",
"shasum": "" "shasum": ""
}, },
"require": { "require": {
@ -433,7 +433,7 @@
}, },
"require-dev": { "require-dev": {
"phpstan/extension-installer": "^1.0", "phpstan/extension-installer": "^1.0",
"phpstan/phpstan": "0.12.66", "phpstan/phpstan": "0.12.80",
"phpstan/phpstan-strict-rules": "^0.12.4" "phpstan/phpstan-strict-rules": "^0.12.4"
}, },
"type": "library", "type": "library",
@ -451,7 +451,7 @@
"issues": "https://github.com/pmmp/ClassLoader/issues", "issues": "https://github.com/pmmp/ClassLoader/issues",
"source": "https://github.com/pmmp/ClassLoader/tree/master" "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", "name": "pocketmine/color",

View File

@ -48,7 +48,10 @@ class PharPluginLoader implements PluginLoader{
* Loads the plugin contained in $file * Loads the plugin contained in $file
*/ */
public function loadPlugin(string $file) : void{ 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");
}
} }
/** /**

View File

@ -48,6 +48,7 @@ class PluginDescription{
private $name; private $name;
/** @var string */ /** @var string */
private $main; private $main;
private string $srcNamespacePrefix = "";
/** @var string[] */ /** @var string[] */
private $api; private $api;
/** @var int[] */ /** @var int[] */
@ -114,6 +115,8 @@ class PluginDescription{
throw new PluginException("Invalid Plugin main, cannot start within the PocketMine namespace"); 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->api = array_map("\strval", (array) ($plugin["api"] ?? []));
$this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin["mcpe-protocol"] ?? [])); $this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin["mcpe-protocol"] ?? []));
$this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin["os"] ?? [])); $this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin["os"] ?? []));
@ -284,6 +287,8 @@ class PluginDescription{
return $this->main; return $this->main;
} }
public function getSrcNamespacePrefix() : string{ return $this->srcNamespacePrefix; }
public function getName() : string{ public function getName() : string{
return $this->name; return $this->name;
} }

View File

@ -210,6 +210,11 @@ parameters:
count: 1 count: 1
path: ../../../src/plugin/PluginDescription.php 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\\<string\\>, mixed given\\.$#" message: "#^Parameter \\#1 \\$plugins of class pocketmine\\\\plugin\\\\PluginGraylist constructor expects array\\<string\\>, mixed given\\.$#"
count: 1 count: 1

@ -1 +1 @@
Subproject commit 888d021260efa5456cd7cc0174fb3a2663960811 Subproject commit dfbea943e1c64094358acac56013b89065884657

View File

@ -1,5 +1,6 @@
name: TesterPlugin name: TesterPlugin
main: pmmp\TesterPlugin\Main main: pmmp\TesterPlugin\Main
src-namespace-prefix: pmmp\TesterPlugin
version: 0.1.0 version: 0.1.0
api: [3.2.0, 4.0.0] api: [3.2.0, 4.0.0]
load: POSTWORLD load: POSTWORLD