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
9 changed files with 21 additions and 7 deletions

View File

@@ -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");
}
}
/**

View File

@@ -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;
}