Files
kiri-core/HttpServer/Route/Dispatch/Dispatch.php
T

64 lines
984 B
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
2020-10-29 18:17:25 +08:00
declare(strict_types=1);
2020-08-31 01:27:08 +08:00
namespace HttpServer\Route\Dispatch;
2020-10-29 18:17:25 +08:00
use Closure;
2020-08-31 01:27:08 +08:00
use HttpServer\Controller;
2020-08-31 10:38:24 +08:00
use HttpServer\Http\Context;
2020-08-31 01:27:08 +08:00
/**
* Class Dispatch
* @package HttpServer\Route\Dispatch
*/
class Dispatch
{
2021-04-19 14:38:17 +08:00
/** @var Closure|array */
protected array|Closure $handler;
protected mixed $request;
/**
* @param $handler
* @return static
*/
2021-04-21 01:49:17 +08:00
public static function create($handler): static
2021-04-19 14:38:17 +08:00
{
$class = new static();
$class->handler = $handler;
if ($handler instanceof Closure) {
$class->bind();
}
return $class;
}
/**
* @return mixed
* 执行函数
* @throws \Exception
*/
public function dispatch(): mixed
{
2021-04-21 01:43:37 +08:00
$dispatchParam = Context::getContext('dispatch-param');
if (empty($dispatchParam)) {
$dispatchParam = [\request()];
}
return \aop($this->handler, $dispatchParam);
2021-04-19 14:38:17 +08:00
}
/**
2021-04-22 14:37:28 +08:00
*
2021-04-19 14:38:17 +08:00
*/
protected function bind()
{
2021-04-22 14:37:28 +08:00
$this->handler = Closure::bind($this->handler, new Controller());
2021-04-19 14:38:17 +08:00
}
2020-08-31 01:27:08 +08:00
}