This commit is contained in:
as2252258@163.com
2021-04-25 01:21:46 +08:00
parent d52c549b30
commit 9f8dbdead3
2 changed files with 64 additions and 45 deletions
+13 -8
View File
@@ -32,26 +32,31 @@ class HttpFilter extends BaseObject
} }
/**
* @param string $className
* @param string $method
* @return array|mixed
*/
public function getRules(string $className, string $method)
{
return $this->hash[$className . '::' . $method] ?? [];
}
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
* @return bool|Validator * @return bool|Validator
* @throws Exception * @throws Exception
*/ */
public function check(string $className, string $method): bool|Validator public function check(array $rules): bool|Validator
{ {
if (!isset($this->hash[$className . '::' . $method])) {
return true;
}
$rules = $this->hash[$className . '::' . $method][request()->getMethod()] ?? [];
if (empty($rules)) { if (empty($rules)) {
return true; return true;
} }
$validator = Validator::getInstance(); $validator = Validator::getInstance();
$validator->setParams(Input()->load()); $validator->setParams(Input()->load());
foreach ($rules as $Key => $val) { foreach ($rules as $val) {
$field = array_shift($val); $field = array_shift($val);
if (empty($val)) { if (empty($val)) {
continue; continue;
+21 -7
View File
@@ -42,7 +42,7 @@ class Node extends HttpService
private string $_error = ''; private string $_error = '';
public array $rules = []; private array $rules = [];
private string $_dataType = ''; private string $_dataType = '';
@@ -69,7 +69,7 @@ class Node extends HttpService
*/ */
public function setDataType(string $dataType) public function setDataType(string $dataType)
{ {
$this->_dataType; $this->_dataType = $dataType;
} }
@@ -338,9 +338,9 @@ class Node extends HttpService
{ {
$annotation = annotation()->getMethods($className, $action); $annotation = annotation()->getMethods($className, $action);
if (empty($annotation)) { if (empty($annotation)) {
return $this; return $this->injectRules($className, $action);
} }
foreach ($annotation as $name => $attribute) { foreach ($annotation as $attribute) {
if ($attribute instanceof Interceptor) { if ($attribute instanceof Interceptor) {
$this->addInterceptor($attribute->interceptor); $this->addInterceptor($attribute->interceptor);
} }
@@ -354,6 +354,22 @@ class Node extends HttpService
$this->addLimits($attribute->limits); $this->addLimits($attribute->limits);
} }
} }
return $this->injectRules($className, $action);
}
/**
* @param string $controller
* @param string $action
* @return $this
* @throws \Exception
*/
private function injectRules(string $controller, string $action): static
{
/** @var HttpFilter $filter */
$filter = Snowflake::app()->get('filter');
$this->rules = $filter->getRules($controller, $action);
return $this; return $this;
} }
@@ -528,11 +544,9 @@ class Node extends HttpService
*/ */
private function runValidator($dispatchParams): mixed private function runValidator($dispatchParams): mixed
{ {
return call_user_func($this->callback, ...$dispatchParams);
/** @var HttpFilter $filter */ /** @var HttpFilter $filter */
$filter = Snowflake::app()->get('filter'); $filter = Snowflake::app()->get('filter');
$validator = $filter->check(get_class($this->handler[0]), $this->handler[1]); $validator = $filter->check($this->rules);
if (!($validator instanceof Validator)) { if (!($validator instanceof Validator)) {
return call_user_func($this->callback, ...$dispatchParams); return call_user_func($this->callback, ...$dispatchParams);
} else if ($validator->validation()) { } else if ($validator->validation()) {