Files
kiri-rpc/src/RpcManager.php
T

58 lines
1.1 KiB
PHP
Raw Normal View History

2021-10-28 10:52:09 +08:00
<?php
namespace Kiri\Rpc;
2021-10-28 15:14:21 +08:00
use Kiri\Kiri;
use ReflectionException;
2021-10-28 10:52:09 +08:00
class RpcManager
{
2021-10-28 15:14:21 +08:00
private static array $_rpc = [];
2021-10-28 10:52:09 +08:00
2021-10-28 15:14:21 +08:00
/**
* @param string $name
* @param string $class
2021-10-29 18:06:04 +08:00
* @param string $serviceId
2021-10-28 15:14:21 +08:00
* @return bool
* @throws ReflectionException
*/
2021-10-29 18:06:04 +08:00
public static function add(string $name, string $class, string $serviceId): 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-10-29 19:00:15 +08:00
if (!isset(static::$_rpc[$name])) static::$_rpc[$name] = ['methods' => [], 'id' => $serviceId];
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-10-29 19:00:15 +08:00
static::$_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-10-29 18:06:04 +08:00
public static function doneList(): array
{
$array = [];
foreach (static::$_rpc as $list) {
2021-10-29 19:00:15 +08:00
$array[] = $list['id'];
2021-10-29 18:06:04 +08:00
}
return $array;
}
2021-10-28 15:14:21 +08:00
/**
* @param string $name
* @param string $method
* @return mixed
*/
public static function get(string $name, string $method): array
2021-10-28 10:52:09 +08:00
{
2021-10-29 19:00:15 +08:00
return static::$_rpc[$name]['methods'][$method] ?? [null, null];
2021-10-28 10:52:09 +08:00
}
}