Skip to content

Commit

Permalink
add rpc validator
Browse files Browse the repository at this point in the history
  • Loading branch information
stelin committed Oct 16, 2019
1 parent aaeb9f2 commit 8646fc5
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/rpc-server/src/Contract/RequestInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Swoft\Rpc\Server\Contract;

use Swoft\Rpc\Server\Request;
use Swoole\Server;

/**
Expand Down Expand Up @@ -32,6 +33,18 @@ public function getMethod(): string;
*/
public function getParams(): array;

/**
* @return array
*/
public function getParamsMap(): array;

/**
* @param array $params
*
* @return self
*/
public function withParams(array $params): self;

/**
* @return array
*/
Expand Down
56 changes: 56 additions & 0 deletions src/rpc-server/src/Middleware/ValidatorMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php declare(strict_types=1);


namespace Swoft\Rpc\Server\Middleware;

use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\BeanFactory;
use Swoft\Rpc\Server\Contract\MiddlewareInterface;
use Swoft\Rpc\Server\Contract\RequestHandlerInterface;
use Swoft\Rpc\Server\Contract\RequestInterface;
use Swoft\Rpc\Server\Contract\ResponseInterface;
use Swoft\Rpc\Server\Request;
use Swoft\Rpc\Server\Router\Router;
use Swoft\Validator\Exception\ValidatorException;
use Swoft\Validator\ValidateRegister;
use Swoft\Validator\Validator;

/**
* Class ValidatorMiddleware
*
* @Bean()
*
* @since 2.0
*/
class ValidatorMiddleware implements MiddlewareInterface
{
/**
* @param RequestInterface $request
* @param RequestHandlerInterface $requestHandler
*
* @return ResponseInterface
* @throws ValidatorException
*/
public function process(RequestInterface $request, RequestHandlerInterface $requestHandler): ResponseInterface
{
[$status, $className] = $request->getAttribute(Request::ROUTER_ATTRIBUTE);

if ($status != Router::FOUND) {
return $requestHandler->handle($request);
}

$method = $request->getMethod();
$paramsMap = $request->getParamsMap();
$validates = ValidateRegister::getValidates($className, $method);
if (empty($validates)) {
return $requestHandler->handle($request);
}

/* @var Validator $validator */
$validator = BeanFactory::getBean('validator');
[$paramsMap] = $validator->validateRequest($paramsMap, $validates);

$request = $request->withParams(array_values($paramsMap));
return $requestHandler->handle($request);
}
}
36 changes: 36 additions & 0 deletions src/rpc-server/src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use ReflectionException;
use Swoft\Bean\Annotation\Mapping\Bean;
use Swoft\Bean\BeanFactory;
use Swoft\Bean\Concern\PrototypeTrait;
use Swoft\Bean\Exception\ContainerException;
use Swoft\Rpc\Exception\RpcException;
Expand Down Expand Up @@ -127,6 +128,41 @@ public static function new(
return $instance;
}

/**
* @return array
* @throws ReflectionException
*/
public function getParamsMap(): array
{
$rc = BeanFactory::getReflection($this->interface);
$rxParams = $rc['methods'][$this->method]['params'];

$index = 0;
$paramsMap = [];
foreach ($rxParams as $methodParams) {
if (!isset($this->params[$index])) {
break;
}

[$name] = $methodParams;
$paramsMap[$name] = $this->params[$index];
}
return $paramsMap;
}

/**
* @param array $params
*
* @return RequestInterface
*/
public function withParams(array $params): RequestInterface
{
$clone = clone $this;

$clone->params = $params;
return $clone;
}

/**
* @return string
*/
Expand Down

0 comments on commit 8646fc5

Please sign in to comment.