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 { $isNew = !array_key_exists($command, $this->commands); $this->commands[$command] = new Command($command, $handler); if ($isNew) { $this->emit(self::EVENT_REGISTERED, [ $command ]); } } 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)) { return; } unset($this->commands[$command]); $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; } public function getCommandNames(): array { return array_keys($this->commands); } }