modify plugin name
This commit is contained in:
@@ -2,12 +2,13 @@
|
||||
|
||||
namespace Kiri\Rpc\Annotation;
|
||||
|
||||
use Kiri;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Annotation\AbstractAttribute;
|
||||
use Kiri\Core\Network;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri;
|
||||
use Kiri\Message\Handler\Router;
|
||||
use Kiri\Rpc\RpcManager;
|
||||
use Kiri\Annotation\AbstractAttribute;
|
||||
use ReflectionException;
|
||||
|
||||
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends AbstractAttribute
|
||||
@@ -35,12 +36,13 @@ use ReflectionException;
|
||||
* @param mixed $class
|
||||
* @param mixed|string $method
|
||||
* @return mixed
|
||||
* @throws ReflectionException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function execute(mixed $class, mixed $method = ''): bool
|
||||
{
|
||||
return Kiri::getDi()->get(RpcManager::class)->add($this->service, $class, $this->create());
|
||||
$manager = Kiri::getDi()->get(RpcManager::class);
|
||||
|
||||
return $manager->add($this->service, $class, $this->create());
|
||||
}
|
||||
|
||||
|
||||
|
||||
+18
-7
@@ -11,8 +11,10 @@ use Kiri\Consul\Agent;
|
||||
use Kiri\Context;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Message\Constrict\RequestInterface;
|
||||
use Kiri\Message\Handler\DataGrip;
|
||||
use Kiri\Message\Handler\Handler;
|
||||
use Kiri\Message\Handler\Router;
|
||||
use Kiri\Message\Handler\RouterCollector;
|
||||
use Kiri\Message\ServerRequest;
|
||||
use Kiri\Server\Contract\OnCloseInterface;
|
||||
use Kiri\Server\Contract\OnConnectInterface;
|
||||
@@ -31,7 +33,6 @@ use Swoole\Coroutine\Channel;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@@ -47,12 +48,15 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
public Annotation $annotation;
|
||||
|
||||
|
||||
private RpcManager $manager;
|
||||
public RpcManager $manager;
|
||||
|
||||
|
||||
private int $timerId;
|
||||
|
||||
|
||||
public RouterCollector $collector;
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
@@ -71,6 +75,8 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
$provider->on(OnServerBeforeStart::class, [$this, 'register']);
|
||||
|
||||
$this->manager = Kiri::getDi()->get(RpcManager::class);
|
||||
|
||||
$this->collector = $this->container->get(DataGrip::class)->get('rpc');
|
||||
}
|
||||
|
||||
|
||||
@@ -212,13 +218,15 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
private function dispatch($data): array
|
||||
{
|
||||
try {
|
||||
[$handler, $params] = $this->getContainer()->get(RpcManager::class)->get($data['service'], $data['method']);
|
||||
if (is_null($handler)) {
|
||||
$handler = $this->collector->find($data['service'], 'GET');
|
||||
if (is_integer($handler) || is_null($handler)) {
|
||||
throw new \Exception('Method not found', -32601);
|
||||
} else {
|
||||
$params = $this->container->getMethodParameters($handler->callback::class, $data['method']);
|
||||
|
||||
Context::setContext(RequestInterface::class, $this->createServerRequest($params));
|
||||
|
||||
return $this->handler($handler);
|
||||
return $this->handler($handler, $data['method'], $params);
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
$code = $throwable->getCode() == 0 ? -32603 : $throwable->getCode();
|
||||
@@ -240,13 +248,16 @@ class RpcJsonp extends Component implements OnConnectInterface, OnReceiveInterfa
|
||||
|
||||
/**
|
||||
* @param Handler $handler
|
||||
* @param string $method
|
||||
* @param $params
|
||||
* @return array
|
||||
*/
|
||||
private function handler(Handler $handler): array
|
||||
private function handler(Handler $handler, string $method, $params): array
|
||||
{
|
||||
$result = call_user_func([$handler->callback, $method], ...$params);
|
||||
return [
|
||||
'jsonrpc' => '2.0',
|
||||
'result' => call_user_func($handler->callback, ...$handler->params),
|
||||
'result' => $result,
|
||||
'id' => $data['id'] ?? null
|
||||
];
|
||||
}
|
||||
|
||||
+4
-14
@@ -8,8 +8,7 @@ use Kiri\Abstracts\Component;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Consul\Agent;
|
||||
use Kiri\Consul\Health;
|
||||
use Kiri\Message\Handler\Handler;
|
||||
use ReflectionException;
|
||||
use Kiri\Message\Handler\Router;
|
||||
|
||||
class RpcManager extends Component
|
||||
{
|
||||
@@ -84,24 +83,15 @@ class RpcManager extends Component
|
||||
* @param string $class
|
||||
* @param array $serviceConfig
|
||||
* @return bool
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function add(string $name, string $class, array $serviceConfig): bool
|
||||
{
|
||||
$methods = Kiri::getDi()->getReflect($class);
|
||||
$lists = $methods->getMethods(\ReflectionMethod::IS_PUBLIC);
|
||||
|
||||
if (!isset($this->_rpc[$name])) {
|
||||
$this->_rpc[$name] = ['methods' => [], 'id' => $serviceConfig['ID'], 'config' => $serviceConfig];
|
||||
}
|
||||
|
||||
foreach ($lists as $reflection) {
|
||||
if ($reflection->getDeclaringClass() != $class) {
|
||||
continue;
|
||||
}
|
||||
$methodName = $reflection->getName();
|
||||
$this->_rpc[$name]['methods'][$methodName] = [new Handler('/', [$class, $methodName]), null];
|
||||
}
|
||||
Router::addServer('rpc', static function () use ($name, $class) {
|
||||
Router::get($name, $class);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user