This commit is contained in:
2021-10-28 17:31:18 +08:00
parent cb3397db17
commit 1955aff8d9
4 changed files with 9 additions and 96 deletions
-44
View File
@@ -1,44 +0,0 @@
<?php
namespace Kiri\Rpc\Annotation;
use Annotation\Attribute;
use Http\Handler\Router;
use Kiri\Kiri;
#[\Attribute(\Attribute::TARGET_METHOD)] class JsonRpcMethod extends Attribute
{
/**
* @param string $method
*/
public function __construct(public string $method)
{
}
/**
* @param mixed $class
* @param mixed|string $method
* @return mixed
* @throws \ReflectionException
*/
public function execute(mixed $class, mixed $method = ''): mixed
{
$reflect = Kiri::getDi()->getReflectMethod($class, $method);
$parent = $reflect->getDeclaringClass()->getAttributes(JsonRpc::class);
if (empty($parent)) {
return parent::execute($class, $method);
}
/** @var JsonRpc $attribute */
$attribute = $parent[0]->newInstance();
Router::addService($attribute->service, function () use ($class, $method) {
Router::jsonp($this->method, [di($class), $method]);
}, $attribute->version);
return parent::execute($class, $method); // TODO: Change the autogenerated stub
}
}
-42
View File
@@ -1,42 +0,0 @@
<?php
namespace Kiri\Rpc\Annotation;
use Annotation\Attribute;
use Kiri\Kiri;
use Kiri\Rpc\RpcManager;
use ReflectionException;
/**
*
*/
#[\Attribute(\Attribute::TARGET_CLASS)] class RpcProduct extends Attribute
{
/**
* @param string $cmd
* @param string $protocol
*/
public function __construct(public string $cmd, public string $protocol)
{
}
/**
* @param mixed $class
* @param mixed|string $method
* @return mixed
*/
public function execute(mixed $class, mixed $method = ''): mixed
{
$class = Kiri::getDi()->get($class);
RpcManager::addCmdHandler($this->cmd, [$class, $method], $this->protocol);
return parent::execute($class, $method); // TODO: Change the autogenerated stub
}
}
+9 -8
View File
@@ -23,29 +23,30 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
public Pool $pool;
protected string $name = '';
/**
* @param string $service
* @param string $method
* @param mixed $data
* @param string $version
* @throws Exception
*/
public function notify(string $service, string $method, mixed $data, string $version = '2.0'): void
public function notify(string $method, mixed $data, string $version = '2.0'): void
{
$config = $this->get_consul($service);
$config = $this->get_consul($this->name);
if (Context::inCoroutine()) {
$client = $this->clientOnCoroutine($config);
} else {
$client = $this->clientNotCoroutine($config);
}
$client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data]));
$client->send(json_encode(['jsonrpc' => $version, 'service' => $this->name, 'method' => $method, 'params' => $data]));
$client->recv(1);
$client->close();
}
/**
* @param string $service
* @param string $method
* @param mixed $data
* @param string $version
@@ -53,9 +54,9 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
* @return mixed
* @throws Exception
*/
public function get(string $service, string $method, mixed $data, string $version = '2.0', string $id = ''): mixed
public function get(string $method, mixed $data, string $version = '2.0', string $id = ''): mixed
{
$config = $this->get_consul($service);
$config = $this->get_consul($this->name);
if (Context::inCoroutine()) {
$client = $this->clientOnCoroutine($config);
} else {
@@ -64,7 +65,7 @@ abstract class JsonRpcConsumers implements OnRpcConsumerInterface
if (empty($id)) $id = Number::create(time());
$client->send(json_encode(['jsonrpc' => $version, 'service' => $service, 'method' => $method, 'params' => $data, 'id' => $id]));
$client->send(json_encode(['jsonrpc' => $version, 'service' => $this->name, 'method' => $method, 'params' => $data, 'id' => $id]));
$read = $client->recv();
$client->close();
return json_decode($read, true);
-2
View File
@@ -26,8 +26,6 @@ class RpcManager
foreach ($lists as $reflection) {
$methodName = $reflection->getName();
//
// $params = Kiri::getDi()->getMethodParameters($class, $methodName);
static::$_rpc[$name][$methodName] = [[$class, $methodName], null];
}