Domain.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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\Container;
  13. use think\Loader;
  14. use think\Request;
  15. use think\Route;
  16. use think\route\dispatch\Callback as CallbackDispatch;
  17. use think\route\dispatch\Controller as ControllerDispatch;
  18. use think\route\dispatch\Module as ModuleDispatch;
  19. class Domain extends RuleGroup
  20. {
  21. /**
  22. * 架构函数
  23. * @access public
  24. * @param Route $router 路由对象
  25. * @param string $name 路由域名
  26. * @param mixed $rule 域名路由
  27. * @param array $option 路由参数
  28. * @param array $pattern 变量规则
  29. */
  30. public function __construct(Route $router, $name = '', $rule = null, $option = [], $pattern = [])
  31. {
  32. $this->router = $router;
  33. $this->domain = $name;
  34. $this->option = $option;
  35. $this->rule = $rule;
  36. $this->pattern = $pattern;
  37. }
  38. /**
  39. * 检测域名路由
  40. * @access public
  41. * @param Request $request 请求对象
  42. * @param string $url 访问地址
  43. * @param bool $completeMatch 路由是否完全匹配
  44. * @return Dispatch|false
  45. */
  46. public function check($request, $url, $completeMatch = false)
  47. {
  48. // 检测别名路由
  49. $result = $this->checkRouteAlias($request, $url);
  50. if (false !== $result) {
  51. return $result;
  52. }
  53. // 检测URL绑定
  54. $result = $this->checkUrlBind($request, $url);
  55. if (!empty($this->option['append'])) {
  56. $request->setRouteVars($this->option['append']);
  57. unset($this->option['append']);
  58. }
  59. if (false !== $result) {
  60. return $result;
  61. }
  62. // 添加域名中间件
  63. if (!empty($this->option['middleware'])) {
  64. Container::get('middleware')->import($this->option['middleware']);
  65. unset($this->option['middleware']);
  66. }
  67. return parent::check($request, $url, $completeMatch);
  68. }
  69. /**
  70. * 设置路由绑定
  71. * @access public
  72. * @param string $bind 绑定信息
  73. * @return $this
  74. */
  75. public function bind($bind)
  76. {
  77. $this->router->bind($bind, $this->domain);
  78. return $this;
  79. }
  80. /**
  81. * 检测路由别名
  82. * @access private
  83. * @param Request $request
  84. * @param string $url URL地址
  85. * @return Dispatch|false
  86. */
  87. private function checkRouteAlias($request, $url)
  88. {
  89. $alias = strpos($url, '|') ? strstr($url, '|', true) : $url;
  90. $item = $this->router->getAlias($alias);
  91. return $item ? $item->check($request, $url) : false;
  92. }
  93. /**
  94. * 检测URL绑定
  95. * @access private
  96. * @param Request $request
  97. * @param string $url URL地址
  98. * @return Dispatch|false
  99. */
  100. private function checkUrlBind($request, $url)
  101. {
  102. $bind = $this->router->getBind($this->domain);
  103. if (!empty($bind)) {
  104. $this->parseBindAppendParam($bind);
  105. // 记录绑定信息
  106. Container::get('app')->log('[ BIND ] ' . var_export($bind, true));
  107. // 如果有URL绑定 则进行绑定检测
  108. $type = substr($bind, 0, 1);
  109. $bind = substr($bind, 1);
  110. $bindTo = [
  111. '\\' => 'bindToClass',
  112. '@' => 'bindToController',
  113. ':' => 'bindToNamespace',
  114. ];
  115. if (isset($bindTo[$type])) {
  116. return $this->{$bindTo[$type]}($request, $url, $bind);
  117. }
  118. }
  119. return false;
  120. }
  121. protected function parseBindAppendParam(&$bind)
  122. {
  123. if (false !== strpos($bind, '?')) {
  124. list($bind, $query) = explode('?', $bind);
  125. parse_str($query, $vars);
  126. $this->append($vars);
  127. }
  128. }
  129. /**
  130. * 绑定到类
  131. * @access protected
  132. * @param Request $request
  133. * @param string $url URL地址
  134. * @param string $class 类名(带命名空间)
  135. * @return CallbackDispatch
  136. */
  137. protected function bindToClass($request, $url, $class)
  138. {
  139. $array = explode('|', $url, 2);
  140. $action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
  141. $param = [];
  142. if (!empty($array[1])) {
  143. $this->parseUrlParams($request, $array[1], $param);
  144. }
  145. return new CallbackDispatch($request, $this, [$class, $action], $param);
  146. }
  147. /**
  148. * 绑定到命名空间
  149. * @access protected
  150. * @param Request $request
  151. * @param string $url URL地址
  152. * @param string $namespace 命名空间
  153. * @return CallbackDispatch
  154. */
  155. protected function bindToNamespace($request, $url, $namespace)
  156. {
  157. $array = explode('|', $url, 3);
  158. $class = !empty($array[0]) ? $array[0] : $this->router->config('default_controller');
  159. $method = !empty($array[1]) ? $array[1] : $this->router->config('default_action');
  160. $param = [];
  161. if (!empty($array[2])) {
  162. $this->parseUrlParams($request, $array[2], $param);
  163. }
  164. return new CallbackDispatch($request, $this, [$namespace . '\\' . Loader::parseName($class, 1), $method], $param);
  165. }
  166. /**
  167. * 绑定到控制器类
  168. * @access protected
  169. * @param Request $request
  170. * @param string $url URL地址
  171. * @param string $controller 控制器名 (支持带模块名 index/user )
  172. * @return ControllerDispatch
  173. */
  174. protected function bindToController($request, $url, $controller)
  175. {
  176. $array = explode('|', $url, 2);
  177. $action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
  178. $param = [];
  179. if (!empty($array[1])) {
  180. $this->parseUrlParams($request, $array[1], $param);
  181. }
  182. return new ControllerDispatch($request, $this, $controller . '/' . $action, $param);
  183. }
  184. /**
  185. * 绑定到模块/控制器
  186. * @access protected
  187. * @param Request $request
  188. * @param string $url URL地址
  189. * @param string $controller 控制器类名(带命名空间)
  190. * @return ModuleDispatch
  191. */
  192. protected function bindToModule($request, $url, $controller)
  193. {
  194. $array = explode('|', $url, 2);
  195. $action = !empty($array[0]) ? $array[0] : $this->router->config('default_action');
  196. $param = [];
  197. if (!empty($array[1])) {
  198. $this->parseUrlParams($request, $array[1], $param);
  199. }
  200. return new ModuleDispatch($request, $this, $controller . '/' . $action, $param);
  201. }
  202. }