Ver Fonte

Update:cli 模式下的参数问题

Jinhui Zhu há 4 anos atrás
pai
commit
5f7a0c8a26
7 ficheiros alterados com 102 adições e 62 exclusões
  1. 5 5
      src/Application.php
  2. 39 29
      src/Base/Request.php
  3. 4 4
      src/Driver/Base.php
  4. 4 2
      src/Qii.php
  5. 18 18
      src/Request/Http.php
  6. 1 1
      src/Request/Url.php
  7. 31 3
      src/Request/Url/Base.php

+ 5 - 5
src/Application.php

@@ -46,6 +46,10 @@ class Application
      * 分发器
      */
     public $dispatcher = null;
+    /**
+     * @var mixed $helper
+     */
+    public $helper;
 
     public function __construct()
     {
@@ -178,11 +182,7 @@ class Application
         if ($env == '') $env = $this->getEnv();
         $ini = Psr4::getInstance()->getFileByPrefix($ini);
         $this->setAppIniFile($ini);
-        if (!Register::setAppConfigure(
-            $ini,
-            $env
-        )
-        ) throw new \Qii\Exceptions\FileNotFound($ini, 404);
+        if (!Register::setAppConfigure($ini, $env)) throw new \Qii\Exceptions\FileNotFound($ini, 404);
         //载入request方法
         $this->request = Psr4::getInstance()->loadClass('\Qii\Request\Http');
         Setting::getInstance()->setDefaultTimeZone();

+ 39 - 29
src/Base/Request.php

@@ -1,4 +1,5 @@
 <?php
+
 namespace Qii\Base;
 
 abstract class Request
@@ -33,6 +34,7 @@ abstract class Request
      * @var Qii_Request_Url url
      */
     public $url;
+
     /**
      * 初始化参数,获取链接中对应的参数并存放到$this->params中
      *
@@ -45,26 +47,29 @@ abstract class Request
         $rewriteRule = \Qii::getInstance()->appConfigure(\Qii\Config\Consts::APP_SITE_METHOD);
         $this->url = new \Qii\Request\Url($rewriteRule);
         $this->host = IS_CLI ? '' : $_SERVER['HTTP_HOST'];
-        $params = (array)$this->url->getParams();
-        if(count($params) > 0) $this->params = array_merge($this->params, $params);
-        $routeInfo = $this->url->getPathArgs();
-		$controller = $this->defaultController();
-		$action = $this->defaultAction();
-		if(count($routeInfo) > 1)
-		{
-			$action = array_pop($routeInfo);
-			$controller = join("\\", $routeInfo);
-		}
-		else if(count($routeInfo) == 1 && !empty($routeInfo[0])) {
-			$controller = $routeInfo[0];
-		}
+        $params = (array)$this->url->request->params();
+        if (count($params) > 0) $this->params = array_merge($this->params, $params);
+        $routeInfo = $this->url->request->getPathArgs();
+        $controller = $this->defaultController();
+        $action = $this->defaultAction();
+        //cli 模式处理
+        if(IS_CLI) {
+            $routeInfo = $this->url->request->getCliPathArgs();
+        }
+        if (count($routeInfo) > 1) {
+            $action = array_pop($routeInfo);
+            $controller = join("\\", $routeInfo);
+        } else if (count($routeInfo) == 1 && !empty($routeInfo[0])) {
+            $controller = $routeInfo[0];
+        }
         //处理url中的数据
-        if(ucwords($rewriteRule) == 'Short'){
+        if (ucwords($rewriteRule) == 'Short') {
             $this->setControllerName($controller);
             $this->setActionName($action);
         }
         return $this;
     }
+
     /**
      * 获取POST数据
      */
@@ -72,6 +77,7 @@ abstract class Request
     {
         return call_user_func_array(array($this->url, 'post'), func_get_args());
     }
+
     /**
      * 默认controller
      */
@@ -79,6 +85,7 @@ abstract class Request
     {
         return \Qii\Config\Register::get(\Qii\Config\Consts::APP_DEFAULT_CONTROLLER, 'index');
     }
+
     /**
      * 默认action
      */
@@ -98,7 +105,7 @@ abstract class Request
     public function redirect($url)
     {
         ob_clean();
-        header('Location:'. $url);
+        header('Location:' . $url);
     }
 
     /**
@@ -524,6 +531,7 @@ abstract class Request
         }
         return false;
     }
+
     /**
      * 是否需要转发
      */
@@ -531,6 +539,7 @@ abstract class Request
     {
         return $this->forward;
     }
+
     /**
      * 设置是否需要转发
      * @param bool $flag
@@ -549,16 +558,16 @@ abstract class Request
      * __setbaseUri
      *
      * @param string $baseUri
-     * @param string $request_uri
+     * @param string $requestUri
      * @return boolean
      */
-    protected function _setbaseUri($baseUri, $request_uri = null)
+    protected function _setBaseUri($baseUri, $requestUri = null)
     {
         if ($baseUri && is_string($baseUri)) {
             $this->_baseUri = $baseUri;
 
             return true;
-        } elseif ($request_uri && is_string($request_uri)) {
+        } elseif ($requestUri && is_string($requestUri)) {
             $scriptFileName = $this->getServer('SCRIPT_FILENAME');
 
             do {
@@ -566,21 +575,21 @@ abstract class Request
                     $fileName = basename($scriptFileName, \Qii::getInstance()->appConfigure('ext', '.php'));
                     $fileNameLen = strlen($fileName);
 
-                    $script_name = $this->getServer('SCRIPT_NAME');
-                    if ($script_name && is_string($script_name)) {
-                        $script = basename($script_name);
+                    $scriptName = $this->getServer('SCRIPT_NAME');
+                    if ($scriptName && is_string($scriptName)) {
+                        $script = basename($scriptName);
 
                         if (strncmp($fileName, $script, $fileNameLen) == 0) {
-                            $basename = $script_name;
+                            $basename = $scriptName;
                             break;
                         }
                     }
 
-                    $phpself_name = $this->getServer('PHP_SELF');
-                    if ($phpself_name && is_string($phpself_name)) {
-                        $phpself = basename($phpself_name);
-                        if (strncmp($fileName, $phpself, $fileNameLen) == 0) {
-                            $basename = $phpself_name;
+                    $phpSelfName = $this->getServer('PHP_SELF');
+                    if ($phpSelfName && is_string($phpSelfName)) {
+                        $phpSelf = basename($phpSelfName);
+                        if (strncmp($fileName, $phpSelf, $fileNameLen) == 0) {
+                            $basename = $phpSelfName;
                             break;
                         }
                     }
@@ -596,14 +605,14 @@ abstract class Request
                 }
             } while (0);
 
-            if ($basename && strstr($request_uri, $basename) == $request_uri) {
+            if ($basename && strstr($requestUri, $basename) == $requestUri) {
                 $this->_baseUri = rtrim($basename, '/');
 
                 return true;
             } elseif ($basename) {
                 $dirname = rtrim(dirname($basename), '/');
                 if ($dirname) {
-                    if (strstr($request_uri, $dirname) == $request_uri) {
+                    if (strstr($requestUri, $dirname) == $requestUri) {
                         $this->_baseUri = $dirname;
 
                         return true;
@@ -618,6 +627,7 @@ abstract class Request
 
         return false;
     }
+
     /**
      * 对于不存在的方法默认调用url中的方法
      */

+ 4 - 4
src/Driver/Base.php

@@ -224,7 +224,7 @@ class Base
         $this->whereCondition = array();
 
         $alias = $this->getTableAlias($table);
-        $this->modelSQL = $sql = sprintf($this->_query['UPDATE'], "`". $alias['name'] ."` ". $alias['alias']) . $set. $where;
+        $this->modelSQL = $sql = sprintf($this->_query['UPDATE'], $alias['name'] . $alias['alias']) . $set. $where;
         return $this->exec($sql);
     }
     /**
@@ -258,9 +258,9 @@ class Base
         $where = count($this->whereCondition) > 0 ? " WHERE ". join(" ", $this->whereCondition) : "";
         $this->whereCondition = array();
 
-        $this->modelSQL = $sql = sprintf($this->_query['DELETE'], $table, $where);
-
-        return $sql;
+        $alias = $this->getTableAlias($table);
+        $this->modelSQL = $sql = sprintf($this->_query['DELETE'], $alias['name'], $where);
+        return $this->exec($sql);
     }
 
     /**

+ 4 - 2
src/Qii.php

@@ -32,7 +32,8 @@ define('QII_SPACE', IS_CLI ? ' ' : '&nbsp;');
 require Qii_DIR . DS . 'Autoloader' . DS . 'Import.php';
 \Qii\Autoloader\Import::setFileLoaded(Qii_DIR . DS . 'Autoloader' . DS . 'Import.php');
 
-\Qii\Autoloader\Import::requires(array(Qii_DIR . DS . 'Consts' . DS . 'Config.php',
+\Qii\Autoloader\Import::requires(
+    array(Qii_DIR . DS . 'Consts' . DS . 'Config.php',
         Qii_DIR . DS . 'Functions' . DS . 'Funcs.php',
         Qii_DIR . DS . 'Autoloader' . DS . 'Factory.php',
         Qii_DIR . DS . 'Application.php',
@@ -181,7 +182,8 @@ if (!function_exists('catch_fatal_error')) {
             $message[] = 'Error line : ' . $error['line'] . ' on ' . \Qii\Exceptions\Errors::getLineMessage($error['file'], $error['line']);
             $message[] = 'Error description : ' . $error['message'];
             if (IS_CLI) {
-                return (new \Qii\Response\Cli())->stdout(
+                $cli = new \Qii\Response\Cli();
+                return $cli->stdout(
                     str_replace("&nbsp;"
                         , " "
                         , strip_tags(join(PHP_EOL, preg_replace("/[\n|\r\n]/", PHP_EOL, $message)))

+ 18 - 18
src/Request/Http.php

@@ -9,10 +9,10 @@ final class Http extends Request
     /**
      * __construct
      *
-     * @param string $request_uri
-     * @param string $base_uri
+     * @param string $requestURI
+     * @param string $baseURI
      */
-    public function __construct($request_uri = null, $base_uri = null)
+    public function __construct($requestURI = null, $baseURI = null)
     {
         if (isset($_SERVER['REQUEST_METHOD'])) {
             $this->method = $_SERVER['REQUEST_METHOD'];
@@ -23,12 +23,12 @@ final class Http extends Request
                 $this->method = 'Unknown';
             }
         }
-        if (empty($request_uri)) {
+        if (empty($requestURI)) {
             do {
                 // #ifdef PHP_WIN32
                 if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                     /* check this first so IIS will catch */
-                    if ($request_uri = $this->getServer('HTTP_X_REWRITE_URL')) {
+                    if ($requestURI = $this->getServer('HTTP_X_REWRITE_URL')) {
                         break;
                     }
 
@@ -36,32 +36,32 @@ final class Http extends Request
                     if ($rewrited = (boolean)$this->getServer('IIS_WasUrlRewritten')) {
                         $unencode = $this->getServer('UNENCODED_URL');
                         if ($unencode && is_string($unencode)) {
-                            $request_uri = $unencode;
+                            $requestURI = $unencode;
                         }
                         break;
                     }
                     // #endif
                 }
-                if ($request_uri = $this->getServer('PATH_INFO')) {
+                if ($requestURI = $this->getServer('PATH_INFO')) {
                     break;
                 }
 
-                if ($request_uri = $this->getServer('REQUEST_URI')) {
+                if ($requestURI = $this->getServer('REQUEST_URI')) {
                     /* Http proxy reqs setup request uri with scheme and host [and port] + the url path, only use url path */
-                    if (strstr($request_uri, 'http') == $request_uri) {
-                        $url_info = parse_url($request_uri);
+                    if (strstr($requestURI, 'http') == $requestURI) {
+                        $url_info = parse_url($requestURI);
                         if ($url_info && isset($url_info['path'])) {
-                            $request_uri = $url_info['path'];
+                            $requestURI = $url_info['path'];
                         }
                     } else {
-                        if ($pos = strstr($request_uri, '?')) {
-                            $request_uri = substr($request_uri, 0, strlen($pos) - 1);
+                        if ($pos = strstr($requestURI, '?')) {
+                            $requestURI = substr($requestURI, 0, strlen($pos) - 1);
                         }
                     }
                     break;
                 }
 
-                if ($request_uri = $this->getServer('ORIG_PATH_INFO')) {
+                if ($requestURI = $this->getServer('ORIG_PATH_INFO')) {
                     /* intended do nothing */
                     /*
                     if ($query = $this->getServer('QUERY_STRING')) {
@@ -72,12 +72,12 @@ final class Http extends Request
 
             } while (0);
         }
-        if ($request_uri && is_string($request_uri)) {
-            $request_uri = str_replace('//', '/', $request_uri);
-            $this->uri = $request_uri;
+        if ($requestURI && is_string($requestURI)) {
+            $requestURI = str_replace('//', '/', $requestURI);
+            $this->uri = $requestURI;
 
             // request_set_base_uri
-            $this->_setbaseUri($base_uri, $request_uri);
+            $this->_setBaseUri($baseURI, $requestURI);
         }
         $this->params = array();
         parent::__construct();

+ 1 - 1
src/Request/Url.php

@@ -41,7 +41,7 @@ class Url
     public static function getPathInfo()
     {
         if (PATH_INFO) {
-            return PATH_INFO;
+            return explode("?", PATH_INFO)[0];
         }
         
         $path = pathinfo($_SERVER['SCRIPT_NAME'], PATHINFO_DIRNAME);

+ 31 - 3
src/Request/Url/Base.php

@@ -255,7 +255,7 @@ abstract class Base
      *
      * @param string $key
      */
-    protected function CLIParams($key = '')
+    public function CliParams($key = '')
     {
         $argv = array();
         if (isset($_SERVER['argv'])) $argv = $_SERVER['argv'];
@@ -263,8 +263,19 @@ abstract class Base
         if ($argv && $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']) {
             if (count($argv) == 1) return;
             array_shift($argv);
-            $args = (array)$this->parseArgs($argv[0]);
+            $path = $argv[0];
+            if($query = stristr($path, '?')) {
+                $path = str_replace(stristr($path, '?'), '', $path);
+            }
+            $args = (array)$this->parseArgs($path);
             //处理GET或POST方法 数据结构 key1=value1 key2=value2 键和值中间不能有空格
+            if($query && strlen($query) > 1) {
+                $queryArr = explode("&", substr($query, 1));
+                foreach ($queryArr as $value) {
+                    list($index, $val) = explode('=', $value, 2);
+                    $args[$index] = $val;
+                }
+            }
             if ($_SERVER['argc'] > 2) {
                 for ($i = 1; $i < $_SERVER['argc'] - 1; $i++) {
                     list($index, $val) = explode('=', $argv[$i], 2);
@@ -278,6 +289,23 @@ abstract class Base
         }
     }
 
+    public function getCliPathArgs() {
+        $argv = array();
+        $args = array();
+        if (isset($_SERVER['argv'])) $argv = $_SERVER['argv'];
+        //修正部分服务器Rewrite 后再加参数不识别的问题(直接进入命令行的模式)
+        if ($argv && $_SERVER['PHP_SELF'] == $_SERVER['SCRIPT_NAME']) {
+            if (count($argv) == 1) return $args;
+            array_shift($argv);
+            $path = $argv[0];
+            if(stristr($path, '?')) {
+                $path = str_replace(stristr($path, '?'), '', $path);
+            }
+            $args = (array)$this->parseArgs($path);
+        }
+        return $args;
+    }
+
     /**
      * 获取参数
      *
@@ -291,7 +319,7 @@ abstract class Base
         if ($this->params != null) return $this->params;
         $this->checkMode($this->_mode);
         //如果是命令行模式
-        if (IS_CLI) return $this->CLIParams($key);
+        if (IS_CLI) return $this->CliParams($key);
 
         if ($this->_mode == 'Normal') {
             if (empty($key)) return $_GET;