This commit is contained in:
2021-04-07 11:42:43 +08:00
parent 83cb712961
commit 2840a0c956
2 changed files with 548 additions and 525 deletions
+3 -4
View File
@@ -9,7 +9,6 @@ use Exception;
use HttpServer\Http\Request;
use HttpServer\Route\Router;
use JetBrains\PhpStorm\Pure;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
@@ -32,7 +31,7 @@ use Snowflake\Snowflake;
* @param string $protocol
* @param int $port
*/
#[Pure] public function __construct(public string $cmd, public string $protocol, public 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, '/');
}
@@ -41,7 +40,6 @@ use Snowflake\Snowflake;
/**
* @param array $handler
* @return Router
* @throws ConfigException
* @throws Exception
*/
public function execute(array $handler): Router
@@ -49,7 +47,8 @@ use Snowflake\Snowflake;
// TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter();
$router->addRoute($this->uri, $handler, Request::HTTP_CMD);
$router->addRoute($this->uri, $handler, Request::HTTP_CMD)
->setDataType($this->protocol);
return $router;
}
+31 -7
View File
@@ -6,22 +6,17 @@ namespace HttpServer\Route;
use Annotation\Route\Middleware;
use Annotation\Route\RpcProducer;
use Closure;
use Exception;
use HttpServer\Abstracts\HttpService;
use HttpServer\Controller;
use HttpServer\Http\Request;
use Exception;
use HttpServer\HttpFilter;
use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits;
use HttpServer\Route\Dispatch\Dispatch;
use JetBrains\PhpStorm\Pure;
use ReflectionException;
use Snowflake\Core\Json;
use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Throwable;
@@ -48,6 +43,9 @@ class Node extends HttpService
public array $rules = [];
private string $_dataType = '';
/** @var ?Closure|?array */
public Closure|array|null $handler;
public string $htmlSuffix = '.html';
@@ -64,6 +62,32 @@ class Node extends HttpService
private array $_after = [];
private array $_limits = [];
/**
* @param string $dataType
*/
public function setDataType(string $dataType)
{
$this->_dataType;
}
/**
* @param string $data
* @return mixed
*/
public function unpack(string $data): mixed
{
if ($this->_dataType == RpcProducer::PROTOCOL_JSON) {
return json_decode($data, true);
}
if ($this->_dataType == RpcProducer::PROTOCOL_SERIALIZE) {
return unserialize($data);
}
return $data;
}
/**
* @param $handler
* @return Node