Last active
July 10, 2020 16:47
-
-
Save TwistedAsylumMC/bd744154037fc03b81cdde5ec10b95af to your computer and use it in GitHub Desktop.
plugin.yml permissions generator for pocketmine 4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$generator = new \twisted\permissiongenerator\PermissionGenerator(); | |
// From a directory and all sub directories | |
$permissions = $generator->fromDirectory("/path/to/directory"); | |
// From a single directory | |
$permissions = $generator->fromDirectory("/path/to/directory", false); | |
// From multiple files | |
$permissions = $generator->fromFiles("/path/to/file1", "/path/to/file2", "/path/to/file3"); | |
// From single file | |
$permissions = $generator->fromFiles("/path/to/file"); | |
// $permissions is a yaml-encoded string that can be pasted into a plugin.yml | |
file_put_contents("./permissions.yml", $permissions); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace twisted\permissiongenerator; | |
use RecursiveDirectoryIterator; | |
use RecursiveIteratorIterator; | |
use SplFileInfo; | |
use function array_filter; | |
use function array_values; | |
use function file_get_contents; | |
use function is_array; | |
use function is_int; | |
use function str_replace; | |
use function token_get_all; | |
use function yaml_emit; | |
use const T_CONSTANT_ENCAPSED_STRING; | |
use const T_WHITESPACE; | |
class PermissionGenerator | |
{ | |
public function fromDirectory(string $path, bool $recursive = true): string | |
{ | |
$filePaths = []; | |
if ($recursive) { | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); | |
/** @var SplFileInfo $file */ | |
foreach ($files as $file) { | |
if ($file->getExtension() === "php") { | |
$filePaths[] = $file->getPathName(); | |
} | |
} | |
} else { | |
$filePaths = glob($path . "*.php"); | |
} | |
return $this->fromFiles(...$filePaths); | |
} | |
public function fromFiles(string ...$paths): string | |
{ | |
$permissions = []; | |
foreach ($paths as $path) { | |
$file = file_get_contents($path); | |
$tokens = array_values(array_filter(token_get_all($file), static function ($token): bool { | |
return is_array($token) && is_int($token[0]) && $token[0] !== T_WHITESPACE; | |
})); | |
for ($i = 0; $i < count($tokens); ++$i) { | |
$token = $tokens[$i] ?? [0]; | |
if ($token[0] === T_STRING && ($token[1] === "setPermission" || $token[1] === "hasPermission")) { | |
$value = $tokens[$i + 1] ?? [0]; | |
if ($value[0] === T_CONSTANT_ENCAPSED_STRING) { | |
$perm = str_replace("\"", "", $value[1]); | |
$permissions[$perm] = [ | |
"default" => "op" | |
]; | |
} | |
} | |
} | |
} | |
ksort($permissions); | |
return yaml_emit(["permissions" => $permissions]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment