Ver código fonte

更新parse route

Zhu Jinhui 7 anos atrás
pai
commit
805e80304a

+ 1 - 0
Qii/Application.php

@@ -338,6 +338,7 @@ class Application
         $rewrite = Psr4::loadStatic(
             '\Qii\Route\Parse',
             'get',
+            \Qii\Request\Url::getPathInfo(),
             $this->request->controller,
             $this->request->action,
             $this->request->url->get(2)

+ 3 - 4
Qii/Autoloader/Helper.php

@@ -27,8 +27,8 @@ class Helper
      */
     public function __get($name)
     {
-        if (substr($name, 0, 7) == 'Helper\\') return $this->get($name);
-        return $this->get('Helper\\' . $name);
+        if (substr($name, 0, 7) == 'helper\\') return $this->get($name);
+        return $this->get('helper\\' . $name);
     }
 
     /**
@@ -58,8 +58,7 @@ class Helper
             if(\Qii\Autoloader\Import::requires($file)){
                 //如果里边包含class的话就将class注册到Qii::instance('class');
                 $className = 'helper\\'. pathinfo($file)['filename'];
-                //echo substr($file, stristr($file, 'helper'), strlen($file));
-                if (class_exists($className, false)) {
+                if (!isset(self::$helpers[$className]) && class_exists($className, false)) {
                     self::$helpers[$className] = \Qii\Autoloader\Psr4::getInstance()->instance($className);
                 }
             }

+ 2 - 0
Qii/Autoloader/Import.php

@@ -20,6 +20,7 @@ class Import
                 return self::requires($n);
             }, $file);
         }
+        $file = str_replace(array('\\', '/'), DS, $file);
         if (self::getFileLoaded($file)) return true;
         if (file_exists($file)) {
             self::setFileLoaded($file);
@@ -41,6 +42,7 @@ class Import
                 return self::includes($n);
             }, $file);
         }
+        $file = str_replace(array('\\', '/'), DS, $file);
         if (self::getIncludeFiles($file) !== null) self::getIncludeFiles($file);
         if (file_exists($file)) {
             $configure = include($file);

+ 6 - 0
Qii/Qii.php

@@ -17,6 +17,12 @@ define('PS', PATH_SEPARATOR);
 define('OS', strtoupper(substr(PHP_OS, 0, 3)));
 
 define('IS_CLI', php_sapi_name() == 'cli' ? true : false);
+if(IS_CLI) {
+    define('PATH_INFO', array_pop($argv));
+}else{
+    define('PATH_INFO', $_SERVER['PATH_INFO']);
+}
+
 /**
  * EOL 和 SPACE
  */

+ 9 - 0
Qii/Request/Url.php

@@ -33,6 +33,15 @@ class Url
         return self::$_instance;
     }
 
+    /**
+     * 获取当前请求的URL
+     *
+     * @return mixed
+     */
+    public static function getPathInfo()
+    {
+        return PATH_INFO;
+    }
 
     /**
      * 获取当前连接地址

+ 2 - 2
Qii/Route/Parse.php

@@ -30,7 +30,7 @@ class Parse
      * xx:* => yy:yyy xxx Controller转发到 yy->yyy
      * *:xxx => yy:yyy 所有Controller转发到 yy->yyy
      */
-    public static function get($controller, $action = '', $thirdParam = '')
+    public static function get($url, $controller, $action = '', $thirdParam = '')
     {
         if ($controller == 'Qii') {
             return array('controller' => $controller, 'action' => $action);
@@ -46,7 +46,7 @@ class Parse
         }
         $class = new $className();
         $class->setConfig($router);
-        return $class->parse($controller, $action, $thirdParam);
+        return $class->parse($url, $controller, $action, $thirdParam);
     }
 }
 

+ 40 - 49
Qii/Route/Parse/Normal.php

@@ -31,6 +31,7 @@ class Normal
 	/**
 	 * 路由转发, 转发对应的规则中xx不能为*
 	 *
+     * @param string $url url链接
 	 * @param String $controller
 	 * @param String $action
 	 * @return Array ($controller, $action);
@@ -42,58 +43,48 @@ class Normal
 	 * *:xxx => yy:yyy 所有Controller转发到 yy->yyy
 	 * xxx:*(yy):第三个参数 => {1}:* 转发xxx:yy => yy:第三个参数
 	 */
-	public function parse($controller, $action, $thirdParam = '')
+	public function parse($url, $controller, $action)
 	{
 		if (!$this->config) {
 			return array('controller' => $controller, 'action' => $action);
 		}
-		$routerArray = array();
-		if (is_array($this->config)) {
-			foreach ($this->config AS $key => $value) {
-				$keyArray = explode(":", $key);
-				$valueArray = explode(":", $value);
-				if (!isset($keyArray[1])) $keyArray[1] = '';
-				if (!isset($valueArray[1])) $valueArray[1] = '';
-				if ('' == $keyArray[1]) {
-					$keyArray[1] = "*";
-				}
-				$routerArray['controller'][$keyArray[0] . ":" . $keyArray[1]] = $valueArray[0];
-				if ($valueArray[1] == '*') $valueArray[1] = $action;
-				if ($keyArray[1] == "*") {
-					$routerArray['action'][$keyArray[0] . ":" . $keyArray[1]] = $valueArray[1];
-				} else {
-					$routerArray['action'][$keyArray[0] . ":" . $keyArray[1]] = $valueArray[1];
-				}
-			}
-		}
-		if (count($routerArray) == 0) {
-			return array('controller' => $controller, 'action' => $action);
-		}
-		if (isset($routerArray["controller"]["*:*"]) && '' != $routerArray["controller"]["*:*"])//*:*=>yyy:* or *:* => *:yyy mode
-		{
-			$controller = ($routerArray['controller']['*:*'] == '*' ? $controller : $routerArray["controller"]["*:*"]);
-			$action = ($routerArray['action']['*:*'] == '*' ? $action : $routerArray['action']['*:*']);
-		} elseif (isset($routerArray["action"][$controller . ":*"]) && '' != $routerArray["action"][$controller . ":*"])//xx:*=>yy:* mode
-		{
-			$action = $routerArray['action'][$controller . ":*"];
-			$controller = $routerArray["controller"][$controller . ":*"];
-			if (stristr($controller, '{1}')) {
-				$controller = str_replace('{1}', $action, $controller);
-				$action = $thirdParam ? $thirdParam : 'index';
-			}
-		} elseif (isset($routerArray["action"]["*:" . $action]) && '' != $routerArray["action"]["*:" . $action])//*:xxx=> yy:yyy mode
-		{
-			$controller = $routerArray["control"]["*:" . $action];
-			$action = $routerArray["action"]["*:" . $action];
-		} elseif (isset($routerArray["controller"][$controller . ":" . $action])) {
-			$tmpAction = $controller . ":" . $action;
-			$action = $routerArray["action"][$controller . ":" . $action];
-			$controller = $routerArray["controller"][$tmpAction];
-			if (stristr($action, '{1}')) {
-				$action = str_replace('{1}', $action, $thirdParam);
-			}
-		}
-		$action = !$action ? 'index' : $action;
-		return array('controller' => $controller, 'action' => $action);
+
+        $dirInfo = explode('/', pathinfo(ltrim($url, '/'), PATHINFO_DIRNAME));
+        $fileName = pathinfo(ltrim($url, '/'), PATHINFO_FILENAME);
+        $dirInfo[] = $fileName;
+        $dir = '';
+        $match = ['key' => '', 'val' => '', 'url' => $url];
+        foreach($dirInfo AS $path)
+        {
+            $dir[] = $path;
+            $joinPath = join($dir, ':') .":*";
+            if(isset($this->config[$joinPath]))
+            {
+                $config = $this->config[$joinPath];
+                //匹配最长的规则
+                if(strlen($config) > strlen($match['val'])) {
+                    $match = array_merge($match, ['key' => $joinPath, 'val' => $config]);
+                }
+            }
+        }
+        $match['dirInfo'] = $dirInfo;
+        //如果match到就解析match的内容
+        if($match['val']) {
+            $matches = explode(':', $match['val']);
+            $match['matches'] = $matches;
+            $action = array_pop($matches);
+            $controller = join('\\', $matches);
+            $controllerExplode = explode('\\', $controller);
+            if(stristr($controller, '{1}')){
+                $controller = join('\\', array_slice($dirInfo,0 , count($controllerExplode)));
+            }
+            $action = $action == '{1}' || $action == '*' ? isset($dirInfo[count($controllerExplode)]) ? $dirInfo[count($controllerExplode)] : 'index' : $action;
+            $match['controller'] = $controller;
+            $match['action'] = $action;
+        }else{
+            $match['controller'] = isset($dirInfo[0]) ? $dirInfo[0] : 'index';
+            $match['action'] = isset($dirInfo[1]) ? $dirInfo[1] : 'index';
+        }
+        return $match;
 	}
 }