No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Dispatch.php 9.6KB

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