From 93a9007f3c684a1e8057dd97ec58c92b70a5ebae Mon Sep 17 00:00:00 2001 From: "Dylan T." Date: Sun, 1 Dec 2024 15:10:07 +0000 Subject: [PATCH] Added ClosureCommand (#6063) this is intended to replace PluginCommand and CommandExecutor, both of which are overengineered and unfit for purpose. Allowing a closure allows much greater flexibility. We can't use this within the core yet, as plugins will expect PluginBase->getCommand() to return PluginCommand (with its associated setExecutor() and similar APIs). However, I think this is useful enough to add by itself. --- src/command/ClosureCommand.php | 60 ++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 src/command/ClosureCommand.php diff --git a/src/command/ClosureCommand.php b/src/command/ClosureCommand.php new file mode 100644 index 000000000..289c82853 --- /dev/null +++ b/src/command/ClosureCommand.php @@ -0,0 +1,60 @@ + $args) : mixed + */ +final class ClosureCommand extends Command{ + /** @phpstan-var Execute */ + private \Closure $execute; + + /** + * @param string[] $permissions + * @phpstan-param Execute $execute + */ + public function __construct( + string $name, + \Closure $execute, + array $permissions, + Translatable|string $description = "", + Translatable|string|null $usageMessage = null, + array $aliases = [] + ){ + Utils::validateCallableSignature( + fn(CommandSender $sender, Command $command, string $commandLabel, array $args) : mixed => 1, + $execute, + ); + $this->execute = $execute; + parent::__construct($name, $description, $usageMessage, $aliases); + $this->setPermissions($permissions); + } + + public function execute(CommandSender $sender, string $commandLabel, array $args){ + return ($this->execute)($sender, $this, $commandLabel, $args); + } +}