Files
kiri-core/Rpc/Producer.php
T

189 lines
3.4 KiB
PHP
Raw Normal View History

2021-03-23 02:29:48 +08:00
<?php
namespace Rpc;
2021-03-23 10:30:14 +08:00
use Exception;
2021-03-24 18:51:07 +08:00
use JetBrains\PhpStorm\ArrayShape;
2021-03-23 02:29:48 +08:00
use Snowflake\Abstracts\Component;
use Snowflake\Snowflake;
/**
* Class Producer
* @package Rpc
*/
class Producer extends Component
{
2021-05-07 17:06:10 +08:00
private static array $producers = [];
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
private static array $classAlias = [];
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
private static array $consumers = [];
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
private static array $cods = [];
2021-03-24 01:00:43 +08:00
2021-03-24 15:06:02 +08:00
/**
* @param string $name
* @param array $handler
* @param array $node
*/
public function addProducer(string $name, array $handler, array $node)
{
2021-05-07 17:06:10 +08:00
static::$classAlias[$handler[0]::class] = $name;
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
static::$consumers[$name] = $handler[0];
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
static::$producers[$name] = $node;
2021-03-24 15:06:02 +08:00
}
2021-03-24 01:00:43 +08:00
2021-03-24 11:01:16 +08:00
/**
* @param string $cmd
* @param array $handler
*/
2021-03-24 15:06:02 +08:00
public function addConsumer(string $cmd, array $handler)
{
2021-04-25 11:22:23 +08:00
$class = $handler[0]::class;
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
if (!isset(static::$classAlias[$class])) {
2021-03-24 15:06:02 +08:00
return;
}
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
$name = static::$classAlias[$class];
2021-03-24 01:00:43 +08:00
2021-05-07 17:06:10 +08:00
static::$cods[$name . '.' . $cmd] = $handler;
2021-03-24 15:06:02 +08:00
}
2021-03-24 01:00:43 +08:00
2021-03-24 11:01:16 +08:00
/**
* @param $cmd
* @param mixed ...$params
* @return mixed
*/
2021-03-24 15:06:02 +08:00
public function dispatch($cmd, mixed ...$params): mixed
{
2021-05-07 17:06:10 +08:00
$handler = static::$cods[$cmd] ?? null;
2021-03-24 15:06:02 +08:00
if (empty($handler)) {
return false;
}
return call_user_func($handler, ...$params);
}
/**
* @param $name
* @return mixed
* @throws Exception
*/
public function get($name): mixed
{
2021-05-07 17:06:10 +08:00
if (!isset(static::$consumers[$name])) {
2021-03-24 15:06:02 +08:00
throw new Exception('Unknown rpc client.');
}
2021-05-07 17:06:10 +08:00
return static::$consumers[$name];
2021-03-24 15:06:02 +08:00
}
2021-03-24 15:53:49 +08:00
/**
* @return array
*/
public function getService(): array
{
$array = [];
2021-05-07 17:06:10 +08:00
foreach (array_keys(static::$cods) as $key) {
2021-03-24 15:53:49 +08:00
$explode = explode('.', $key);
$prefix = array_shift($explode);
$explode = implode('.', $explode);
if (!isset($array[$prefix])) {
$array[$prefix] = [];
}
$array[$prefix][] = $explode;
}
return $array;
}
2021-03-24 15:06:02 +08:00
/**
* @param string $name
2021-03-24 19:02:43 +08:00
* @param string|null $host
2021-03-24 15:06:02 +08:00
* @return mixed
* @throws Exception
*/
2021-03-24 19:02:43 +08:00
public function getClient(string $name, string $host = null): Client
2021-03-24 15:06:02 +08:00
{
2021-05-07 17:06:10 +08:00
$producer = static::$producers[$name] ?? null;
2021-03-24 18:32:14 +08:00
if ($producer === null) {
2021-03-24 15:06:02 +08:00
throw new Exception('Unknown rpc client config.');
}
2021-03-24 19:02:43 +08:00
if (!empty($host)) {
$producer['host'] = $host;
} else if (!isset($producer['host'])) {
2021-03-24 18:51:07 +08:00
$producer['host'] = Snowflake::localhost();
}
2021-03-24 15:06:02 +08:00
$producerName = $this->getName($name, $producer);
$snowflake = Snowflake::app();
if (!$snowflake->has($producerName)) {
2021-03-24 18:32:14 +08:00
return $snowflake->set($producerName, $this->definer($name, $producer));
2021-03-24 15:06:02 +08:00
} else {
return $snowflake->get($producerName);
}
}
2021-03-24 19:13:58 +08:00
/**
* @param string $name
* @param string|null $host
* @return Client
2021-03-31 18:37:53 +08:00
* @throws Exception
2021-03-24 19:13:58 +08:00
*/
public function consumer(string $name, string $host = null): Client
{
return $this->getClient($name, $host);
}
2021-03-24 18:32:14 +08:00
/**
* @param $name
* @param $producer
* @return array
*/
2021-03-24 18:51:07 +08:00
#[ArrayShape(['class' => "string", 'service' => "", 'config' => ""])]
2021-03-24 18:32:14 +08:00
private function definer($name, $producer): array
{
return ['class' => Client::class, 'service' => $name, 'config' => $producer];
}
2021-03-24 15:06:02 +08:00
/**
* @param $name
* @return Client|bool
* @throws Exception
*/
public function __get($name): Client|bool
{
return $this->get($name); // TODO: Change the autogenerated stub
}
2021-04-06 18:16:36 +08:00
2021-03-24 15:06:02 +08:00
/**
* @param $name
2021-03-24 18:51:07 +08:00
* @param $config
2021-03-24 15:06:02 +08:00
* @return string
*/
2021-03-24 18:51:07 +08:00
private function getName($name, $config): string
2021-03-24 15:06:02 +08:00
{
2021-03-24 18:51:07 +08:00
return 'rpc.client.' . $name . '.' . $config['host'];
2021-03-24 15:06:02 +08:00
}
2021-03-23 02:29:48 +08:00
}