2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace HttpServer\Route;
|
|
|
|
|
|
|
|
|
|
|
2021-04-20 00:40:10 +08:00
|
|
|
use Annotation\Route\After;
|
|
|
|
|
use Annotation\Route\Interceptor;
|
|
|
|
|
use Annotation\Route\Limits;
|
2021-03-05 17:35:00 +08:00
|
|
|
use Annotation\Route\Middleware;
|
2021-04-07 11:42:43 +08:00
|
|
|
use Annotation\Route\RpcProducer;
|
2020-09-04 17:51:10 +08:00
|
|
|
use Closure;
|
2021-04-07 11:42:43 +08:00
|
|
|
use Exception;
|
2021-02-20 17:33:28 +08:00
|
|
|
use HttpServer\Abstracts\HttpService;
|
2021-03-05 18:03:18 +08:00
|
|
|
use HttpServer\Controller;
|
2020-08-31 01:27:08 +08:00
|
|
|
use HttpServer\Http\Request;
|
2021-03-19 16:32:34 +08:00
|
|
|
use HttpServer\HttpFilter;
|
2021-02-08 17:20:43 +08:00
|
|
|
use JetBrains\PhpStorm\Pure;
|
2020-12-24 11:12:23 +08:00
|
|
|
use Snowflake\Core\Json;
|
2020-08-31 12:38:32 +08:00
|
|
|
use Snowflake\Snowflake;
|
2020-12-15 19:17:42 +08:00
|
|
|
use Throwable;
|
2021-04-19 17:15:51 +08:00
|
|
|
use validator\Validator;
|
2020-12-15 19:17:42 +08:00
|
|
|
use function Input;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class Node
|
2020-08-31 12:38:32 +08:00
|
|
|
* @package Snowflake\Snowflake\Route
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-02-20 17:33:28 +08:00
|
|
|
class Node extends HttpService
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
2021-04-20 11:42:24 +08:00
|
|
|
public string $path = '';
|
|
|
|
|
public int $index = 0;
|
|
|
|
|
public string $method = '';
|
|
|
|
|
|
|
|
|
|
/** @var Node[] $childes */
|
|
|
|
|
public array $childes = [];
|
|
|
|
|
|
|
|
|
|
public array $group = [];
|
|
|
|
|
|
|
|
|
|
private string $_error = '';
|
|
|
|
|
|
|
|
|
|
public array $rules = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private string $_dataType = '';
|
|
|
|
|
|
|
|
|
|
/** @var ?Closure|?array */
|
|
|
|
|
public Closure|array|null $handler;
|
|
|
|
|
public string $htmlSuffix = '.html';
|
|
|
|
|
public bool $enableHtmlSuffix = false;
|
|
|
|
|
public array $namespace = [];
|
|
|
|
|
public array $middleware = [];
|
|
|
|
|
|
|
|
|
|
/** @var array|Closure */
|
|
|
|
|
public Closure|array $callback = [];
|
|
|
|
|
|
|
|
|
|
private string $_alias = '';
|
|
|
|
|
|
|
|
|
|
private array $_interceptors = [];
|
|
|
|
|
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
|
|
|
|
|
* @throws
|
|
|
|
|
*/
|
|
|
|
|
public function bindHandler($handler): static
|
|
|
|
|
{
|
|
|
|
|
if (is_string($handler) && str_contains($handler, '@')) {
|
|
|
|
|
list($controller, $action) = explode('@', $handler);
|
|
|
|
|
if (!empty($this->namespace)) {
|
|
|
|
|
$controller = implode('\\', $this->namespace) . '\\' . $controller;
|
|
|
|
|
}
|
|
|
|
|
$this->handler = $this->getReflect($controller, $action);
|
|
|
|
|
} else if ($handler != null && !is_callable($handler, true)) {
|
|
|
|
|
$this->_error = 'Controller is con\'t exec.';
|
|
|
|
|
} else if ($handler instanceof Closure) {
|
|
|
|
|
$this->handler = $handler;
|
|
|
|
|
} else {
|
|
|
|
|
[$controller, $action] = $this->handler = $handler;
|
|
|
|
|
if (!($controller instanceof Controller)) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
$this->annotationInject(get_class($controller), $action);
|
|
|
|
|
}
|
|
|
|
|
if (!empty($this->handler)) {
|
2021-04-20 11:45:39 +08:00
|
|
|
$this->callback = Reduce::reduce(function () {
|
2021-04-20 11:48:56 +08:00
|
|
|
return call_user_func($this->handler, func_get_args());
|
2021-04-20 11:45:39 +08:00
|
|
|
}, $this->annotation());
|
2021-04-20 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return Closure
|
|
|
|
|
*/
|
|
|
|
|
public function createDispatch(): Closure
|
|
|
|
|
{
|
|
|
|
|
return function () {
|
2021-04-20 11:48:56 +08:00
|
|
|
return call_user_func($this->handler, func_get_args());
|
2021-04-20 11:42:24 +08:00
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function annotation(): array
|
|
|
|
|
{
|
|
|
|
|
$middleWares = $this->getMiddleWares();
|
|
|
|
|
$middleWares = $this->annotation_limit($this, $middleWares);
|
|
|
|
|
$middleWares = $this->annotation_interceptor($this, $middleWares);
|
|
|
|
|
return $middleWares;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $middleWares
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function annotation_interceptor(Node $node, $middleWares = []): array
|
|
|
|
|
{
|
|
|
|
|
if (!$node->hasInterceptor()) {
|
|
|
|
|
return $middleWares;
|
|
|
|
|
}
|
|
|
|
|
foreach ($node->getInterceptor() as $item) {
|
|
|
|
|
$middleWares[] = $item;
|
|
|
|
|
}
|
|
|
|
|
return $middleWares;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param $middleWares
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
protected function annotation_limit(Node $node, $middleWares = []): array
|
|
|
|
|
{
|
|
|
|
|
if (!$node->hasLimits()) {
|
|
|
|
|
return $middleWares;
|
|
|
|
|
}
|
|
|
|
|
foreach ($node->getLimits() as $item) {
|
|
|
|
|
$middleWares[] = $item;
|
|
|
|
|
}
|
|
|
|
|
return $middleWares;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function hasInterceptor(): bool
|
|
|
|
|
{
|
|
|
|
|
return count($this->_interceptors) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function hasLimits(): bool
|
|
|
|
|
{
|
|
|
|
|
return count($this->_limits) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param null $response
|
|
|
|
|
* @return mixed
|
2021-04-20 11:45:39 +08:00
|
|
|
* @throws Exception
|
2021-04-20 11:42:24 +08:00
|
|
|
*/
|
|
|
|
|
public function afterDispatch($response = null): mixed
|
|
|
|
|
{
|
|
|
|
|
if (is_object($this->_after[0])) {
|
|
|
|
|
return call_user_func($this->_after, \request(), $response);
|
|
|
|
|
}
|
|
|
|
|
foreach ($this->_after as $value) {
|
|
|
|
|
call_user_func($value, \request(), $response);
|
|
|
|
|
}
|
|
|
|
|
return $this->_after;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getInterceptor(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_interceptors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getAfters(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_after;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
#[Pure] public function hasAfter(): bool
|
|
|
|
|
{
|
|
|
|
|
return count($this->_after) > 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getLimits(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->_limits;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $request
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function methodAllow(Request $request): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->method == $request->getMethod()) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return $this->method == 'any';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return bool
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function checkSuffix(): bool
|
|
|
|
|
{
|
|
|
|
|
if ($this->enableHtmlSuffix) {
|
|
|
|
|
$url = request()->getUri();
|
|
|
|
|
$nowLength = strlen($this->htmlSuffix);
|
|
|
|
|
if (strpos($url, $this->htmlSuffix) !== strlen($url) - $nowLength) {
|
|
|
|
|
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
|
|
|
|
|
* @return null|array
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function getReflect(string $controller, string $action): ?array
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
$reflect = Snowflake::getDi()->getReflect($controller);
|
|
|
|
|
if (empty($reflect)) {
|
|
|
|
|
throw new Exception($controller . ' Class is con\'t Instantiable.');
|
|
|
|
|
}
|
|
|
|
|
if (!empty($action) && !$reflect->hasMethod($action)) {
|
|
|
|
|
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->annotationInject($reflect->getName(), $action);
|
|
|
|
|
|
|
|
|
|
return [$reflect->newInstance(), $action];
|
|
|
|
|
} catch (Throwable $exception) {
|
|
|
|
|
$this->_error = $exception->getMessage();
|
|
|
|
|
$this->addError($exception, 'router');
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Closure|array|string $handler
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function addInterceptor(Closure|string|array $handler)
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($handler) || is_object($handler[0])) {
|
|
|
|
|
$handler = [$handler];
|
|
|
|
|
}
|
|
|
|
|
foreach ($handler as $closure) {
|
|
|
|
|
if (in_array($closure, $this->_interceptors)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->_interceptors[] = $closure;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $className
|
|
|
|
|
* @param string $action
|
|
|
|
|
* @return Node
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function annotationInject(string $className, string $action): Node
|
|
|
|
|
{
|
|
|
|
|
$annotation = annotation()->getMethods($className, $action);
|
|
|
|
|
if (empty($annotation)) {
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
foreach ($annotation as $name => $attribute) {
|
|
|
|
|
if ($attribute instanceof Interceptor) {
|
|
|
|
|
$this->addInterceptor($attribute->interceptor);
|
|
|
|
|
}
|
|
|
|
|
if ($attribute instanceof After) {
|
|
|
|
|
$this->addAfter($attribute->after);
|
|
|
|
|
}
|
|
|
|
|
if ($attribute instanceof Middleware) {
|
|
|
|
|
$this->addMiddleware($attribute->middleware);
|
|
|
|
|
}
|
|
|
|
|
if ($attribute instanceof Limits) {
|
|
|
|
|
$this->addLimits($attribute->limits);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Closure|array|string $handler
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function addAfter(Closure|string|array $handler)
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($handler) || is_object($handler[0])) {
|
|
|
|
|
$handler = [$handler];
|
|
|
|
|
}
|
|
|
|
|
foreach ($handler as $closure) {
|
|
|
|
|
if (in_array($closure, $this->_after)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->_after[] = $closure;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Closure|array|string $handler
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function addLimits(Closure|string|array $handler)
|
|
|
|
|
{
|
|
|
|
|
if (!is_array($handler) || is_object($handler[0])) {
|
|
|
|
|
$handler = [$handler];
|
|
|
|
|
}
|
|
|
|
|
foreach ($handler as $closure) {
|
|
|
|
|
if (in_array($closure, $this->_limits)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->_limits[] = $closure;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* 错误信息
|
|
|
|
|
*/
|
|
|
|
|
public function getError(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->_error;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Node $node
|
|
|
|
|
* @param string $field
|
|
|
|
|
* @return Node
|
|
|
|
|
*/
|
|
|
|
|
public function addChild(Node $node, string $field): Node
|
|
|
|
|
{
|
|
|
|
|
$field = (string)$field;
|
|
|
|
|
/** @var Node $oLod */
|
|
|
|
|
$oLod = $this->childes[$field] ?? null;
|
|
|
|
|
if (!empty($oLod)) {
|
|
|
|
|
$node = $oLod;
|
|
|
|
|
}
|
|
|
|
|
$this->childes[$field] = $node;
|
|
|
|
|
return $this->childes[$field];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $search
|
|
|
|
|
* @return Node|null
|
|
|
|
|
*/
|
|
|
|
|
public function findNode(string $search): ?Node
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->childes)) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (isset($this->childes[$search])) {
|
|
|
|
|
return $this->childes[$search];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$_searchMatch = '/<(\w+)?:(.+)?>/';
|
|
|
|
|
foreach ($this->childes as $key => $val) {
|
|
|
|
|
if (preg_match($_searchMatch, (string)$key, $match)) {
|
|
|
|
|
Input()->addGetParam($match[1] ?? '--', $search);
|
|
|
|
|
return $this->childes[$key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $alias
|
|
|
|
|
* @return $this
|
|
|
|
|
* 别称
|
|
|
|
|
*/
|
|
|
|
|
public function alias(string $alias): static
|
|
|
|
|
{
|
|
|
|
|
$this->_alias = $alias;
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getAlias(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->_alias;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-04-20 11:45:39 +08:00
|
|
|
* @param Closure|array $class
|
|
|
|
|
* @return $this
|
2021-04-20 11:42:24 +08:00
|
|
|
*/
|
|
|
|
|
public function addMiddleware(Closure|array $class): static
|
|
|
|
|
{
|
|
|
|
|
if (empty($class)) return $this;
|
|
|
|
|
foreach ($class as $closure) {
|
|
|
|
|
if (in_array($closure, $this->middleware)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
$this->middleware[] = $closure;
|
|
|
|
|
}
|
|
|
|
|
return $this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array
|
|
|
|
|
*/
|
|
|
|
|
public function getMiddleWares(): array
|
|
|
|
|
{
|
|
|
|
|
return $this->middleware;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function dispatch(): mixed
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->callback)) {
|
|
|
|
|
return Json::to(404, $this->errorMsg());
|
|
|
|
|
}
|
2021-04-20 11:56:14 +08:00
|
|
|
return $this->httpFilter(...func_get_args());
|
2021-04-20 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function httpFilter(): mixed
|
|
|
|
|
{
|
|
|
|
|
try {
|
2021-04-20 11:56:01 +08:00
|
|
|
$dispatchParams = func_get_args();
|
2021-04-20 11:52:28 +08:00
|
|
|
if (empty($dispatchParams)) {
|
2021-04-20 11:56:01 +08:00
|
|
|
$dispatchParams = [\request()];
|
2021-04-20 11:42:24 +08:00
|
|
|
}
|
2021-04-20 11:52:28 +08:00
|
|
|
if ($this->handler instanceof Closure) {
|
2021-04-20 12:04:05 +08:00
|
|
|
return call_user_func($this->handler, $dispatchParams);
|
2021-04-20 11:51:02 +08:00
|
|
|
}
|
2021-04-20 12:04:05 +08:00
|
|
|
return $this->runWith($this->callback, $dispatchParams);
|
2021-04-20 11:52:28 +08:00
|
|
|
return $this->runFilter(...$dispatchParams);
|
2021-04-20 11:42:24 +08:00
|
|
|
} catch (Throwable $throwable) {
|
|
|
|
|
$this->addError($throwable, 'throwable');
|
|
|
|
|
|
|
|
|
|
$code = $throwable->getCode() == 0 ? 500 : $throwable->getCode();
|
|
|
|
|
|
|
|
|
|
return Json::to($code, $throwable->getMessage());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws \Exception
|
|
|
|
|
*/
|
|
|
|
|
private function runFilter(): mixed
|
|
|
|
|
{
|
|
|
|
|
/** @var HttpFilter $filter */
|
|
|
|
|
$filter = Snowflake::app()->get('filter');
|
|
|
|
|
$validator = $filter->check(get_class($this->handler[0]), $this->handler[1]);
|
|
|
|
|
if (!($validator instanceof Validator)) {
|
2021-04-20 12:04:05 +08:00
|
|
|
return $this->runWith($this->callback, func_get_args());
|
2021-04-20 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
if (!$validator->validation()) {
|
|
|
|
|
return Json::to(5005, $validator->getError());
|
|
|
|
|
}
|
2021-04-20 12:04:05 +08:00
|
|
|
return $this->runWith($this->callback, func_get_args());
|
2021-04-20 11:42:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function runWith(): mixed
|
|
|
|
|
{
|
|
|
|
|
if (func_num_args() > 0) {
|
|
|
|
|
return call_user_func($this->callback, ...func_get_args());
|
|
|
|
|
} else {
|
|
|
|
|
return call_user_func($this->callback, \request());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function errorMsg(): string
|
|
|
|
|
{
|
|
|
|
|
return $this->_error ?? 'Page not found.';
|
|
|
|
|
}
|
2021-03-03 18:35:04 +08:00
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|