This commit is contained in:
2021-10-28 10:52:09 +08:00
parent e1fcefde2d
commit ec75ccfc00
5 changed files with 93 additions and 14 deletions
+17 -2
View File
@@ -2,7 +2,11 @@
namespace Kiri\Rpc\Annotation; namespace Kiri\Rpc\Annotation;
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc use Annotation\Attribute;
use Http\Handler\Router;
use Kiri\Kiri;
#[\Attribute(\Attribute::TARGET_CLASS)] class JsonRpc extends Attribute
{ {
@@ -10,10 +14,21 @@ namespace Kiri\Rpc\Annotation;
* @param string $method * @param string $method
* @param string $version * @param string $version
*/ */
public function __construct(public string $method, public string $version = '2.0') public function __construct(public string $method, public string $version = '2.0', public string $protocol = 'json')
{ {
} }
/**
* @param mixed $class
* @param mixed|string $method
* @return mixed
*/
public function execute(mixed $class, mixed $method = ''): mixed
{
return parent::execute($class, $method); // TODO: Change the autogenerated stub
}
} }
+44
View File
@@ -0,0 +1,44 @@
<?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
}
}
+3
View File
@@ -5,4 +5,7 @@ namespace Kiri\Rpc;
interface OnRpcConsumerInterface interface OnRpcConsumerInterface
{ {
public function execute();
} }
+20
View File
@@ -0,0 +1,20 @@
<?php
namespace Kiri\Rpc;
class RpcManager
{
public static function add()
{
}
public static function get()
{
}
}
+9 -12
View File
@@ -3,31 +3,28 @@
namespace Kiri\Rpc; namespace Kiri\Rpc;
use Annotation\Target;
use Http\Constrict\RequestInterface; use Http\Constrict\RequestInterface;
use Http\Handler\Controller;
use Kiri\Rpc\Annotation\JsonRpc; use Kiri\Rpc\Annotation\JsonRpc;
/** /**
* *
*/ */
#[JsonRpc(method: 'test.service', version: '2.0')] #[Target]
class TestRpcService implements OnJsonRpcInterface #[JsonRpc(method: 'test', version: '2.0')]
class TestRpcService extends Controller implements OnJsonRpcInterface
{ {
/** /**
* @var RequestInterface
*/
public RequestInterface $request;
/**
* @param int $i
* @param int $b
* @return int * @return int
*/ */
public function execute(int $i, int $b): int public function execute(): int
{ {
return $i + $b; return $this->request->int('a') + $this->request->int('b');
} }
} }