This commit is contained in:
as2252258@163.com
2021-03-29 03:19:22 +08:00
parent 62ea2c7d7d
commit 796b819fe5
2 changed files with 75 additions and 62 deletions
+60 -62
View File
@@ -22,78 +22,76 @@ 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
* 执行函数 * 执行函数
*/ */
public function dispatch(): mixed public function dispatch(): mixed
{ {
/** @var Aop $aop */ return \aop($this->handler, $this->request);
$aop = Snowflake::app()->get('aop'); }
return call_user_func([$aop, 'dispatch'], $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);
} }
} }
+15
View File
@@ -298,6 +298,21 @@ if (!function_exists('fire')) {
} }
} }
if (!function_exists('aop')) {
/**
* @param string $event
* @param array $params
* @throws Exception
* @throws Exception
*/
function aop(mixed $handler, array $params = [])
{
return Snowflake::app()->get('aop')->dispatch($handler, ...$params);
}
}
if (!function_exists('objectPool')) { if (!function_exists('objectPool')) {