Dispatch.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: liu21st <liu21st@gmail.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\route;
  12. use think\App;
  13. use think\Container;
  14. use think\exception\ValidateException;
  15. use think\Request;
  16. use think\Response;
  17. abstract class Dispatch
  18. {
  19. /**
  20. * 应用对象
  21. * @var App
  22. */
  23. protected $app;
  24. /**
  25. * 请求对象
  26. * @var Request
  27. */
  28. protected $request;
  29. /**
  30. * 路由规则
  31. * @var Rule
  32. */
  33. protected $rule;
  34. /**
  35. * 调度信息
  36. * @var mixed
  37. */
  38. protected $dispatch;
  39. /**
  40. * 调度参数
  41. * @var array
  42. */
  43. protected $param;
  44. /**
  45. * 状态码
  46. * @var string
  47. */
  48. protected $code;
  49. /**
  50. * 是否进行大小写转换
  51. * @var bool
  52. */
  53. protected $convert;
  54. public function __construct(Request $request, Rule $rule, $dispatch, $param = [], $code = null)
  55. {
  56. $this->request = $request;
  57. $this->rule = $rule;
  58. $this->app = Container::get('app');
  59. $this->dispatch = $dispatch;
  60. $this->param = $param;
  61. $this->code = $code;
  62. if (isset($param['convert'])) {
  63. $this->convert = $param['convert'];
  64. }
  65. }
  66. public function init()
  67. {
  68. // 执行路由后置操作
  69. if ($this->rule->doAfter()) {
  70. // 设置请求的路由信息
  71. // 设置当前请求的参数
  72. $this->request->setRouteVars($this->rule->getVars());
  73. $this->request->routeInfo([
  74. 'rule' => $this->rule->getRule(),
  75. 'route' => $this->rule->getRoute(),
  76. 'option' => $this->rule->getOption(),
  77. 'var' => $this->rule->getVars(),
  78. ]);
  79. $this->doRouteAfter();
  80. }
  81. return $this;
  82. }
  83. /**
  84. * 检查路由后置操作
  85. * @access protected
  86. * @return void
  87. */
  88. protected function doRouteAfter()
  89. {
  90. // 记录匹配的路由信息
  91. $option = $this->rule->getOption();
  92. $matches = $this->rule->getVars();
  93. // 添加中间件
  94. if (!empty($option['middleware'])) {
  95. $this->app['middleware']->import($option['middleware']);
  96. }
  97. // 绑定模型数据
  98. if (!empty($option['model'])) {
  99. $this->createBindModel($option['model'], $matches);
  100. }
  101. // 指定Header数据
  102. if (!empty($option['header'])) {
  103. $header = $option['header'];
  104. $this->app['hook']->add('response_send', function ($response) use ($header) {
  105. $response->header($header);
  106. });
  107. }
  108. // 指定Response响应数据
  109. if (!empty($option['response'])) {
  110. foreach ($option['response'] as $response) {
  111. $this->app['hook']->add('response_send', $response);
  112. }
  113. }
  114. // 开启请求缓存
  115. if (isset($option['cache']) && $this->request->isGet()) {
  116. $this->parseRequestCache($option['cache']);
  117. }
  118. if (!empty($option['append'])) {
  119. $this->request->setRouteVars($option['append']);
  120. }
  121. }
  122. /**
  123. * 执行路由调度
  124. * @access public
  125. * @return mixed
  126. */
  127. public function run()
  128. {
  129. $option = $this->rule->getOption();
  130. // 检测路由after行为
  131. if (!empty($option['after'])) {
  132. $dispatch = $this->checkAfter($option['after']);
  133. if ($dispatch instanceof Response) {
  134. return $dispatch;
  135. }
  136. }
  137. // 数据自动验证
  138. if (isset($option['validate'])) {
  139. $this->autoValidate($option['validate']);
  140. }
  141. $data = $this->exec();
  142. return $this->autoResponse($data);
  143. }
  144. protected function autoResponse($data)
  145. {
  146. if ($data instanceof Response) {
  147. $response = $data;
  148. } elseif (!is_null($data)) {
  149. // 默认自动识别响应输出类型
  150. $isAjax = $this->request->isAjax();
  151. $type = $isAjax ? $this->rule->getConfig('default_ajax_return') : $this->rule->getConfig('default_return_type');
  152. $response = Response::create($data, $type);
  153. } else {
  154. $data = ob_get_clean();
  155. $content = false === $data ? '' : $data;
  156. $status = '' === $content && $this->request->isJson() ? 204 : 200;
  157. $response = Response::create($content, '', $status);
  158. }
  159. return $response;
  160. }
  161. /**
  162. * 检查路由后置行为
  163. * @access protected
  164. * @param mixed $after 后置行为
  165. * @return mixed
  166. */
  167. protected function checkAfter($after)
  168. {
  169. $this->app['log']->notice('路由后置行为建议使用中间件替代!');
  170. $hook = $this->app['hook'];
  171. $result = null;
  172. foreach ((array) $after as $behavior) {
  173. $result = $hook->exec($behavior);
  174. if (!is_null($result)) {
  175. break;
  176. }
  177. }
  178. // 路由规则重定向
  179. if ($result instanceof Response) {
  180. return $result;
  181. }
  182. return false;
  183. }
  184. /**
  185. * 验证数据
  186. * @access protected
  187. * @param array $option
  188. * @return void
  189. * @throws ValidateException
  190. */
  191. protected function autoValidate($option)
  192. {
  193. list($validate, $scene, $message, $batch) = $option;
  194. if (is_array($validate)) {
  195. // 指定验证规则
  196. $v = $this->app->validate();
  197. $v->rule($validate);
  198. } else {
  199. // 调用验证器
  200. $v = $this->app->validate($validate);
  201. if (!empty($scene)) {
  202. $v->scene($scene);
  203. }
  204. }
  205. if (!empty($message)) {
  206. $v->message($message);
  207. }
  208. // 批量验证
  209. if ($batch) {
  210. $v->batch(true);
  211. }
  212. if (!$v->check($this->request->param())) {
  213. throw new ValidateException($v->getError());
  214. }
  215. }
  216. /**
  217. * 处理路由请求缓存
  218. * @access protected
  219. * @param Request $request 请求对象
  220. * @param string|array $cache 路由缓存
  221. * @return void
  222. */
  223. protected function parseRequestCache($cache)
  224. {
  225. if (is_array($cache)) {
  226. list($key, $expire, $tag) = array_pad($cache, 3, null);
  227. } else {
  228. $key = str_replace('|', '/', $this->request->url());
  229. $expire = $cache;
  230. $tag = null;
  231. }
  232. $cache = $this->request->cache($key, $expire, $tag);
  233. $this->app->setResponseCache($cache);
  234. }
  235. /**
  236. * 路由绑定模型实例
  237. * @access protected
  238. * @param array|\Clousre $bindModel 绑定模型
  239. * @param array $matches 路由变量
  240. * @return void
  241. */
  242. protected function createBindModel($bindModel, $matches)
  243. {
  244. foreach ($bindModel as $key => $val) {
  245. if ($val instanceof \Closure) {
  246. $result = $this->app->invokeFunction($val, $matches);
  247. } else {
  248. $fields = explode('&', $key);
  249. if (is_array($val)) {
  250. list($model, $exception) = $val;
  251. } else {
  252. $model = $val;
  253. $exception = true;
  254. }
  255. $where = [];
  256. $match = true;
  257. foreach ($fields as $field) {
  258. if (!isset($matches[$field])) {
  259. $match = false;
  260. break;
  261. } else {
  262. $where[] = [$field, '=', $matches[$field]];
  263. }
  264. }
  265. if ($match) {
  266. $query = strpos($model, '\\') ? $model::where($where) : $this->app->model($model)->where($where);
  267. $result = $query->failException($exception)->find();
  268. }
  269. }
  270. if (!empty($result)) {
  271. // 注入容器
  272. $this->app->instance(get_class($result), $result);
  273. }
  274. }
  275. }
  276. public function convert($convert)
  277. {
  278. $this->convert = $convert;
  279. return $this;
  280. }
  281. public function getDispatch()
  282. {
  283. return $this->dispatch;
  284. }
  285. public function getParam()
  286. {
  287. return $this->param;
  288. }
  289. abstract public function exec();
  290. public function __sleep()
  291. {
  292. return ['rule', 'dispatch', 'convert', 'param', 'code', 'controller', 'actionName'];
  293. }
  294. public function __wakeup()
  295. {
  296. $this->app = Container::get('app');
  297. $this->request = $this->app['request'];
  298. }
  299. public function __debugInfo()
  300. {
  301. $data = get_object_vars($this);
  302. unset($data['app'], $data['request'], $data['rule']);
  303. return $data;
  304. }
  305. }