From 3dbb95e627925d3fe94816c45b0abd9338f7993c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Mon, 26 Apr 2021 12:45:29 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Controller.php | 16 ++++++++++++---- HttpServer/Route/Node.php | 28 ++++++++++++++++++++++++---- 2 files changed, 36 insertions(+), 8 deletions(-) diff --git a/HttpServer/Controller.php b/HttpServer/Controller.php index b455a3c9..42f1ae33 100644 --- a/HttpServer/Controller.php +++ b/HttpServer/Controller.php @@ -4,15 +4,12 @@ declare(strict_types=1); namespace HttpServer; +use Exception; use HttpServer\Abstracts\HttpService; use HttpServer\Http\HttpHeaders; use HttpServer\Http\HttpParams; use HttpServer\Http\Request; -use Exception; -use ReflectionException; use Snowflake\Abstracts\TraitApplication; -use Snowflake\Exception\ComponentException; -use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; /** @@ -69,6 +66,7 @@ class Controller extends HttpService /** * @return Request|null + * @throws Exception */ 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 * @return mixed diff --git a/HttpServer/Route/Node.php b/HttpServer/Route/Node.php index 42489b47..95a31b77 100644 --- a/HttpServer/Route/Node.php +++ b/HttpServer/Route/Node.php @@ -21,7 +21,6 @@ use JetBrains\PhpStorm\Pure; use Snowflake\Core\Json; use Snowflake\Snowflake; use Throwable; -use validator\Validator; use function Input; /** @@ -130,13 +129,32 @@ class Node extends HttpService if (empty($dispatchParam)) { $dispatchParam = [\request()]; } -// return call_user_func($this->handler, ...$dispatchParam); - - return \aop($this->handler, $dispatchParam); + if ($this->beforeAction()) { + 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 */ @@ -299,6 +317,8 @@ class Node extends HttpService throw new Exception('method ' . $action . ' not exists at ' . $controller . '.'); } + $this->_hasBeforeAction = $reflect->hasMethod('beforeAction'); + $this->annotationInject($reflect->getName(), $action); return [$reflect->newInstance(), $action];