classAlias[get_class($handler[0])] = $name; $this->consumers[$name] = $handler[0]; $this->producers[$name] = $node; } /** * @param string $cmd * @param array $handler */ public function addConsumer(string $cmd, array $handler) { $class = get_class($handler[0]); if (!isset($this->classAlias[$class])) { return; } $name = $this->classAlias[$class]; $this->cmds[$name . '.' . $cmd] = $handler; } /** * @param $cmd * @param mixed ...$params * @return mixed */ public function dispatch($cmd, mixed ...$params): mixed { $handler = $this->cmds[$cmd] ?? null; if (empty($handler)) { return false; } return call_user_func($handler, ...$params); } /** * @param $name * @return mixed * @throws Exception */ public function get($name): mixed { if (!isset($this->consumers[$name])) { throw new Exception('Unknown rpc client.'); } return $this->consumers[$name]; } /** * @param string $name * @return mixed * @throws Exception */ public function getClient(string $name): Client { if (!is_array($producer = $this->producers[$name] ?? null)) { throw new Exception('Unknown rpc client config.'); } $producerName = $this->getName($name, $producer); $snowflake = Snowflake::app(); if (!$snowflake->has($producerName)) { return $snowflake->set($producerName, ['class' => Client::class, 'config' => $producer]); } else { return $snowflake->get($producerName); } } /** * @param $name * @return Client|bool * @throws Exception */ public function __get($name): Client|bool { return $this->get($name); // TODO: Change the autogenerated stub } /** * @param $name * @param $rand * @return string */ private function getName($name, $rand): string { return 'rpc.client.' . $name . '.' . $rand['host']; } }