PluginDescription: const-ify plugin.yml parsing keys

This commit is contained in:
Dylan K. Taylor 2022-12-23 16:11:03 +00:00
parent 51a684c0ea
commit 3987ee6cb2
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D

View File

@ -37,6 +37,32 @@ use function stripos;
use function yaml_parse; use function yaml_parse;
class PluginDescription{ class PluginDescription{
private const KEY_NAME = "name";
private const KEY_VERSION = "version";
private const KEY_MAIN = "main";
private const KEY_SRC_NAMESPACE_PREFIX = "src-namespace-prefix";
private const KEY_API = "api";
private const KEY_MCPE_PROTOCOL = "mcpe-protocol";
private const KEY_OS = "os";
private const KEY_DEPEND = "depend";
private const KEY_SOFTDEPEND = "softdepend";
private const KEY_LOADBEFORE = "loadbefore";
private const KEY_EXTENSIONS = "extensions";
private const KEY_WEBSITE = "website";
private const KEY_DESCRIPTION = "description";
private const KEY_LOGGER_PREFIX = "prefix";
private const KEY_LOAD = "load";
private const KEY_AUTHOR = "author";
private const KEY_AUTHORS = "authors";
private const KEY_PERMISSIONS = "permissions";
private const KEY_COMMANDS = "commands";
private const KEY_COMMAND_PERMISSION = "permission";
private const KEY_COMMAND_DESCRIPTION = self::KEY_DESCRIPTION;
private const KEY_COMMAND_USAGE = "usage";
private const KEY_COMMAND_ALIASES = "aliases";
private const KEY_COMMAND_PERMISSION_MESSAGE = "permission-message";
/** /**
* @var mixed[] * @var mixed[]
* @phpstan-var array<string, mixed> * @phpstan-var array<string, mixed>
@ -107,49 +133,49 @@ class PluginDescription{
private function loadMap(array $plugin) : void{ private function loadMap(array $plugin) : void{
$this->map = $plugin; $this->map = $plugin;
$this->name = $plugin["name"]; $this->name = $plugin[self::KEY_NAME];
if(preg_match('/^[A-Za-z0-9 _.-]+$/', $this->name) === 0){ if(preg_match('/^[A-Za-z0-9 _.-]+$/', $this->name) === 0){
throw new PluginDescriptionParseException("Invalid Plugin name"); throw new PluginDescriptionParseException("Invalid Plugin name");
} }
$this->name = str_replace(" ", "_", $this->name); $this->name = str_replace(" ", "_", $this->name);
$this->version = (string) $plugin["version"]; $this->version = (string) $plugin[self::KEY_VERSION];
$this->main = $plugin["main"]; $this->main = $plugin[self::KEY_MAIN];
if(stripos($this->main, "pocketmine\\") === 0){ if(stripos($this->main, "pocketmine\\") === 0){
throw new PluginDescriptionParseException("Invalid Plugin main, cannot start within the PocketMine namespace"); throw new PluginDescriptionParseException("Invalid Plugin main, cannot start within the PocketMine namespace");
} }
$this->srcNamespacePrefix = $plugin["src-namespace-prefix"] ?? ""; $this->srcNamespacePrefix = $plugin[self::KEY_SRC_NAMESPACE_PREFIX] ?? "";
$this->api = array_map("\strval", (array) ($plugin["api"] ?? [])); $this->api = array_map("\strval", (array) ($plugin[self::KEY_API] ?? []));
$this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin["mcpe-protocol"] ?? [])); $this->compatibleMcpeProtocols = array_map("\intval", (array) ($plugin[self::KEY_MCPE_PROTOCOL] ?? []));
$this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin["os"] ?? [])); $this->compatibleOperatingSystems = array_map("\strval", (array) ($plugin[self::KEY_OS] ?? []));
if(isset($plugin["commands"]) && is_array($plugin["commands"])){ if(isset($plugin[self::KEY_COMMANDS]) && is_array($plugin[self::KEY_COMMANDS])){
foreach($plugin["commands"] as $commandName => $commandData){ foreach($plugin[self::KEY_COMMANDS] as $commandName => $commandData){
if(!is_string($commandName)){ if(!is_string($commandName)){
throw new PluginDescriptionParseException("Invalid Plugin commands, key must be the name of the command"); throw new PluginDescriptionParseException("Invalid Plugin commands, key must be the name of the command");
} }
if(!is_array($commandData)){ if(!is_array($commandData)){
throw new PluginDescriptionParseException("Command $commandName has invalid properties"); throw new PluginDescriptionParseException("Command $commandName has invalid properties");
} }
if(!isset($commandData["permission"]) || !is_string($commandData["permission"])){ if(!isset($commandData[self::KEY_COMMAND_PERMISSION]) || !is_string($commandData[self::KEY_COMMAND_PERMISSION])){
throw new PluginDescriptionParseException("Command $commandName does not have a valid permission set"); throw new PluginDescriptionParseException("Command $commandName does not have a valid permission set");
} }
$this->commands[$commandName] = new PluginDescriptionCommandEntry( $this->commands[$commandName] = new PluginDescriptionCommandEntry(
$commandData["description"] ?? null, $commandData[self::KEY_COMMAND_DESCRIPTION] ?? null,
$commandData["usage"] ?? null, $commandData[self::KEY_COMMAND_USAGE] ?? null,
$commandData["aliases"] ?? [], $commandData[self::KEY_COMMAND_ALIASES] ?? [],
$commandData["permission"], $commandData[self::KEY_COMMAND_PERMISSION],
$commandData["permission-message"] ?? null $commandData[self::KEY_COMMAND_PERMISSION_MESSAGE] ?? null
); );
} }
} }
if(isset($plugin["depend"])){ if(isset($plugin[self::KEY_DEPEND])){
$this->depend = (array) $plugin["depend"]; $this->depend = (array) $plugin[self::KEY_DEPEND];
} }
if(isset($plugin["extensions"])){ if(isset($plugin[self::KEY_EXTENSIONS])){
$extensions = (array) $plugin["extensions"]; $extensions = (array) $plugin[self::KEY_EXTENSIONS];
$isLinear = $extensions === array_values($extensions); $isLinear = $extensions === array_values($extensions);
foreach($extensions as $k => $v){ foreach($extensions as $k => $v){
if($isLinear){ if($isLinear){
@ -160,20 +186,20 @@ class PluginDescription{
} }
} }
$this->softDepend = (array) ($plugin["softdepend"] ?? $this->softDepend); $this->softDepend = (array) ($plugin[self::KEY_SOFTDEPEND] ?? $this->softDepend);
$this->loadBefore = (array) ($plugin["loadbefore"] ?? $this->loadBefore); $this->loadBefore = (array) ($plugin[self::KEY_LOADBEFORE] ?? $this->loadBefore);
$this->website = (string) ($plugin["website"] ?? $this->website); $this->website = (string) ($plugin[self::KEY_WEBSITE] ?? $this->website);
$this->description = (string) ($plugin["description"] ?? $this->description); $this->description = (string) ($plugin[self::KEY_DESCRIPTION] ?? $this->description);
$this->prefix = (string) ($plugin["prefix"] ?? $this->prefix); $this->prefix = (string) ($plugin[self::KEY_LOGGER_PREFIX] ?? $this->prefix);
if(isset($plugin["load"])){ if(isset($plugin[self::KEY_LOAD])){
$order = PluginEnableOrder::fromString($plugin["load"]); $order = PluginEnableOrder::fromString($plugin[self::KEY_LOAD]);
if($order === null){ if($order === null){
throw new PluginDescriptionParseException("Invalid Plugin \"load\""); throw new PluginDescriptionParseException("Invalid Plugin \"" . self::KEY_LOAD . "\"");
} }
$this->order = $order; $this->order = $order;
}else{ }else{
@ -181,24 +207,24 @@ class PluginDescription{
} }
$this->authors = []; $this->authors = [];
if(isset($plugin["author"])){ if(isset($plugin[self::KEY_AUTHOR])){
if(is_array($plugin["author"])){ if(is_array($plugin[self::KEY_AUTHOR])){
$this->authors = $plugin["author"]; $this->authors = $plugin[self::KEY_AUTHOR];
}else{ }else{
$this->authors[] = $plugin["author"]; $this->authors[] = $plugin[self::KEY_AUTHOR];
} }
} }
if(isset($plugin["authors"])){ if(isset($plugin[self::KEY_AUTHORS])){
foreach($plugin["authors"] as $author){ foreach($plugin[self::KEY_AUTHORS] as $author){
$this->authors[] = $author; $this->authors[] = $author;
} }
} }
if(isset($plugin["permissions"])){ if(isset($plugin[self::KEY_PERMISSIONS])){
try{ try{
$this->permissions = PermissionParser::loadPermissions($plugin["permissions"]); $this->permissions = PermissionParser::loadPermissions($plugin[self::KEY_PERMISSIONS]);
}catch(PermissionParserException $e){ }catch(PermissionParserException $e){
throw new PluginDescriptionParseException("Invalid Plugin \"permissions\": " . $e->getMessage(), 0, $e); throw new PluginDescriptionParseException("Invalid Plugin \"" . self::KEY_PERMISSIONS . "\": " . $e->getMessage(), 0, $e);
} }
} }
} }