mirror of
https://github.com/pmmp/PocketMine-MP.git
synced 2025-04-22 00:33:59 +00:00
Move permission parsing to dedicated PermissionParser class
This commit is contained in:
parent
58cafc853f
commit
efc2d72d5f
@ -27,10 +27,6 @@ declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
use function is_array;
|
||||
use function is_bool;
|
||||
use function strtolower;
|
||||
|
||||
/**
|
||||
* Represents a permission
|
||||
*/
|
||||
@ -42,106 +38,6 @@ class Permission{
|
||||
|
||||
public static $DEFAULT_PERMISSION = self::DEFAULT_OP;
|
||||
|
||||
/**
|
||||
* @param bool|string $value
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function getByName($value) : string{
|
||||
if(is_bool($value)){
|
||||
if($value){
|
||||
return "true";
|
||||
}else{
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
switch(strtolower($value)){
|
||||
case "op":
|
||||
case "isop":
|
||||
case "operator":
|
||||
case "isoperator":
|
||||
case "admin":
|
||||
case "isadmin":
|
||||
return self::DEFAULT_OP;
|
||||
|
||||
case "!op":
|
||||
case "notop":
|
||||
case "!operator":
|
||||
case "notoperator":
|
||||
case "!admin":
|
||||
case "notadmin":
|
||||
return self::DEFAULT_NOT_OP;
|
||||
|
||||
case "true":
|
||||
return self::DEFAULT_TRUE;
|
||||
case "false":
|
||||
return self::DEFAULT_FALSE;
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException("Unknown permission default name \"$value\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param string $default
|
||||
*
|
||||
* @return Permission[]
|
||||
*/
|
||||
public static function loadPermissions(array $data, string $default = self::DEFAULT_OP) : array{
|
||||
$result = [];
|
||||
foreach($data as $key => $entry){
|
||||
$result[] = self::loadPermission($key, $entry, $default, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param string $default
|
||||
* @param array $output
|
||||
*
|
||||
* @return Permission
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function loadPermission(string $name, array $data, string $default = self::DEFAULT_OP, array &$output = []) : Permission{
|
||||
$desc = null;
|
||||
$children = [];
|
||||
if(isset($data["default"])){
|
||||
$value = Permission::getByName($data["default"]);
|
||||
if($value !== null){
|
||||
$default = $value;
|
||||
}else{
|
||||
throw new \InvalidStateException("'default' key contained unknown value");
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data["children"])){
|
||||
if(is_array($data["children"])){
|
||||
foreach($data["children"] as $k => $v){
|
||||
if(is_array($v)){
|
||||
if(($perm = self::loadPermission($k, $v, $default, $output)) !== null){
|
||||
$output[] = $perm;
|
||||
}
|
||||
}
|
||||
$children[$k] = true;
|
||||
}
|
||||
}else{
|
||||
throw new \InvalidStateException("'children' key is of wrong type");
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data["description"])){
|
||||
$desc = $data["description"];
|
||||
}
|
||||
|
||||
return new Permission($name, $desc, $default, $children);
|
||||
}
|
||||
|
||||
/** @var string */
|
||||
private $name;
|
||||
|
||||
|
127
src/pocketmine/permission/PermissionParser.php
Normal file
127
src/pocketmine/permission/PermissionParser.php
Normal file
@ -0,0 +1,127 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
*
|
||||
* ____ _ _ __ __ _ __ __ ____
|
||||
* | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
|
||||
* | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
|
||||
* | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
|
||||
* |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* @author PocketMine Team
|
||||
* @link http://www.pocketmine.net/
|
||||
*
|
||||
*
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace pocketmine\permission;
|
||||
|
||||
class PermissionParser{
|
||||
|
||||
/**
|
||||
* @param bool|string $value
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws \InvalidArgumentException
|
||||
*/
|
||||
public static function defaultFromString($value) : string{
|
||||
if(is_bool($value)){
|
||||
if($value){
|
||||
return "true";
|
||||
}else{
|
||||
return "false";
|
||||
}
|
||||
}
|
||||
switch(strtolower($value)){
|
||||
case "op":
|
||||
case "isop":
|
||||
case "operator":
|
||||
case "isoperator":
|
||||
case "admin":
|
||||
case "isadmin":
|
||||
return Permission::DEFAULT_OP;
|
||||
|
||||
case "!op":
|
||||
case "notop":
|
||||
case "!operator":
|
||||
case "notoperator":
|
||||
case "!admin":
|
||||
case "notadmin":
|
||||
return Permission::DEFAULT_NOT_OP;
|
||||
|
||||
case "true":
|
||||
return Permission::DEFAULT_TRUE;
|
||||
case "false":
|
||||
return Permission::DEFAULT_FALSE;
|
||||
}
|
||||
|
||||
throw new \InvalidArgumentException("Unknown permission default name \"$value\"");
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array $data
|
||||
* @param string $default
|
||||
*
|
||||
* @return Permission[]
|
||||
*/
|
||||
public static function loadPermissions(array $data, string $default = Permission::DEFAULT_OP) : array{
|
||||
$result = [];
|
||||
foreach($data as $key => $entry){
|
||||
$result[] = self::loadPermission($key, $entry, $default, $result);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $name
|
||||
* @param array $data
|
||||
* @param string $default
|
||||
* @param array $output
|
||||
*
|
||||
* @return Permission
|
||||
*
|
||||
* @throws \Exception
|
||||
*/
|
||||
public static function loadPermission(string $name, array $data, string $default = Permission::DEFAULT_OP, array &$output = []) : Permission{
|
||||
$desc = null;
|
||||
$children = [];
|
||||
if(isset($data["default"])){
|
||||
$value = PermissionParser::defaultFromString($data["default"]);
|
||||
if($value !== null){
|
||||
$default = $value;
|
||||
}else{
|
||||
throw new \InvalidStateException("'default' key contained unknown value");
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data["children"])){
|
||||
if(is_array($data["children"])){
|
||||
foreach($data["children"] as $k => $v){
|
||||
if(is_array($v)){
|
||||
if(($perm = self::loadPermission($k, $v, $default, $output)) !== null){
|
||||
$output[] = $perm;
|
||||
}
|
||||
}
|
||||
$children[$k] = true;
|
||||
}
|
||||
}else{
|
||||
throw new \InvalidStateException("'children' key is of wrong type");
|
||||
}
|
||||
}
|
||||
|
||||
if(isset($data["description"])){
|
||||
$desc = $data["description"];
|
||||
}
|
||||
|
||||
return new Permission($name, $desc, $default, $children);
|
||||
}
|
||||
}
|
@ -24,6 +24,7 @@ declare(strict_types=1);
|
||||
namespace pocketmine\plugin;
|
||||
|
||||
use pocketmine\permission\Permission;
|
||||
use pocketmine\permission\PermissionParser;
|
||||
use function array_map;
|
||||
use function array_values;
|
||||
use function constant;
|
||||
@ -146,7 +147,7 @@ class PluginDescription{
|
||||
}
|
||||
|
||||
if(isset($plugin["permissions"])){
|
||||
$this->permissions = Permission::loadPermissions($plugin["permissions"]);
|
||||
$this->permissions = PermissionParser::loadPermissions($plugin["permissions"]);
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user