45 lines
1006 B
PHP
45 lines
1006 B
PHP
<?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
|
|
}
|
|
|
|
|
|
}
|