Files
kiri-core/Annotation/Route/RpcProducer.php
T

58 lines
1.2 KiB
PHP
Raw Normal View History

2021-03-23 15:21:12 +08:00
<?php
namespace Annotation\Route;
use Annotation\Attribute;
use Exception;
use HttpServer\Http\Request;
use HttpServer\Route\Router;
use JetBrains\PhpStorm\Pure;
2021-07-11 03:57:25 +08:00
use Rpc\Actuator;
2021-03-23 15:21:12 +08:00
use Snowflake\Snowflake;
2021-03-30 11:25:44 +08:00
/**
* Class RpcProducer
* @package Annotation\Route
*/
2021-03-24 11:26:46 +08:00
#[\Attribute(\Attribute::TARGET_METHOD)] class RpcProducer extends Attribute
2021-03-23 15:21:12 +08:00
{
2021-07-11 03:57:25 +08:00
const PROTOCOL_JSON = 'json';
const PROTOCOL_SERIALIZE = 'serialize';
2021-03-23 15:21:12 +08:00
2021-04-07 11:32:29 +08:00
2021-07-11 03:57:25 +08:00
/**
* Route constructor.
* @param string $cmd
* @param string $protocol
* @param int $port
*/
#[Pure] public function __construct(public string $cmd, public string $protocol = self::PROTOCOL_SERIALIZE, public int $port = 443)
{
}
2021-03-23 15:21:12 +08:00
2021-07-12 17:17:36 +08:00
/**
* @param mixed $class
* @param mixed|null $method
* @return Router
* @throws Exception
*/
2021-05-03 03:48:52 +08:00
public function execute(mixed $class, mixed $method = null): Router
2021-07-11 03:57:25 +08:00
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$cmd = $this->cmd;
$callback = function (Actuator $actuator) use ($cmd, $class, $method) {
2021-07-11 04:03:00 +08:00
$actuator->addListener($cmd, $class::class . '@' . $method);
2021-07-11 03:57:25 +08:00
};
$router->addRpcService($this->port, $callback);
return $router;
}
2021-03-23 15:21:12 +08:00
}