This commit is contained in:
2021-04-19 14:38:17 +08:00
parent d5c8ad8f9b
commit 0b5ae62803
2 changed files with 64 additions and 63 deletions
+61 -63
View File
@@ -8,10 +8,7 @@ namespace HttpServer\Route\Dispatch;
use Closure; use Closure;
use HttpServer\Controller; use HttpServer\Controller;
use HttpServer\Http\Context; use HttpServer\Http\Context;
use HttpServer\Http\Request;
use ReflectionException; use ReflectionException;
use Snowflake\Aop;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\NotFindClassException; use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -22,76 +19,77 @@ use Snowflake\Snowflake;
class Dispatch class Dispatch
{ {
/** @var Closure|array */ /** @var Closure|array */
protected array|Closure $handler; protected array|Closure $handler;
protected mixed $request; protected mixed $request;
/** /**
* @param $handler * @param $handler
* @param $request * @param $request
* @return static * @return static
* @throws NotFindClassException * @throws NotFindClassException
* @throws ReflectionException * @throws ReflectionException
*/ */
public static function create($handler, $request): static public static function create($handler, $request): static
{ {
$class = new static(); $class = new static();
$class->handler = $handler; $class->handler = $handler;
$class->request = $request; $class->request = $request;
if ($handler instanceof Closure) { if ($handler instanceof Closure) {
$class->bind(); $class->bind();
} }
$class->bindParam(); $class->bindParam();
return $class; return $class;
} }
/** /**
* @return mixed * @return mixed
* 执行函数 * 执行函数
*/ * @throws \Exception
public function dispatch(): mixed */
{ public function dispatch(): mixed
return \aop($this->handler, $this->request); {
} return \aop($this->handler, $this->request);
}
/** /**
* @throws ReflectionException * @throws ReflectionException
* @throws NotFindClassException * @throws NotFindClassException
*/ */
protected function bind() protected function bind()
{ {
$class = $this->bindRequest(Snowflake::createObject(Controller::class)); $class = $this->bindRequest(Snowflake::createObject(Controller::class));
$this->handler = Closure::bind($this->handler, $class); $this->handler = Closure::bind($this->handler, $class);
} }
/** /**
* @param $controller * @param $controller
* @return mixed * @return mixed
*/ */
protected function bindRequest($controller): mixed protected function bindRequest($controller): mixed
{ {
$controller->request = Context::getContext('request'); $controller->request = Context::getContext('request');
$controller->headers = $controller->request?->headers; $controller->headers = $controller->request?->headers;
$controller->input = $controller->request?->params; $controller->input = $controller->request?->params;
return $controller; return $controller;
} }
/** /**
* 参数绑定 * 参数绑定
*/ */
protected function bindParam() protected function bindParam()
{ {
if ($this->handler instanceof Closure) { if ($this->handler instanceof Closure) {
return; return;
} }
$controller = $this->handler[0]; $controller = $this->handler[0];
$this->bindRequest($controller); $this->bindRequest($controller);
} }
} }
+3
View File
@@ -56,6 +56,9 @@ class Aop extends Component
$aopName = get_class($close[0]) . '::' . $close[1]; $aopName = get_class($close[0]) . '::' . $close[1];
if (!isset($this->_aop[$aopName])) { if (!isset($this->_aop[$aopName])) {
if (!method_exists($close[0], $close[1])) {
return response()->close(404);
}
return call_user_func($close, ...$get_args); return call_user_func($close, ...$get_args);
} }