PageRenderTime 43ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/xianpipa/ThinkPHP/Library/Think/Route.class.php

https://gitlab.com/fangfangchen/xianpipa
PHP | 316 lines | 262 code | 8 blank | 46 comment | 51 complexity | 1a61e767e9b441b52780334d0bfa4fe8 MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK IT ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006-2014 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;
  12. /**
  13. * ThinkPHP路由解析类
  14. */
  15. class Route {
  16. // 路由检测
  17. public static function check(){
  18. $depr = C('URL_PATHINFO_DEPR');
  19. $regx = preg_replace('/\.'.__EXT__.'$/i','',trim($_SERVER['PATH_INFO'],$depr));
  20. // 分隔符替换 确保路由定义使用统一的分隔符
  21. if('/' != $depr){
  22. $regx = str_replace($depr,'/',$regx);
  23. }
  24. // URL映射定义(静态路由)
  25. $maps = C('URL_MAP_RULES');
  26. if(isset($maps[$regx])) {
  27. $var = self::parseUrl($maps[$regx]);
  28. $_GET = array_merge($var, $_GET);
  29. return true;
  30. }
  31. // 动态路由处理
  32. $routes = C('URL_ROUTE_RULES');
  33. if(!empty($routes)) {
  34. foreach ($routes as $rule=>$route){
  35. if(is_numeric($rule)){
  36. // 支持 array('rule','adddress',...) 定义路由
  37. $rule = array_shift($route);
  38. }
  39. if(is_array($route) && isset($route[2])){
  40. // 路由参数
  41. $options = $route[2];
  42. if(isset($options['ext']) && __EXT__ != $options['ext']){
  43. // URL后缀检测
  44. continue;
  45. }
  46. if(isset($options['method']) && REQUEST_METHOD != strtoupper($options['method'])){
  47. // 请求类型检测
  48. continue;
  49. }
  50. // 自定义检测
  51. if(!empty($options['callback']) && is_callable($options['callback'])) {
  52. if(false === call_user_func($options['callback'])) {
  53. continue;
  54. }
  55. }
  56. }
  57. if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) { // 正则路由
  58. if($route instanceof \Closure) {
  59. // 执行闭包
  60. $result = self::invokeRegx($route, $matches);
  61. // 如果返回布尔值 则继续执行
  62. return is_bool($result) ? $result : exit;
  63. }else{
  64. return self::parseRegex($matches,$route,$regx);
  65. }
  66. }else{ // 规则路由
  67. $len1 = substr_count($regx,'/');
  68. $len2 = substr_count($rule,'/');
  69. if($len1>=$len2 || strpos($rule,'[')) {
  70. if('$' == substr($rule,-1,1)) {// 完整匹配
  71. if($len1 != $len2) {
  72. continue;
  73. }else{
  74. $rule = substr($rule,0,-1);
  75. }
  76. }
  77. $match = self::checkUrlMatch($regx,$rule);
  78. if(false !== $match) {
  79. if($route instanceof \Closure) {
  80. // 执行闭包
  81. $result = self::invokeRule($route, $match);
  82. // 如果返回布尔值 则继续执行
  83. return is_bool($result) ? $result : exit;
  84. }else{
  85. return self::parseRule($rule,$route,$regx);
  86. }
  87. }
  88. }
  89. }
  90. }
  91. }
  92. return false;
  93. }
  94. // 检测URL和规则路由是否匹配
  95. private static function checkUrlMatch($regx,$rule) {
  96. $m1 = explode('/',$regx);
  97. $m2 = explode('/',$rule);
  98. $var = array();
  99. foreach ($m2 as $key=>$val){
  100. if(0 === strpos($val,'[:')){
  101. $val = substr($val,1,-1);
  102. }
  103. if(':' == substr($val,0,1)) {// 动态变量
  104. if($pos = strpos($val,'|')){
  105. // 使用函数过滤
  106. $val = substr($val,1,$pos-1);
  107. }
  108. if(strpos($val,'\\')) {
  109. $type = substr($val,-1);
  110. if('d'==$type) {
  111. if(isset($m1[$key]) && !is_numeric($m1[$key]))
  112. return false;
  113. }
  114. $name = substr($val, 1, -2);
  115. }elseif($pos = strpos($val,'^')){
  116. $array = explode('-',substr(strstr($val,'^'),1));
  117. if(in_array($m1[$key],$array)) {
  118. return false;
  119. }
  120. $name = substr($val, 1, $pos - 1);
  121. }else{
  122. $name = substr($val, 1);
  123. }
  124. $var[$name] = isset($m1[$key])?$m1[$key]:'';
  125. }elseif(0 !== strcasecmp($val,$m1[$key])){
  126. return false;
  127. }
  128. }
  129. // 成功匹配后返回URL中的动态变量数组
  130. return $var;
  131. }
  132. // 解析规范的路由地址
  133. // 地址格式 [控制器/操作?]参数1=值1&参数2=值2...
  134. private static function parseUrl($url) {
  135. $var = array();
  136. if(false !== strpos($url,'?')) { // [控制器/操作?]参数1=值1&参数2=值2...
  137. $info = parse_url($url);
  138. $path = explode('/',$info['path']);
  139. parse_str($info['query'],$var);
  140. }elseif(strpos($url,'/')){ // [控制器/操作]
  141. $path = explode('/',$url);
  142. }else{ // 参数1=值1&参数2=值2...
  143. parse_str($url,$var);
  144. }
  145. if(isset($path)) {
  146. $var[C('VAR_ACTION')] = array_pop($path);
  147. if(!empty($path)) {
  148. $var[C('VAR_CONTROLLER')] = array_pop($path);
  149. }
  150. if(!empty($path)) {
  151. $var[C('VAR_MODULE')] = array_pop($path);
  152. }
  153. }
  154. return $var;
  155. }
  156. // 解析规则路由
  157. // '路由规则'=>'[控制器/操作]?额外参数1=值1&额外参数2=值2...'
  158. // '路由规则'=>array('[控制器/操作]','额外参数1=值1&额外参数2=值2...')
  159. // '路由规则'=>'外部地址'
  160. // '路由规则'=>array('外部地址','重定向代码')
  161. // 路由规则中 :开头 表示动态变量
  162. // 外部地址中可以用动态变量 采用 :1 :2 的方式
  163. // 'news/:month/:day/:id'=>array('News/read?cate=1','status=1'),
  164. // 'new/:id'=>array('/new.php?id=:1',301), 重定向
  165. private static function parseRule($rule,$route,$regx) {
  166. // 获取路由地址规则
  167. $url = is_array($route)?$route[0]:$route;
  168. // 获取URL地址中的参数
  169. $paths = explode('/',$regx);
  170. // 解析路由规则
  171. $matches = array();
  172. $rule = explode('/',$rule);
  173. foreach ($rule as $item){
  174. $fun = '';
  175. if(0 === strpos($item,'[:')){
  176. $item = substr($item,1,-1);
  177. }
  178. if(0===strpos($item,':')) { // 动态变量获取
  179. if($pos = strpos($item,'|')){
  180. // 支持函数过滤
  181. $fun = substr($item,$pos+1);
  182. $item = substr($item,0,$pos);
  183. }
  184. if($pos = strpos($item,'^') ) {
  185. $var = substr($item,1,$pos-1);
  186. }elseif(strpos($item,'\\')){
  187. $var = substr($item,1,-2);
  188. }else{
  189. $var = substr($item,1);
  190. }
  191. $matches[$var] = !empty($fun)? $fun(array_shift($paths)) : array_shift($paths);
  192. }else{ // 过滤URL中的静态变量
  193. array_shift($paths);
  194. }
  195. }
  196. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
  197. if(strpos($url,':')) { // 传递动态参数
  198. $values = array_values($matches);
  199. $url = preg_replace_callback('/:(\d+)/', function($match) use($values){ return $values[$match[1] - 1]; }, $url);
  200. }
  201. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  202. exit;
  203. }else{
  204. // 解析路由地址
  205. $var = self::parseUrl($url);
  206. // 解析路由地址里面的动态参数
  207. $values = array_values($matches);
  208. foreach ($var as $key=>$val){
  209. if(0===strpos($val,':')) {
  210. $var[$key] = $values[substr($val,1)-1];
  211. }
  212. }
  213. $var = array_merge($matches,$var);
  214. // 解析剩余的URL参数
  215. if(!empty($paths)) {
  216. preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths));
  217. }
  218. // 解析路由自动传入参数
  219. if(is_array($route) && isset($route[1])) {
  220. if(is_array($route[1])){
  221. $params = $route[1];
  222. }else{
  223. parse_str($route[1],$params);
  224. }
  225. $var = array_merge($var,$params);
  226. }
  227. $_GET = array_merge($var,$_GET);
  228. }
  229. return true;
  230. }
  231. // 解析正则路由
  232. // '路由正则'=>'[控制器/操作]?参数1=值1&参数2=值2...'
  233. // '路由正则'=>array('[控制器/操作]?参数1=值1&参数2=值2...','额外参数1=值1&额外参数2=值2...')
  234. // '路由正则'=>'外部地址'
  235. // '路由正则'=>array('外部地址','重定向代码')
  236. // 参数值和外部地址中可以用动态变量 采用 :1 :2 的方式
  237. // '/new\/(\d+)\/(\d+)/'=>array('News/read?id=:1&page=:2&cate=1','status=1'),
  238. // '/new\/(\d+)/'=>array('/new.php?id=:1&page=:2&status=1','301'), 重定向
  239. private static function parseRegex($matches,$route,$regx) {
  240. // 获取路由地址规则
  241. $url = is_array($route)?$route[0]:$route;
  242. $url = preg_replace_callback('/:(\d+)/', function($match) use($matches){return $matches[$match[1]];}, $url);
  243. if(0=== strpos($url,'/') || 0===strpos($url,'http')) { // 路由重定向跳转
  244. header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
  245. exit;
  246. }else{
  247. // 解析路由地址
  248. $var = self::parseUrl($url);
  249. // 处理函数
  250. foreach($var as $key=>$val){
  251. if(strpos($val,'|')){
  252. list($val,$fun) = explode('|',$val);
  253. $var[$key] = $fun($val);
  254. }
  255. }
  256. // 解析剩余的URL参数
  257. $regx = substr_replace($regx,'',0,strlen($matches[0]));
  258. if($regx) {
  259. preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
  260. $var[strtolower($match[1])] = strip_tags($match[2]);
  261. }, $regx);
  262. }
  263. // 解析路由自动传入参数
  264. if(is_array($route) && isset($route[1])) {
  265. if(is_array($route[1])){
  266. $params = $route[1];
  267. }else{
  268. parse_str($route[1],$params);
  269. }
  270. $var = array_merge($var,$params);
  271. }
  272. $_GET = array_merge($var,$_GET);
  273. }
  274. return true;
  275. }
  276. // 执行正则匹配下的闭包方法 支持参数调用
  277. static private function invokeRegx($closure, $var = array()) {
  278. $reflect = new \ReflectionFunction($closure);
  279. $params = $reflect->getParameters();
  280. $args = array();
  281. array_shift($var);
  282. foreach ($params as $param){
  283. if(!empty($var)) {
  284. $args[] = array_shift($var);
  285. }elseif($param->isDefaultValueAvailable()){
  286. $args[] = $param->getDefaultValue();
  287. }
  288. }
  289. return $reflect->invokeArgs($args);
  290. }
  291. // 执行规则匹配下的闭包方法 支持参数调用
  292. static private function invokeRule($closure, $var = array()) {
  293. $reflect = new \ReflectionFunction($closure);
  294. $params = $reflect->getParameters();
  295. $args = array();
  296. foreach ($params as $param){
  297. $name = $param->getName();
  298. if(isset($var[$name])) {
  299. $args[] = $var[$name];
  300. }elseif($param->isDefaultValueAvailable()){
  301. $args[] = $param->getDefaultValue();
  302. }
  303. }
  304. return $reflect->invokeArgs($args);
  305. }
  306. }