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
+43 -38
View File
@@ -16,49 +16,54 @@ class HttpFilter extends BaseObject
{ {
private array $hash = []; private array $hash = [];
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
* @param array $rules * @param array $rules
* @return $this * @return $this
*/ */
public function register(string $className, string $method, array $rules): HttpFilter public function register(string $className, string $method, array $rules): HttpFilter
{ {
$this->hash[$className . '::' . $method] = $rules; $this->hash[$className . '::' . $method] = $rules;
return $this; return $this;
} }
/** /**
* @param string $className * @param string $className
* @param string $method * @param string $method
* @return bool|Validator * @return array|mixed
* @throws Exception */
*/ public function getRules(string $className, string $method)
public function check(string $className, string $method): bool|Validator {
{ return $this->hash[$className . '::' . $method] ?? [];
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()); * @param string $className
foreach ($rules as $Key => $val) { * @param string $method
$field = array_shift($val); * @return bool|Validator
if (empty($val)) { * @throws Exception
continue; */
} public function check(array $rules): bool|Validator
$validator->make($field, $val); {
} if (empty($rules)) {
return $validator; return true;
} }
$validator = Validator::getInstance();
$validator->setParams(Input()->load());
foreach ($rules as $val) {
$field = array_shift($val);
if (empty($val)) {
continue;
}
$validator->make($field, $val);
}
return $validator;
}
} }
+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()) {