Browse Source

Fixed argv问题

朱金辉 2 năm trước cách đây
mục cha
commit
6998afa958
6 tập tin đã thay đổi với 31 bổ sung13 xóa
  1. 8 2
      _cli.php
  2. 2 2
      src/Autoloader/Psr4.php
  3. 2 2
      src/Base/Route.php
  4. 13 4
      src/Functions/Funcs.php
  5. 0 2
      src/Language/Loader.php
  6. 6 1
      src/Qii.php

+ 8 - 2
_cli.php

@@ -310,5 +310,11 @@ class cmd
     }
 }
 
-array_shift($argv);
-new cmd($argv);
+$args = is_array($argv) && count($argv) > 0 ? $argv : array();
+if(count($args) == 0 && isset($_SERVER['argv'])
+    && is_array($_SERVER['argv']) && count($_SERVER['argv']) > 0) {
+    $args = $_SERVER['argv'];
+}
+array_shift($args);
+
+new cmd($args);

+ 2 - 2
src/Autoloader/Psr4.php

@@ -36,14 +36,14 @@ class Psr4
      */
     const APP_LOAD_PREFIX = '__qii_psr4_instance';
     /**
-     * @var $_loadedClass 保存加载过的类
+     * @var array $_loadedClass 保存加载过的类
      */
     protected static $_loadedClass = array();
 
     protected static $_loadedClassParams = array();
 
     /**
-     * @var $_realpath 将转换后的路径存放到此变量中
+     * @var array $_realpath 将转换后的路径存放到此变量中
      */
     protected static $_realpath = array();
 

+ 2 - 2
src/Base/Route.php

@@ -152,10 +152,10 @@ class Route
         foreach ($route as $item) {
             $middleware = $item[0];
             $subRoute = $item[1];
-            if(!isset($subRoute[$method]) && !is_set($subRoute['ANY'])) {
+            if(!isset($subRoute[$method]) && !isset($subRoute['ANY'])) {
                 continue;
             }
-            $routes = array_merge($subRoute[$method], $subRoute['ANY']);
+            $routes = array_merge((array) $subRoute[$method], (array) $subRoute['ANY']);
             $callable = '';
 
             foreach ($routes as $routeUri) {

+ 13 - 4
src/Functions/Funcs.php

@@ -141,6 +141,15 @@ function _require($files)
 	return \Qii\Autoloader\Import::requires($files);
 }
 
+/**
+ * 检测字符的编码
+ *
+ * @param string $str
+ * @return false|string
+ */
+function detectEncode($str) {
+    return mb_detect_encoding($str, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
+}
 /**
  * 将字符串转换成指定编码
  *
@@ -150,7 +159,7 @@ function _require($files)
  */
 function convertCode($str, $to)
 {
-    $fromCode = mb_detect_encoding($str, array('ASCII', 'UTF-8', 'GB2312', 'GBK', 'BIG5'));
+    $fromCode = detectEncode($str);
     if($fromCode == $to) return $str;
     return mb_convert_encoding($str, $to, $fromCode);
 }
@@ -184,7 +193,7 @@ function toGBK($str)
  * @param string $needle
  * @return bool
  */
-function str_contains(string $haystack, string $needle)
+function strContains(string $haystack, string $needle)
 {
     return '' === $needle || false !== strpos($haystack, $needle);
 }
@@ -195,7 +204,7 @@ function str_contains(string $haystack, string $needle)
  * @param string $needle
  * @return bool
  */
-function str_starts_with($haystack, $needle)
+function strStartsWith($haystack, $needle)
 {
     return 0 === strncmp($haystack, $needle, \strlen($needle));
 }
@@ -206,7 +215,7 @@ function str_starts_with($haystack, $needle)
  * @param string $needle
  * @return bool
  */
-function str_ends_with($haystack, $needle)
+function strEndsWith($haystack, $needle)
 {
     if ('' === $needle || $needle === $haystack) {
         return true;

+ 0 - 2
src/Language/Loader.php

@@ -63,8 +63,6 @@ class Loader
 
 		//先获取语言配置信息
 		$language = Import::includes($dir . 'i18n' . DS . 'language.php');
-		//如果是cli模式就使用英文
-		if(IS_CLI) $language = "EN";
 		$fileName = $dir . 'i18n' . DS . $language . DS . $package . '.php';
 		if (isset($this->loaded[$fileName])) return;
 		$this->loaded[$fileName] = true;

+ 6 - 1
src/Qii.php

@@ -25,7 +25,12 @@ 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));
+    $args = is_array($argv) && count($argv) > 0 ? $argv : array();
+    if(count($args) == 0 && isset($_SERVER['argv'])
+        && is_array($_SERVER['argv']) && count($_SERVER['argv']) > 0) {
+        $args = $_SERVER['argv'];
+    }
+    define('PATH_INFO', array_pop($args));
 } else {
     define('PATH_INFO', isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : '');
 }