modify
This commit is contained in:
@@ -1,131 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route;
|
||||
|
||||
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\Exception\AuthException;
|
||||
use HttpServer\Route\Filter\BodyFilter;
|
||||
use HttpServer\Route\Filter\FilterException;
|
||||
use HttpServer\Route\Filter\HeaderFilter;
|
||||
use HttpServer\Route\Filter\QueryFilter;
|
||||
use Exception;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class Filter
|
||||
* @package Snowflake\Snowflake\Route
|
||||
*/
|
||||
class Filter extends HttpService
|
||||
{
|
||||
|
||||
/** @var Filter\Filter[] */
|
||||
private array $_filters = [];
|
||||
|
||||
/** @var array */
|
||||
public array $grant = [];
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
* @return BodyFilter|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setBody(array $value): bool|BodyFilter
|
||||
{
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @var BodyFilter $class */
|
||||
$class = Snowflake::createObject(BodyFilter::class);
|
||||
$class->rules = [];
|
||||
$class->params = Input()->params();
|
||||
|
||||
return $this->_filters[] = $class;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
* @return HeaderFilter|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setHeader(array $value): HeaderFilter|bool
|
||||
{
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @var HeaderFilter $class */
|
||||
$class = Snowflake::createObject(HeaderFilter::class);
|
||||
$class->rules = [];
|
||||
$class->params = request()->headers->getHeaders();
|
||||
|
||||
return $this->_filters[] = $class;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $value
|
||||
* @return QueryFilter|bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function setQuery(array $value): QueryFilter|bool
|
||||
{
|
||||
if (empty($value)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/** @var QueryFilter $class */
|
||||
$class = Snowflake::createObject(QueryFilter::class);
|
||||
$class->rules = [];
|
||||
$class->params = request()->headers->getHeaders();
|
||||
|
||||
return $this->_filters[] = $class;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function handler(): bool
|
||||
{
|
||||
if (($error = $this->filters()) !== true) {
|
||||
throw new FilterException($error);
|
||||
}
|
||||
if (!$this->grant()) {
|
||||
throw new AuthException('Authentication error.');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
private function filters(): bool
|
||||
{
|
||||
if (empty($this->_filters)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->_filters as $filter) {
|
||||
if (!$filter->check()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return mixed
|
||||
*/
|
||||
private function grant(): mixed
|
||||
{
|
||||
if (!is_callable($this->grant, true)) {
|
||||
return true;
|
||||
}
|
||||
return call_user_func($this->grant);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route\Filter;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class BodyFilter
|
||||
* @package Snowflake\Snowflake\Route\Filter
|
||||
*/
|
||||
class BodyFilter extends Filter
|
||||
{
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function check(): bool
|
||||
{
|
||||
return $this->validator();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route\Filter;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use validator\Validator;
|
||||
|
||||
/**
|
||||
* Class Filter
|
||||
* @package Snowflake\Snowflake\Route\Filter
|
||||
*/
|
||||
abstract class Filter extends HttpService
|
||||
{
|
||||
|
||||
public array $rules = [];
|
||||
|
||||
public array $params = [];
|
||||
|
||||
abstract public function check();
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function validator(): bool
|
||||
{
|
||||
$validator = Validator::getInstance();
|
||||
$validator->setParams($this->params);
|
||||
foreach ($this->rules as $val) {
|
||||
$field = array_shift($val);
|
||||
if (empty($val)) {
|
||||
continue;
|
||||
}
|
||||
$validator->make($field, $val);
|
||||
}
|
||||
if (!$validator->validation()) {
|
||||
return $this->addError($validator->getError());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route\Filter;
|
||||
|
||||
|
||||
|
||||
use Throwable;
|
||||
|
||||
class FilterException extends \Exception
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* FilterException constructor.
|
||||
* @param string $message
|
||||
* @param int $code
|
||||
* @param Throwable|null $previous
|
||||
*/
|
||||
public function __construct($message = "", $code = 0, Throwable $previous = null)
|
||||
{
|
||||
parent::__construct($message, $code, $previous);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route\Filter;
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class HeaderFilter
|
||||
* @package Snowflake\Snowflake\Route\Filter
|
||||
*/
|
||||
class HeaderFilter extends Filter
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function check(): bool
|
||||
{
|
||||
return $this->validator();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route\Filter;
|
||||
|
||||
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class QueryFilter
|
||||
* @package Snowflake\Snowflake\Route\Filter
|
||||
*/
|
||||
class QueryFilter extends Filter
|
||||
{
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
public function check(): bool
|
||||
{
|
||||
return $this->validator();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace HttpServer\Route;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
/**
|
||||
* Class TcpListen
|
||||
* @package Snowflake\Snowflake\Route
|
||||
*/
|
||||
class Handler extends HttpService
|
||||
{
|
||||
|
||||
/** @var Router */
|
||||
protected Router $router;
|
||||
|
||||
/**
|
||||
* Listen constructor.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->router = Snowflake::app()->router;
|
||||
|
||||
parent::__construct([]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
* @param $handler
|
||||
*/
|
||||
public function group($config, $handler)
|
||||
{
|
||||
$this->router->group($config, $handler, $this);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $route
|
||||
* @param $handler
|
||||
* @return Handler|Node|null
|
||||
*/
|
||||
public function handler($route, $handler): Handler|Node|null
|
||||
{
|
||||
return $this->router->addRoute($route, $handler, 'receive');
|
||||
}
|
||||
|
||||
}
|
||||
+15
-44
@@ -126,11 +126,11 @@ class Node extends HttpService
|
||||
public function createDispatch(): Closure
|
||||
{
|
||||
return function () {
|
||||
$dispatchParam = Context::getContext('dispatch-param');
|
||||
if (empty($dispatchParam)) {
|
||||
$dispatchParam = [\request()];
|
||||
}
|
||||
return \aop($this->handler, $dispatchParam);
|
||||
$dispatchParam = Context::getContext('dispatch-param');
|
||||
if (empty($dispatchParam)) {
|
||||
$dispatchParam = [\request()];
|
||||
}
|
||||
return \aop($this->handler, $dispatchParam);
|
||||
};
|
||||
}
|
||||
|
||||
@@ -276,31 +276,10 @@ class Node extends HttpService
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return $this->checkRule();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkRule(): bool
|
||||
{
|
||||
if (empty($this->rules)) {
|
||||
return true;
|
||||
}
|
||||
foreach ($this->rules as $rule) {
|
||||
if (!isset($rule['class'])) {
|
||||
$rule['class'] = Filter::class;
|
||||
}
|
||||
/** @var Filter $object */
|
||||
$object = Snowflake::createObject($rule);
|
||||
if (!$object->handler()) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $controller
|
||||
* @param string $action
|
||||
@@ -518,19 +497,11 @@ class Node extends HttpService
|
||||
*/
|
||||
public function dispatch(): mixed
|
||||
{
|
||||
try {
|
||||
Context::setContext('dispatch-param', func_get_args());
|
||||
if (empty($this->callback)) {
|
||||
return Json::to(404, $this->errorMsg());
|
||||
}
|
||||
return $this->httpFilter();
|
||||
} catch (Throwable $throwable) {
|
||||
$this->addError($throwable, 'throwable');
|
||||
|
||||
$code = $throwable->getCode() == 0 ? 500 : $throwable->getCode();
|
||||
|
||||
return Json::to($code, $throwable->getMessage());
|
||||
Context::setContext('dispatch-param', func_get_args());
|
||||
if (empty($this->callback)) {
|
||||
return Json::to(404, $this->errorMsg());
|
||||
}
|
||||
return $this->httpFilter();
|
||||
}
|
||||
|
||||
|
||||
@@ -548,11 +519,11 @@ class Node extends HttpService
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $dispatchParams
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
/**
|
||||
* @param $dispatchParams
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private function runValidator($dispatchParams): mixed
|
||||
{
|
||||
/** @var HttpFilter $filter */
|
||||
|
||||
+47
-52
@@ -13,63 +13,58 @@ class Reduce
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $last
|
||||
* @param $middleWares
|
||||
* @return mixed
|
||||
*/
|
||||
public static function reduce($last, $middleWares): mixed
|
||||
{
|
||||
return array_reduce(array_reverse($middleWares), static::core(), $last);
|
||||
}
|
||||
/**
|
||||
* @param $last
|
||||
* @param $middleWares
|
||||
* @return mixed
|
||||
*/
|
||||
public static function reduce($last, $middleWares): mixed
|
||||
{
|
||||
return array_reduce(array_reverse($middleWares), static::core(), $last);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $middleWares
|
||||
* @return mixed
|
||||
*/
|
||||
public static function after($middleWares): mixed
|
||||
{
|
||||
return array_reduce(array_reverse($middleWares), function ($stack, $pipe) {
|
||||
return function ($request, $passable) use ($stack, $pipe) {
|
||||
try {
|
||||
if (!($pipe instanceof After)) {
|
||||
return call_user_func($pipe, $request, $passable, $stack);
|
||||
}
|
||||
return $pipe->onHandler($request, $passable);
|
||||
} catch (\Throwable $throwable) {
|
||||
logger()->addError($throwable, 'throwable');
|
||||
return Json::to(0, $throwable);
|
||||
}
|
||||
};
|
||||
});
|
||||
}
|
||||
/**
|
||||
* @param $middleWares
|
||||
* @return mixed
|
||||
*/
|
||||
public static function after($middleWares): mixed
|
||||
{
|
||||
return array_reduce(array_reverse($middleWares), function ($stack, $pipe) {
|
||||
return function ($request, $passable) use ($stack, $pipe) {
|
||||
if (!($pipe instanceof After)) {
|
||||
return call_user_func($pipe, $request, $passable, $stack);
|
||||
}
|
||||
return $pipe->onHandler($request, $passable);
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return Closure
|
||||
*/
|
||||
private static function core(): Closure
|
||||
{
|
||||
return function ($stack, $pipe) {
|
||||
return static::passable($stack, $pipe);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @return Closure
|
||||
*/
|
||||
private static function core(): Closure
|
||||
{
|
||||
return function ($stack, $pipe) {
|
||||
return static::passable($stack, $pipe);
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $stack
|
||||
* @param $pipe
|
||||
* @return Closure
|
||||
*/
|
||||
public static function passable($stack, $pipe): Closure
|
||||
{
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if ($pipe instanceof Middleware) {
|
||||
return $pipe->onHandler($passable, $stack);
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}
|
||||
/**
|
||||
* @param $stack
|
||||
* @param $pipe
|
||||
* @return Closure
|
||||
*/
|
||||
public static function passable($stack, $pipe): Closure
|
||||
{
|
||||
return function ($passable) use ($stack, $pipe) {
|
||||
if ($pipe instanceof Middleware) {
|
||||
return $pipe->onHandler($passable, $stack);
|
||||
}
|
||||
return call_user_func($pipe, $passable, $stack);
|
||||
};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+574
-593
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user