This commit is contained in:
2021-04-26 12:45:29 +08:00
parent c907c42c07
commit 3dbb95e627
2 changed files with 36 additions and 8 deletions
+12 -4
View File
@@ -4,15 +4,12 @@ declare(strict_types=1);
namespace HttpServer; namespace HttpServer;
use Exception;
use HttpServer\Abstracts\HttpService; use HttpServer\Abstracts\HttpService;
use HttpServer\Http\HttpHeaders; use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams; use HttpServer\Http\HttpParams;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use Exception;
use ReflectionException;
use Snowflake\Abstracts\TraitApplication; use Snowflake\Abstracts\TraitApplication;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -69,6 +66,7 @@ class Controller extends HttpService
/** /**
* @return Request|null * @return Request|null
* @throws Exception
*/ */
public function getRequest(): ?Request public function getRequest(): ?Request
{ {
@@ -76,6 +74,16 @@ class Controller extends HttpService
} }
/**
* @param Request $request
* @return bool
*/
public function beforeAction(Request $request): bool
{
return true;
}
/** /**
* @param $name * @param $name
* @return mixed * @return mixed
+24 -4
View File
@@ -21,7 +21,6 @@ use JetBrains\PhpStorm\Pure;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Throwable; use Throwable;
use validator\Validator;
use function Input; use function Input;
/** /**
@@ -130,13 +129,32 @@ class Node extends HttpService
if (empty($dispatchParam)) { if (empty($dispatchParam)) {
$dispatchParam = [\request()]; $dispatchParam = [\request()];
} }
// return call_user_func($this->handler, ...$dispatchParam); if ($this->beforeAction()) {
return \aop($this->handler, $dispatchParam);
return \aop($this->handler, $dispatchParam); }
return response()->close(400);
}; };
} }
private bool $_hasBeforeAction = false;
/**
* @return bool
* @throws Exception
*/
public function beforeAction(): bool
{
if (!$this->_hasBeforeAction) return true;
[$controller, $action] = $this->handler;
if (!$controller->beforeAction(\request())) {
return false;
}
return true;
}
/** /**
* @return array * @return array
*/ */
@@ -299,6 +317,8 @@ class Node extends HttpService
throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); throw new Exception('method ' . $action . ' not exists at ' . $controller . '.');
} }
$this->_hasBeforeAction = $reflect->hasMethod('beforeAction');
$this->annotationInject($reflect->getName(), $action); $this->annotationInject($reflect->getName(), $action);
return [$reflect->newInstance(), $action]; return [$reflect->newInstance(), $action];