2021-10-28 10:52:09 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Rpc;
|
|
|
|
|
|
2021-12-02 14:06:57 +08:00
|
|
|
use Kiri\Consul\Agent;
|
2021-10-28 15:14:21 +08:00
|
|
|
use Kiri\Kiri;
|
|
|
|
|
use ReflectionException;
|
|
|
|
|
|
2021-10-28 10:52:09 +08:00
|
|
|
class RpcManager
|
|
|
|
|
{
|
|
|
|
|
|
2021-11-30 14:44:34 +08:00
|
|
|
|
|
|
|
|
/**
|
2021-11-30 14:44:51 +08:00
|
|
|
* @var array
|
2021-11-30 14:44:34 +08:00
|
|
|
*/
|
2021-12-02 14:06:57 +08:00
|
|
|
private array $_rpc = [];
|
2021-10-28 15:14:21 +08:00
|
|
|
|
2021-10-28 10:52:09 +08:00
|
|
|
|
2021-10-28 15:14:21 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string $class
|
2021-12-02 14:06:57 +08:00
|
|
|
* @param array $serviceConfig
|
2021-10-28 15:14:21 +08:00
|
|
|
* @return bool
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
2021-12-02 14:06:57 +08:00
|
|
|
public function add(string $name, string $class, array $serviceConfig): bool
|
2021-10-28 10:52:09 +08:00
|
|
|
{
|
2021-10-28 15:14:21 +08:00
|
|
|
$methods = Kiri::getDi()->getReflect($class);
|
|
|
|
|
$lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC);
|
|
|
|
|
|
2021-12-02 14:12:27 +08:00
|
|
|
if (!isset($this->_rpc[$name])) $this->_rpc[$name] = ['methods' => [], 'id' => $serviceConfig['ID'], 'config' => $serviceConfig];
|
2021-10-28 10:52:09 +08:00
|
|
|
|
2021-10-28 15:14:21 +08:00
|
|
|
foreach ($lists as $reflection) {
|
2021-10-28 15:39:34 +08:00
|
|
|
$methodName = $reflection->getName();
|
2021-10-28 15:14:21 +08:00
|
|
|
|
2021-12-02 14:06:57 +08:00
|
|
|
$this->_rpc[$name]['methods'][$methodName] = [[$class, $methodName], null];
|
2021-10-28 15:14:21 +08:00
|
|
|
}
|
|
|
|
|
return true;
|
2021-10-28 10:52:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-11-30 14:44:34 +08:00
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
2021-12-02 14:06:57 +08:00
|
|
|
public function doneList(): array
|
2021-10-29 18:06:04 +08:00
|
|
|
{
|
|
|
|
|
$array = [];
|
2021-12-02 14:06:57 +08:00
|
|
|
foreach ($this->_rpc as $list) {
|
|
|
|
|
$array[] = $list;
|
2021-10-29 18:06:04 +08:00
|
|
|
}
|
|
|
|
|
return $array;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-12-02 14:06:57 +08:00
|
|
|
/**
|
|
|
|
|
* @throws ReflectionException
|
|
|
|
|
*/
|
|
|
|
|
public function register()
|
|
|
|
|
{
|
|
|
|
|
$agent = Kiri::getDi()->get(Agent::class);
|
|
|
|
|
foreach ($this->_rpc as $list) {
|
|
|
|
|
$data = $agent->service->register($list['config']);
|
|
|
|
|
if ($data->getStatusCode() != 200) {
|
|
|
|
|
exit($data->getBody()->getContents());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2021-10-28 15:14:21 +08:00
|
|
|
/**
|
|
|
|
|
* @param string $name
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2021-12-02 14:06:57 +08:00
|
|
|
public function get(string $name, string $method): array
|
2021-10-28 10:52:09 +08:00
|
|
|
{
|
2021-12-02 14:06:57 +08:00
|
|
|
return $this->_rpc[$name]['methods'][$method] ?? [null, null];
|
2021-10-28 10:52:09 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|