改名
This commit is contained in:
@@ -2,7 +2,11 @@
|
||||
|
||||
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 $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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -5,4 +5,7 @@ namespace Kiri\Rpc;
|
||||
interface OnRpcConsumerInterface
|
||||
{
|
||||
|
||||
|
||||
public function execute();
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Rpc;
|
||||
|
||||
class RpcManager
|
||||
{
|
||||
|
||||
|
||||
public static function add()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
public static function get()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
+9
-12
@@ -3,31 +3,28 @@
|
||||
namespace Kiri\Rpc;
|
||||
|
||||
|
||||
use Annotation\Target;
|
||||
use Http\Constrict\RequestInterface;
|
||||
use Http\Handler\Controller;
|
||||
use Kiri\Rpc\Annotation\JsonRpc;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
#[JsonRpc(method: 'test.service', version: '2.0')]
|
||||
class TestRpcService implements OnJsonRpcInterface
|
||||
#[Target]
|
||||
#[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
|
||||
*/
|
||||
public function execute(int $i, int $b): int
|
||||
public function execute(): int
|
||||
{
|
||||
return $i + $b;
|
||||
return $this->request->int('a') + $this->request->int('b');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user