This commit is contained in:
as2252258@163.com
2021-07-11 03:57:25 +08:00
parent 27dc21e24e
commit 12d547c1a2
13 changed files with 1038 additions and 816 deletions
+28 -28
View File
@@ -9,6 +9,7 @@ use Exception;
use HttpServer\Http\Request;
use HttpServer\Route\Router;
use JetBrains\PhpStorm\Pure;
use Rpc\Actuator;
use Snowflake\Snowflake;
@@ -19,39 +20,38 @@ use Snowflake\Snowflake;
#[\Attribute(\Attribute::TARGET_METHOD)] class RpcProducer extends Attribute
{
private string $uri = '';
const PROTOCOL_JSON = 'json';
const PROTOCOL_SERIALIZE = 'serialize';
const PROTOCOL_JSON = 'json';
const PROTOCOL_SERIALIZE = 'serialize';
/**
* 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)
{
$this->uri = 'rpc/p' . $this->port . '/' . ltrim($this->cmd, '/');
}
/**
* 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)
{
}
/**
* @param array $handler
* @return Router
* @throws Exception
*/
/**
* @param array $handler
* @return Router
* @throws Exception
*/
public function execute(mixed $class, mixed $method = null): Router
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$router->addRoute($this->uri, [$class, $method], Request::HTTP_CMD)
->setDataType($this->protocol);
return $router;
}
{
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$cmd = $this->cmd;
$callback = function (Actuator $actuator) use ($cmd, $class, $method) {
$actuator->addListener($cmd, $class . '@' . $method);
};
$router->addRpcService($this->port, $callback);
return $router;
}
}