This commit is contained in:
2021-03-19 16:32:34 +08:00
parent fc18533f7c
commit 0c98f8858d
5 changed files with 112 additions and 5 deletions
+1
View File
@@ -35,6 +35,7 @@ class Loader extends BaseObject
/** /**
* @param $path * @param $path
* @param $namespace * @param $namespace
* @throws ComponentException
*/ */
public function loader($path, $namespace) public function loader($path, $namespace)
{ {
+16 -3
View File
@@ -5,6 +5,11 @@ namespace Annotation\Route;
use Annotation\Attribute; use Annotation\Attribute;
use HttpServer\HttpFilter;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
/** /**
* Class Filter * Class Filter
@@ -15,9 +20,9 @@ use Annotation\Attribute;
/** /**
* Filter constructor. * Filter constructor.
* @param string $uri * @param array $rules
*/ */
public function __construct(public string $uri) public function __construct(public array $rules)
{ {
} }
@@ -25,10 +30,18 @@ use Annotation\Attribute;
/** /**
* @param array $handler * @param array $handler
* @return array * @return array
* @throws ReflectionException
* @throws ComponentException
* @throws NotFindClassException
*/ */
public function execute(array $handler): array public function execute(array $handler): array
{ {
// TODO: Implement execute() method. [$class, $method] = $handler;
/** @var HttpFilter $filter */
$filter = Snowflake::app()->get('filter');
$filter->register(get_class($class), $method, $this->rules);
return $handler; return $handler;
} }
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace HttpServer;
use Exception;
use Snowflake\Abstracts\BaseObject;
use validator\Validator;
/**
* Class HttpFilter
* @package HttpServer\Http
*/
class HttpFilter extends BaseObject
{
private array $hash = [];
/**
* @param string $className
* @param string $method
* @param array $rules
* @return $this
*/
public function register(string $className, string $method, array $rules): HttpFilter
{
$this->hash[$className . '::' . $method] = $rules;
return $this;
}
/**
* @param string $className
* @param string $method
* @return bool
* @throws Exception
*/
public function check(string $className, string $method): bool|Validator
{
if (!isset($this->hash[$className . '::' . $method])) {
return true;
}
$rules = $this->hash[$className . '::' . $method][request()->getMethod()] ?? [];
if (empty($rules)) {
return true;
}
$validator = Validator::getInstance();
$validator->setParams(Input()->load());
foreach ($rules as $Key => $val) {
$field = array_shift($val);
if (empty($val)) {
continue;
}
$validator->make($field, $val);
}
return $validator;
}
}
+27
View File
@@ -12,6 +12,7 @@ use HttpServer\Controller;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use Exception; use Exception;
use HttpServer\HttpFilter;
use HttpServer\IInterface\After; use HttpServer\IInterface\After;
use HttpServer\IInterface\Interceptor; use HttpServer\IInterface\Interceptor;
use HttpServer\IInterface\Limits; use HttpServer\IInterface\Limits;
@@ -24,6 +25,7 @@ use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Throwable; use Throwable;
use validator\Validator;
use function Input; use function Input;
/** /**
@@ -563,6 +565,31 @@ class Node extends HttpService
if (empty($this->callback)) { if (empty($this->callback)) {
return Json::to(404, $this->errorMsg()); return Json::to(404, $this->errorMsg());
} }
return $this->httpFilter(...func_get_args());
}
/**
* @return mixed
* @throws ComponentException
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function httpFilter(): mixed
{
if (!is_array($this->handler)) {
return $this->runWith(...func_get_args());
}
[$class, $method] = $this->handler;
/** @var HttpFilter $filter */
$filter = Snowflake::app()->get('filter');
$validator = $filter->check(get_class($class), $method);
if ($validator instanceof Validator && !$validator->validation()) {
return Json::to(401, $validator->getError());
}
return $this->runWith(...func_get_args()); return $this->runWith(...func_get_args());
} }
+4 -2
View File
@@ -14,6 +14,7 @@ use Exception;
use HttpServer\Client\Http2; use HttpServer\Client\Http2;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Http\Response; use HttpServer\Http\Response;
use HttpServer\HttpFilter;
use HttpServer\Route\Router; use HttpServer\Route\Router;
use HttpServer\Server; use HttpServer\Server;
@@ -292,10 +293,10 @@ abstract class BaseApplication extends Service
/** /**
* @throws ComponentException * @return SRedis
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
* @return SRedis * @throws ComponentException
*/ */
public function getRedisFromPool(): SRedis public function getRedisFromPool(): SRedis
{ {
@@ -443,6 +444,7 @@ abstract class BaseApplication extends Service
'redis' => ['class' => Redis::class], 'redis' => ['class' => Redis::class],
'jwt' => ['class' => Jwt::class], 'jwt' => ['class' => Jwt::class],
'async' => ['class' => Async::class], 'async' => ['class' => Async::class],
'filter' => ['class' => HttpFilter::class],
'object' => ['class' => ObjectPool::class], 'object' => ['class' => ObjectPool::class],
'goto' => ['class' => BaseGoto::class], 'goto' => ['class' => BaseGoto::class],
'http2' => ['class' => Http2::class], 'http2' => ['class' => Http2::class],