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

58 lines
1.1 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;
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
{
private string $uri = '';
2021-04-07 11:32:29 +08:00
const PROTOCOL_JSON = 'json';
const PROTOCOL_SERIALIZE = 'serialize';
2021-03-23 15:21:12 +08:00
/**
* Route constructor.
* @param string $cmd
2021-04-07 11:30:30 +08:00
* @param string $protocol
2021-03-23 15:21:12 +08:00
* @param int $port
*/
2021-04-07 11:42:43 +08:00
#[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-03-23 18:09:15 +08:00
$this->uri = 'rpc/p' . $this->port . '/' . ltrim($this->cmd, '/');
2021-03-23 15:21:12 +08:00
}
/**
* @param array $handler
* @return Router
* @throws Exception
*/
public function execute(array $handler): Router
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
2021-04-07 11:42:43 +08:00
$router->addRoute($this->uri, $handler, Request::HTTP_CMD)
->setDataType($this->protocol);
2021-03-23 15:21:12 +08:00
return $router;
}
}