Moved plugin extension requirement checks to PluginManager::checkPluginLoadability()

these don't really belong in PluginDescription.
This commit is contained in:
Dylan K. Taylor 2021-10-20 22:13:30 +01:00
parent aa408c9a97
commit 44508a138f
No known key found for this signature in database
GPG Key ID: 8927471A91CAFD3D
2 changed files with 30 additions and 43 deletions

View File

@ -30,13 +30,9 @@ use function array_map;
use function array_values;
use function is_array;
use function is_string;
use function phpversion;
use function preg_match;
use function str_replace;
use function stripos;
use function strlen;
use function substr;
use function version_compare;
use function yaml_parse;
class PluginDescription{
@ -247,40 +243,6 @@ class PluginDescription{
return $this->extensions;
}
/**
* Checks if the current PHP runtime has the extensions required by the plugin.
*
* @throws PluginException if there are required extensions missing or have incompatible version, or if the version constraint cannot be parsed
*/
public function checkRequiredExtensions() : void{
foreach($this->extensions as $name => $versionConstrs){
$gotVersion = phpversion($name);
if($gotVersion === false){
throw new PluginException("Required extension $name not loaded");
}
foreach($versionConstrs as $constr){ // versionConstrs_loop
if($constr === "*"){
continue;
}
if($constr === ""){
throw new PluginException("One of the extension version constraints of $name is empty. Consider quoting the version string in plugin.yml");
}
foreach(["<=", "le", "<>", "!=", "ne", "<", "lt", "==", "=", "eq", ">=", "ge", ">", "gt"] as $comparator){
// warning: the > character should be quoted in YAML
if(substr($constr, 0, strlen($comparator)) === $comparator){
$version = substr($constr, strlen($comparator));
if(!version_compare($gotVersion, $version, $comparator)){
throw new PluginException("Required extension $name has an incompatible version ($gotVersion not $constr)");
}
continue 2; // versionConstrs_loop
}
}
throw new PluginException("Error parsing version constraint: $constr");
}
}
}
/**
* @return string[]
*/

View File

@ -64,12 +64,16 @@ use function is_string;
use function is_subclass_of;
use function iterator_to_array;
use function mkdir;
use function phpversion;
use function realpath;
use function shuffle;
use function sprintf;
use function stripos;
use function strlen;
use function strpos;
use function strtolower;
use function substr;
use function version_compare;
/**
* Manages all the plugins
@ -136,7 +140,7 @@ class PluginManager{
return Path::join(dirname($pluginPath), $pluginName);
}
private function checkPluginLoadability(PluginDescription $description) : Translatable|string|null{
private function checkPluginLoadability(PluginDescription $description) : Translatable|null{
$name = $description->getName();
if(stripos($name, "pocketmine") !== false or stripos($name, "minecraft") !== false or stripos($name, "mojang") !== false){
return KnownTranslationFactory::pocketmine_plugin_restrictedName();
@ -168,10 +172,31 @@ class PluginManager{
}
}
try{
$description->checkRequiredExtensions();
}catch(PluginException $ex){
return $ex->getMessage();
foreach($description->getRequiredExtensions() as $extensionName => $versionConstrs){
$gotVersion = phpversion($extensionName);
if($gotVersion === false){
return KnownTranslationFactory::pocketmine_plugin_extensionNotLoaded($extensionName);
}
foreach($versionConstrs as $k => $constr){ // versionConstrs_loop
if($constr === "*"){
continue;
}
if($constr === ""){
return KnownTranslationFactory::pocketmine_plugin_emptyExtensionVersionConstraint(extensionName: $extensionName, constraintIndex: "$k");
}
foreach(["<=", "le", "<>", "!=", "ne", "<", "lt", "==", "=", "eq", ">=", "ge", ">", "gt"] as $comparator){
// warning: the > character should be quoted in YAML
if(substr($constr, 0, strlen($comparator)) === $comparator){
$version = substr($constr, strlen($comparator));
if(!version_compare($gotVersion, $version, $comparator)){
return KnownTranslationFactory::pocketmine_plugin_incompatibleExtensionVersion(extensionName: $extensionName, extensionVersion: $gotVersion, pluginRequirement: $constr);
}
continue 2; // versionConstrs_loop
}
}
return KnownTranslationFactory::pocketmine_plugin_invalidExtensionVersionConstraint(extensionName: $extensionName, versionConstraint: $constr);
}
}
return null;