From 070f60718776a23d7ebe5f77aa4984b641d3853d Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Mon, 7 Apr 2025 00:06:55 +0200 Subject: [PATCH] Add support for registering classes with CommandRegistry * Calling registerClass() on CommandRegistry will result in all methods having the #[RpcCallable("command-here")] attribute being registered as commands. * Commands can be unregistered by calling unregisterClass(). --- src/Attributes/RpcCallable.php | 13 ++++++++++++ src/CommandBus.php | 2 +- src/CommandBusClient.php | 2 +- src/CommandBusInterface.php | 2 +- src/CommandRegistry.php | 38 +++++++++++++++++++++++++++++++++- 5 files changed, 53 insertions(+), 4 deletions(-) create mode 100644 src/Attributes/RpcCallable.php diff --git a/src/Attributes/RpcCallable.php b/src/Attributes/RpcCallable.php new file mode 100644 index 0000000..2b83c83 --- /dev/null +++ b/src/Attributes/RpcCallable.php @@ -0,0 +1,13 @@ +executeContext($context); diff --git a/src/CommandBusClient.php b/src/CommandBusClient.php index 5d87f37..2af7ea9 100644 --- a/src/CommandBusClient.php +++ b/src/CommandBusClient.php @@ -161,7 +161,7 @@ class CommandBusClient implements CommandBusInterface /** * {@inheritDoc} */ - public function execute(string $command, array $context): PromiseInterface + public function execute(string $command, array $context = []): PromiseInterface { if (!$this->connection || !$this->connection->isWritable()) { $this->reconnect(); diff --git a/src/CommandBusInterface.php b/src/CommandBusInterface.php index c3d4e84..f90232a 100644 --- a/src/CommandBusInterface.php +++ b/src/CommandBusInterface.php @@ -17,7 +17,7 @@ interface CommandBusInterface extends EventEmitterInterface * @param array $context Data to pass in the call context * @®eturn PromiseInterface A promise that will be resolved with the result */ - public function execute(string $command, array $context): PromiseInterface; + public function execute(string $command, array $context = []): PromiseInterface; /** * Notify all connected clients to emit the notify event, and also be diff --git a/src/CommandRegistry.php b/src/CommandRegistry.php index c05b22f..0a26912 100644 --- a/src/CommandRegistry.php +++ b/src/CommandRegistry.php @@ -1,6 +1,7 @@ */ + /** @var array The registered commands and callbacks */ private array $commands = []; + /** @var array> Commands registered by scanning a class */ + private array $classCommands = []; public function register(string $command, callable $handler): void { @@ -29,6 +32,29 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface } } + public function registerClass(object $target): void + { + $a = function (\ReflectionMethod $r, string $a) { + $attrs = $r->getAttributes($a); + if (!$attrs) return null; + return array_shift($attrs)->newInstance(); + }; + + $r = new \ReflectionObject($target); + + $name = get_class($target); + $this->classCommands[$name] = []; + + foreach ($r->getMethods() as $method) { + $methodRpc = $a($method, Attributes\RpcCallable::class); + if ($methodRpc) { + $this->register($methodRpc->name, $method->getClosure($target)); + $this->classCommands[$name][] = $methodRpc->name; + } + } + + } + public function unregister(string $command): void { if (!array_key_exists($command, $this->commands)) { @@ -40,6 +66,16 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface $this->emit(self::EVENT_UNREGISTERED, [ $command ]); } + public function unregisterClass(object $target): void + { + $name = get_class($target); + if (!isset($this->classCommands[$name])) return; + foreach ($this->classCommands[$name] as $command) { + $this->unregister($command); + } + unset($this->classCommands[$name]); + } + public function findCommand(string $command): ?Command { return $this->commands[$command] ?? null;