| Total Complexity | 32 |
| Total Lines | 238 |
| Duplicated Lines | 0 % |
| Coverage | 0% |
| Changes | 0 | ||
| 1 | <?php |
||
| 21 | abstract class Dispatch |
||
|
1 ignored issue
–
show
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * 应用对象 |
||
| 25 | * @var \think\App |
||
| 26 | */ |
||
| 27 | protected $app; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * 请求对象 |
||
| 31 | * @var Request |
||
| 32 | */ |
||
| 33 | protected $request; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * 路由规则 |
||
| 37 | * @var Rule |
||
| 38 | */ |
||
| 39 | protected $rule; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * 调度信息 |
||
| 43 | * @var mixed |
||
| 44 | */ |
||
| 45 | protected $dispatch; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * 调度参数 |
||
| 49 | * @var array |
||
| 50 | */ |
||
| 51 | protected $param; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * 状态码 |
||
| 55 | * @var int |
||
| 56 | */ |
||
| 57 | protected $code; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * 是否进行大小写转换 |
||
| 61 | * @var bool |
||
| 62 | */ |
||
| 63 | protected $convert; |
||
| 64 | |||
| 65 | public function __construct(Request $request, Rule $rule, $dispatch, array $param = [], int $code = null) |
||
| 75 | } |
||
| 76 | } |
||
| 77 | |||
| 78 | public function init(App $app) |
||
| 79 | { |
||
| 80 | $this->app = $app; |
||
| 81 | // 执行路由后置操作 |
||
| 82 | $this->doRouteAfter(); |
||
| 83 | } |
||
| 84 | |||
| 85 | /** |
||
| 86 | * 执行路由调度 |
||
| 87 | * @access public |
||
| 88 | * @return mixed |
||
| 89 | */ |
||
| 90 | public function run(): Response |
||
| 91 | { |
||
| 92 | $option = $this->rule->getOption(); |
||
| 93 | |||
| 94 | // 数据自动验证 |
||
| 95 | if (isset($option['validate'])) { |
||
| 96 | $this->autoValidate($option['validate']); |
||
| 97 | } |
||
| 98 | |||
| 99 | $data = $this->exec(); |
||
| 100 | |||
| 101 | return $this->autoResponse($data); |
||
| 102 | } |
||
| 103 | |||
| 104 | protected function autoResponse($data): Response |
||
| 105 | { |
||
| 106 | if ($data instanceof Response) { |
||
| 107 | $response = $data; |
||
| 108 | } elseif (!is_null($data)) { |
||
| 109 | // 默认自动识别响应输出类型 |
||
| 110 | $type = $this->request->isJson() ? 'json' : 'html'; |
||
| 111 | $response = Response::create($data, $type); |
||
| 112 | } else { |
||
| 113 | $data = ob_get_clean(); |
||
| 114 | |||
| 115 | $content = false === $data ? '' : $data; |
||
| 116 | $status = false === $data ? 204 : 200; |
||
| 117 | $response = Response::create($content, '', $status); |
||
| 118 | } |
||
| 119 | |||
| 120 | return $response; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * 检查路由后置操作 |
||
| 125 | * @access protected |
||
| 126 | * @return void |
||
| 127 | */ |
||
| 128 | protected function doRouteAfter(): void |
||
| 129 | { |
||
| 130 | // 记录匹配的路由信息 |
||
| 131 | $option = $this->rule->getOption(); |
||
| 132 | |||
| 133 | // 添加中间件 |
||
| 134 | if (!empty($option['middleware'])) { |
||
| 135 | $this->app['middleware']->import($option['middleware']); |
||
| 136 | } |
||
| 137 | |||
| 138 | // 绑定模型数据 |
||
| 139 | if (!empty($option['model'])) { |
||
| 140 | $this->createBindModel($option['model'], $this->request->route()); |
||
| 141 | } |
||
| 142 | |||
| 143 | if (!empty($option['append'])) { |
||
| 144 | $this->request->setRoute($option['append']); |
||
| 145 | } |
||
| 146 | } |
||
| 147 | |||
| 148 | /** |
||
| 149 | * 路由绑定模型实例 |
||
| 150 | * @access protected |
||
| 151 | * @param array $bindModel 绑定模型 |
||
|
1 ignored issue
–
show
|
|||
| 152 | * @param array $matches 路由变量 |
||
|
1 ignored issue
–
show
|
|||
| 153 | * @return void |
||
| 154 | */ |
||
| 155 | protected function createBindModel(array $bindModel, array $matches): void |
||
| 190 | } |
||
| 191 | } |
||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * 验证数据 |
||
| 196 | * @access protected |
||
| 197 | * @param array $option |
||
|
1 ignored issue
–
show
|
|||
| 198 | * @return void |
||
| 199 | * @throws \think\exception\ValidateException |
||
| 200 | */ |
||
| 201 | protected function autoValidate(array $option): void |
||
| 202 | { |
||
| 203 | list($validate, $scene, $message, $batch) = $option; |
||
| 204 | |||
| 205 | if (is_array($validate)) { |
||
| 206 | // 指定验证规则 |
||
| 207 | $v = new Validate(); |
||
| 208 | $v->rule($validate); |
||
| 209 | } else { |
||
| 210 | // 调用验证器 |
||
| 211 | /** @var Validate $class */ |
||
| 212 | $class = $this->app->parseClass('validate', $validate); |
||
| 213 | $v = new $class(); |
||
| 214 | |||
| 215 | if (!empty($scene)) { |
||
| 216 | $v->scene($scene); |
||
| 217 | } |
||
| 218 | } |
||
| 219 | |||
| 220 | $v->message($message)->batch($batch)->failException(true)->check($this->request->param()); |
||
| 221 | } |
||
| 222 | |||
| 223 | public function convert(bool $convert) |
||
| 224 | { |
||
| 225 | $this->convert = $convert; |
||
| 226 | |||
| 227 | return $this; |
||
| 228 | } |
||
| 229 | |||
| 230 | public function getDispatch() |
||
| 231 | { |
||
| 232 | return $this->dispatch; |
||
| 233 | } |
||
| 234 | |||
| 235 | public function getParam(): array |
||
| 236 | { |
||
| 237 | return $this->param; |
||
| 238 | } |
||
| 239 | |||
| 240 | abstract public function exec(); |
||
| 241 | |||
| 242 | public function __sleep() |
||
| 243 | { |
||
| 244 | return ['rule', 'dispatch', 'convert', 'param', 'code', 'controller', 'actionName']; |
||
| 245 | } |
||
| 246 | |||
| 247 | public function __wakeup() |
||
| 251 | } |
||
| 252 | |||
| 253 | public function __debugInfo() |
||
| 254 | { |
||
| 255 | return [ |
||
| 259 | ]; |
||
| 260 | } |
||
| 261 | } |
||
| 262 |