RuleItem.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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\Route;
  13. class RuleItem extends Rule
  14. {
  15. /**
  16. * 路由规则
  17. * @var string
  18. */
  19. protected $name;
  20. /**
  21. * 路由地址
  22. * @var string|\Closure
  23. */
  24. protected $route;
  25. /**
  26. * 请求类型
  27. * @var string
  28. */
  29. protected $method;
  30. /**
  31. * 架构函数
  32. * @access public
  33. * @param Route $router 路由实例
  34. * @param RuleGroup $group 路由所属分组对象
  35. * @param string|array $name 路由规则
  36. * @param string $method 请求类型
  37. * @param string|\Closure $route 路由地址
  38. * @param array $option 路由参数
  39. * @param array $pattern 变量规则
  40. */
  41. public function __construct(Route $router, RuleGroup $group, $name, $route, $method = '*', $option = [], $pattern = [])
  42. {
  43. $this->router = $router;
  44. $this->parent = $group;
  45. $this->route = $route;
  46. $this->method = $method;
  47. $this->option = $option;
  48. $this->pattern = $pattern;
  49. $this->setRule($name);
  50. }
  51. /**
  52. * 路由规则预处理
  53. * @access public
  54. * @param string $rule 路由规则
  55. * @return void
  56. */
  57. public function setRule($rule)
  58. {
  59. if ('$' == substr($rule, -1, 1)) {
  60. // 是否完整匹配
  61. $rule = substr($rule, 0, -1);
  62. $this->option['complete_match'] = true;
  63. }
  64. $this->name($rule);
  65. }
  66. /**
  67. * 获取当前路由地址
  68. * @access public
  69. * @return mixed
  70. */
  71. public function getRoute()
  72. {
  73. return $this->route;
  74. }
  75. /**
  76. * 设置为自动路由
  77. * @access public
  78. * @return $this
  79. */
  80. public function isAuto()
  81. {
  82. $this->parent->setAutoRule($this);
  83. return $this;
  84. }
  85. /**
  86. * 设置为MISS路由
  87. * @access public
  88. * @return $this
  89. */
  90. public function isMiss()
  91. {
  92. $this->parent->setMissRule($this);
  93. return $this;
  94. }
  95. /**
  96. * 检测路由
  97. * @access public
  98. * @param Request $request 请求对象
  99. * @param string $url 访问地址
  100. * @param string $depr 路径分隔符
  101. * @param bool $completeMatch 路由是否完全匹配
  102. * @return Dispatch
  103. */
  104. public function check($request, $url, $depr = '/', $completeMatch = false)
  105. {
  106. if ($this->parent && $prefix = $this->parent->getFullName()) {
  107. $this->name = $prefix . ($this->name ? '/' . ltrim($this->name, '/') : '');
  108. }
  109. if ($dispatch = $this->checkCrossDomain($request)) {
  110. // 允许跨域
  111. return $dispatch;
  112. }
  113. // 检查参数有效性
  114. if (!$this->checkOption($this->option, $request)) {
  115. return false;
  116. }
  117. // 合并分组参数
  118. $this->mergeGroupOptions();
  119. $option = $this->option;
  120. if (!empty($option['append'])) {
  121. $request->route($option['append']);
  122. }
  123. // 是否区分 / 地址访问
  124. if (!empty($option['remove_slash']) && '/' != $this->name) {
  125. $this->name = rtrim($this->name, '/');
  126. $url = rtrim($url, '|');
  127. }
  128. // 检查前置行为
  129. if (isset($option['before']) && false === $this->checkBefore($option['before'])) {
  130. return false;
  131. }
  132. if (isset($option['ext'])) {
  133. // 路由ext参数 优先于系统配置的URL伪静态后缀参数
  134. $url = preg_replace('/\.(' . $request->ext() . ')$/i', '', $url);
  135. }
  136. return $this->checkRule($request, $url, $depr, $completeMatch, $option);
  137. }
  138. /**
  139. * 检测路由规则
  140. * @access private
  141. * @param Request $request 请求对象
  142. * @param string $url URL地址
  143. * @param string $depr URL分隔符(全局)
  144. * @param bool $completeMatch 路由是否完全匹配
  145. * @param array $option 路由参数
  146. * @return array|false
  147. */
  148. private function checkRule($request, $url, $depr, $completeMatch = false, $option = [])
  149. {
  150. // 检查完整规则定义
  151. if (isset($this->pattern['__url__']) && !preg_match(0 === strpos($this->pattern['__url__'], '/') ? $this->pattern['__url__'] : '/^' . $this->pattern['__url__'] . '/', str_replace('|', $depr, $url))) {
  152. return false;
  153. }
  154. // 检查路由的参数分隔符
  155. if (isset($option['param_depr'])) {
  156. $url = str_replace(['|', $option['param_depr']], [$depr, '|'], $url);
  157. }
  158. $len1 = substr_count($url, '|');
  159. $len2 = substr_count($this->name, '/');
  160. // 多余参数是否合并
  161. $merge = !empty($option['merge_extra_vars']) ? true : false;
  162. if ($merge && $len1 > $len2) {
  163. $url = str_replace('|', $depr, $url);
  164. $url = implode('|', explode($depr, $url, $len2 + 1));
  165. }
  166. if (isset($option['complete_match'])) {
  167. $completeMatch = $option['complete_match'];
  168. }
  169. if ($len1 >= $len2 || strpos($this->name, '[')) {
  170. // 完整匹配
  171. if ($completeMatch && (!$merge && $len1 != $len2 && (false === strpos($this->name, '[') || $len1 > $len2 || $len1 < $len2 - substr_count($this->name, '[')))) {
  172. return false;
  173. }
  174. $pattern = array_merge($this->parent->getPattern(), $this->pattern);
  175. if (false !== $match = $this->match($url, $pattern)) {
  176. // 匹配到路由规则
  177. return $this->parseRule($request, $this->name, $this->route, $url, $option, $match);
  178. }
  179. }
  180. return false;
  181. }
  182. /**
  183. * 检测URL和规则路由是否匹配
  184. * @access private
  185. * @param string $url URL地址
  186. * @param array $pattern 变量规则
  187. * @return array|false
  188. */
  189. private function match($url, $pattern)
  190. {
  191. $m2 = explode('/', $this->name);
  192. $m1 = explode('|', $url);
  193. $var = [];
  194. foreach ($m2 as $key => $val) {
  195. // val中定义了多个变量 <id><name>
  196. if (false !== strpos($val, '<') && preg_match_all('/<(\w+(\??))>/', $val, $matches)) {
  197. $value = [];
  198. $replace = [];
  199. foreach ($matches[1] as $name) {
  200. if (strpos($name, '?')) {
  201. $name = substr($name, 0, -1);
  202. $replace[] = '(' . (isset($pattern[$name]) ? $pattern[$name] : '\w+') . ')?';
  203. } else {
  204. $replace[] = '(' . (isset($pattern[$name]) ? $pattern[$name] : '\w+') . ')';
  205. }
  206. $value[] = $name;
  207. }
  208. $val = str_replace($matches[0], $replace, $val);
  209. if (preg_match('/^' . $val . '$/', isset($m1[$key]) ? $m1[$key] : '', $match)) {
  210. array_shift($match);
  211. foreach ($value as $k => $name) {
  212. if (isset($match[$k])) {
  213. $var[$name] = $match[$k];
  214. }
  215. }
  216. continue;
  217. } else {
  218. return false;
  219. }
  220. }
  221. if (0 === strpos($val, '[:')) {
  222. // 可选参数
  223. $val = substr($val, 1, -1);
  224. $optional = true;
  225. } else {
  226. $optional = false;
  227. }
  228. if (0 === strpos($val, ':')) {
  229. // URL变量
  230. $name = substr($val, 1);
  231. if (!$optional && !isset($m1[$key])) {
  232. return false;
  233. }
  234. if (isset($m1[$key]) && isset($pattern[$name])) {
  235. // 检查变量规则
  236. if ($pattern[$name] instanceof \Closure) {
  237. $result = call_user_func_array($pattern[$name], [$m1[$key]]);
  238. if (false === $result) {
  239. return false;
  240. }
  241. } elseif (!preg_match(0 === strpos($pattern[$name], '/') ? $pattern[$name] : '/^' . $pattern[$name] . '$/', $m1[$key])) {
  242. return false;
  243. }
  244. }
  245. $var[$name] = isset($m1[$key]) ? $m1[$key] : '';
  246. } elseif (!isset($m1[$key]) || 0 !== strcasecmp($val, $m1[$key])) {
  247. return false;
  248. }
  249. }
  250. // 成功匹配后返回URL中的动态变量数组
  251. return $var;
  252. }
  253. }