<?php<liu21st@gmail.com>declare (strict_types = 1);
namespace think\route;
use think\App;
use think\Container;
use think\Request;
use think\Response;
use think\Validate;
abstract class Dispatch
{
protected $app;
protected $request;
protected $rule;
protected $dispatch;
protected $param;
public function __construct(Request $request, Rule $rule, $dispatch, array $param = [])
{
$this->request = $request;
$this->rule = $rule;
$this->dispatch = $dispatch;
$this->param = $param;
}
public function init(App $app)
{
$this->app = $app;
$this->doRouteAfter();
}
public function run(): Response
{
if ($this->rule instanceof RuleItem && $this->request->method() == 'OPTIONS' && $this->rule->isAutoOptions()) {
$rules = $this->rule->getRouter()->getRule($this->rule->getRule());
$allow = [];
foreach ($rules as $item) {
$allow[] = strtoupper($item->getMethod());
}
return Response::create('', 'html', 204)->header(['Allow' => implode(', ', $allow)]);
}
$data = $this->exec();
return $this->autoResponse($data);
}
protected function autoResponse($data): Response
{
if ($data instanceof Response) {
$response = $data;
} elseif (!is_null($data)) {
$type = $this->request->isJson() ? 'json' : 'html';
$response = Response::create($data, $type);
} else {
$data = ob_get_clean();
$content = false === $data ? '' : $data;
$status = '' === $content && $this->request->isJson() ? 204 : 200;
$response = Response::create($content, 'html', $status);
}
return $response;
}
protected function doRouteAfter(): void
{
$option = $this->rule->getOption();
if (!empty($option['middleware'])) {
$this->app->middleware->import($option['middleware'], 'route');
}
if (!empty($option['append'])) {
$this->param = array_merge($this->param, $option['append']);
}
if (!empty($option['model'])) {
$this->createBindModel($option['model'], $this->param);
}
$this->request->setRule($this->rule);
$this->request->setRoute($this->param);
if (isset($option['validate'])) {
$this->autoValidate($option['validate']);
}
}
protected function createBindModel(array $bindModel, array $matches): void
{
foreach ($bindModel as $key => $val) {
if ($val instanceof \Closure) {
$result = $this->app->invokeFunction($val, $matches);
} else {
$fields = explode('&', $key);
if (is_array($val)) {
[$model, $exception] = $val;
} else {
$model = $val;
$exception = true;
}
$where = [];
$match = true;
foreach ($fields as $field) {
if (!isset($matches[$field])) {
$match = false;
break;
} else {
$where[] = [$field, '=', $matches[$field]];
}
}
if ($match) {
$result = $model::where($where)->failException($exception)->find();
}
}
if (!empty($result)) {
$this->app->instance(get_class($result), $result);
}
}
}
protected function autoValidate(array $option): void
{
[$validate, $scene, $message, $batch] = $option;
if (is_array($validate)) {
$v = new Validate();
$v->rule($validate);
} else {
$class = false !== strpos($validate, '\\') ? $validate : $this->app->parseClass('validate', $validate);
$v = new $class();
if (!empty($scene)) {
$v->scene($scene);
}
}
$v->message($message)
->batch($batch)
->failException(true)
->check($this->request->param());
}
public function getDispatch()
{
return $this->dispatch;
}
public function getParam(): array
{
return $this->param;
}
abstract public function exec();
public function __sleep()
{
return ['rule', 'dispatch', 'param', 'controller', 'actionName'];
}
public function __wakeup()
{
$this->app = Container::pull('app');
$this->request = $this->app->request;
}
public function __debugInfo()
{
return [
'dispatch' => $this->dispatch,
'param' => $this->param,
'rule' => $this->rule,
];
}
}