123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316 |
- <?php
- namespace Think;
- class Route {
-
-
- public static function check(){
- $depr = C('URL_PATHINFO_DEPR');
- $regx = preg_replace('/\.'.__EXT__.'$/i','',trim($_SERVER['PATH_INFO'],$depr));
-
- if('/' != $depr){
- $regx = str_replace($depr,'/',$regx);
- }
-
- $maps = C('URL_MAP_RULES');
- if(isset($maps[$regx])) {
- $var = self::parseUrl($maps[$regx]);
- $_GET = array_merge($var, $_GET);
- return true;
- }
-
- $routes = C('URL_ROUTE_RULES');
- if(!empty($routes)) {
- foreach ($routes as $rule=>$route){
- if(is_numeric($rule)){
-
- $rule = array_shift($route);
- }
- if(is_array($route) && isset($route[2])){
-
- $options = $route[2];
- if(isset($options['ext']) && __EXT__ != $options['ext']){
-
- continue;
- }
- if(isset($options['method']) && REQUEST_METHOD != strtoupper($options['method'])){
-
- continue;
- }
-
- if(!empty($options['callback']) && is_callable($options['callback'])) {
- if(false === call_user_func($options['callback'])) {
- continue;
- }
- }
- }
- if(0===strpos($rule,'/') && preg_match($rule,$regx,$matches)) {
- if($route instanceof \Closure) {
-
- $result = self::invokeRegx($route, $matches);
-
- return is_bool($result) ? $result : exit;
- }else{
- return self::parseRegex($matches,$route,$regx);
- }
- }else{
- $len1 = substr_count($regx,'/');
- $len2 = substr_count($rule,'/');
- if($len1>=$len2 || strpos($rule,'[')) {
- if('$' == substr($rule,-1,1)) {
- if($len1 != $len2) {
- continue;
- }else{
- $rule = substr($rule,0,-1);
- }
- }
- $match = self::checkUrlMatch($regx,$rule);
- if(false !== $match) {
- if($route instanceof \Closure) {
-
- $result = self::invokeRule($route, $match);
-
- return is_bool($result) ? $result : exit;
- }else{
- return self::parseRule($rule,$route,$regx);
- }
- }
- }
- }
- }
- }
- return false;
- }
-
- private static function checkUrlMatch($regx,$rule) {
- $m1 = explode('/',$regx);
- $m2 = explode('/',$rule);
- $var = array();
- foreach ($m2 as $key=>$val){
- if(0 === strpos($val,'[:')){
- $val = substr($val,1,-1);
- }
-
- if(':' == substr($val,0,1)) {
- if($pos = strpos($val,'|')){
-
- $val = substr($val,1,$pos-1);
- }
- if(strpos($val,'\\')) {
- $type = substr($val,-1);
- if('d'==$type) {
- if(isset($m1[$key]) && !is_numeric($m1[$key]))
- return false;
- }
- $name = substr($val, 1, -2);
- }elseif($pos = strpos($val,'^')){
- $array = explode('-',substr(strstr($val,'^'),1));
- if(in_array($m1[$key],$array)) {
- return false;
- }
- $name = substr($val, 1, $pos - 1);
- }else{
- $name = substr($val, 1);
- }
- $var[$name] = isset($m1[$key])?$m1[$key]:'';
- }elseif(0 !== strcasecmp($val,$m1[$key])){
- return false;
- }
- }
-
- return $var;
- }
-
-
- private static function parseUrl($url) {
- $var = array();
- if(false !== strpos($url,'?')) {
- $info = parse_url($url);
- $path = explode('/',$info['path']);
- parse_str($info['query'],$var);
- }elseif(strpos($url,'/')){
- $path = explode('/',$url);
- }else{
- parse_str($url,$var);
- }
- if(isset($path)) {
- $var[C('VAR_ACTION')] = array_pop($path);
- if(!empty($path)) {
- $var[C('VAR_CONTROLLER')] = array_pop($path);
- }
- if(!empty($path)) {
- $var[C('VAR_MODULE')] = array_pop($path);
- }
- }
- return $var;
- }
-
-
-
-
-
-
-
-
-
- private static function parseRule($rule,$route,$regx) {
-
- $url = is_array($route)?$route[0]:$route;
-
- $paths = explode('/',$regx);
-
- $matches = array();
- $rule = explode('/',$rule);
- foreach ($rule as $item){
- $fun = '';
- if(0 === strpos($item,'[:')){
- $item = substr($item,1,-1);
- }
- if(0===strpos($item,':')) {
- if($pos = strpos($item,'|')){
-
- $fun = substr($item,$pos+1);
- $item = substr($item,0,$pos);
- }
- if($pos = strpos($item,'^') ) {
- $var = substr($item,1,$pos-1);
- }elseif(strpos($item,'\\')){
- $var = substr($item,1,-2);
- }else{
- $var = substr($item,1);
- }
- $matches[$var] = !empty($fun)? $fun(array_shift($paths)) : array_shift($paths);
- }else{
- array_shift($paths);
- }
- }
- if(0=== strpos($url,'/') || 0===strpos($url,'http')) {
- if(strpos($url,':')) {
- $values = array_values($matches);
- $url = preg_replace_callback('/:(\d+)/', function($match) use($values){ return $values[$match[1] - 1]; }, $url);
- }
- header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
- exit;
- }else{
-
- $var = self::parseUrl($url);
-
- $values = array_values($matches);
- foreach ($var as $key=>$val){
- if(0===strpos($val,':')) {
- $var[$key] = $values[substr($val,1)-1];
- }
- }
- $var = array_merge($matches,$var);
-
- if(!empty($paths)) {
- preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){ $var[strtolower($match[1])]=strip_tags($match[2]);}, implode('/',$paths));
- }
-
- if(is_array($route) && isset($route[1])) {
- if(is_array($route[1])){
- $params = $route[1];
- }else{
- parse_str($route[1],$params);
- }
- $var = array_merge($var,$params);
- }
- $_GET = array_merge($var,$_GET);
- }
- return true;
- }
-
-
-
-
-
-
-
-
- private static function parseRegex($matches,$route,$regx) {
-
- $url = is_array($route)?$route[0]:$route;
- $url = preg_replace_callback('/:(\d+)/', function($match) use($matches){return $matches[$match[1]];}, $url);
- if(0=== strpos($url,'/') || 0===strpos($url,'http')) {
- header("Location: $url", true,(is_array($route) && isset($route[1]))?$route[1]:301);
- exit;
- }else{
-
- $var = self::parseUrl($url);
-
- foreach($var as $key=>$val){
- if(strpos($val,'|')){
- list($val,$fun) = explode('|',$val);
- $var[$key] = $fun($val);
- }
- }
-
- $regx = substr_replace($regx,'',0,strlen($matches[0]));
- if($regx) {
- preg_replace_callback('/(\w+)\/([^\/]+)/', function($match) use(&$var){
- $var[strtolower($match[1])] = strip_tags($match[2]);
- }, $regx);
- }
-
- if(is_array($route) && isset($route[1])) {
- if(is_array($route[1])){
- $params = $route[1];
- }else{
- parse_str($route[1],$params);
- }
- $var = array_merge($var,$params);
- }
- $_GET = array_merge($var,$_GET);
- }
- return true;
- }
-
- static private function invokeRegx($closure, $var = array()) {
- $reflect = new \ReflectionFunction($closure);
- $params = $reflect->getParameters();
- $args = array();
- array_shift($var);
- foreach ($params as $param){
- if(!empty($var)) {
- $args[] = array_shift($var);
- }elseif($param->isDefaultValueAvailable()){
- $args[] = $param->getDefaultValue();
- }
- }
- return $reflect->invokeArgs($args);
- }
-
- static private function invokeRule($closure, $var = array()) {
- $reflect = new \ReflectionFunction($closure);
- $params = $reflect->getParameters();
- $args = array();
- foreach ($params as $param){
- $name = $param->getName();
- if(isset($var[$name])) {
- $args[] = $var[$name];
- }elseif($param->isDefaultValueAvailable()){
- $args[] = $param->getDefaultValue();
- }
- }
- return $reflect->invokeArgs($args);
- }
- }
|