2021-03-23 15:13:33 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace Annotation\Rpc;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
use Annotation\Attribute;
|
2021-03-23 17:00:17 +08:00
|
|
|
use Exception;
|
2021-03-23 15:13:33 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class RpcClient
|
|
|
|
|
* @package Annotation\Rpc
|
|
|
|
|
*/
|
2021-03-24 11:26:46 +08:00
|
|
|
#[\Attribute(\Attribute::TARGET_CLASS)] class RpcClient extends Attribute
|
2021-03-23 15:13:33 +08:00
|
|
|
{
|
|
|
|
|
|
2021-03-23 16:14:05 +08:00
|
|
|
private array $config;
|
|
|
|
|
|
2021-03-23 15:13:33 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* RpcClient constructor.
|
|
|
|
|
* @param string $cmd
|
|
|
|
|
* @param int $port
|
2021-03-23 16:49:35 +08:00
|
|
|
* @param int $timeout
|
2021-03-23 15:13:33 +08:00
|
|
|
* @param int $mode
|
|
|
|
|
*/
|
|
|
|
|
public function __construct(
|
|
|
|
|
public string $cmd,
|
|
|
|
|
public int $port,
|
2021-03-24 15:06:02 +08:00
|
|
|
public int $timeout,
|
|
|
|
|
public int $mode
|
2021-03-23 15:13:33 +08:00
|
|
|
)
|
|
|
|
|
{
|
2021-03-24 11:25:02 +08:00
|
|
|
$this->config = ['port' => $port, 'mode' => $mode, 'timeout' => $timeout];
|
2021-03-23 15:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param array $handler
|
|
|
|
|
* @return mixed
|
2021-03-23 17:00:17 +08:00
|
|
|
* @throws Exception
|
2021-03-23 15:13:33 +08:00
|
|
|
*/
|
|
|
|
|
public function execute(array $handler): mixed
|
|
|
|
|
{
|
|
|
|
|
$rpc = Snowflake::app()->getRpc();
|
2021-03-23 16:14:05 +08:00
|
|
|
$rpc->addProducer($this->cmd, $handler, $this->config);
|
2021-03-23 17:00:17 +08:00
|
|
|
|
2021-03-23 16:14:05 +08:00
|
|
|
return parent::execute($handler);
|
2021-03-23 15:13:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|