Bladeren bron

Unify and faster CLI SAPI validation

Joanhey 6 jaren geleden
bovenliggende
commit
d3f97a6f46
3 gewijzigde bestanden met toevoegingen van 16 en 14 verwijderingen
  1. 2 0
      Lib/Constants.php
  2. 9 9
      Protocols/Http.php
  3. 5 5
      Worker.php

+ 2 - 0
Lib/Constants.php

@@ -31,6 +31,8 @@ define('WORKERMAN_SEND_FAIL', 2);
 define('OS_TYPE_LINUX', 'linux');
 define('OS_TYPE_WINDOWS', 'windows');
 
+define('NO_CLI', PHP_SAPI !== 'cli'); 
+
 // Compatible with php7
 if(!class_exists('Error'))
 {

+ 9 - 9
Protocols/Http.php

@@ -273,7 +273,7 @@ class Http
      */
     public static function header($content, $replace = true, $http_response_code = null)
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             \header($content, $replace, $http_response_code);
             return;
         }
@@ -312,7 +312,7 @@ class Http
      */
     public static function headerRemove($name)
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             \header_remove($name);
             return;
         }
@@ -355,7 +355,7 @@ class Http
         $secure = false,
         $HTTPOnly = false
     ) {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
         }
 
@@ -389,7 +389,7 @@ class Http
      */
     public static function sessionId($id = null)
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             return $id ? \session_id($id) : \session_id();
         }
         if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
@@ -407,7 +407,7 @@ class Http
      */
     public static function sessionName($name = null)
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             return $name ? \session_name($name) : \session_name();
         }
         $session_name = HttpCache::$sessionName;
@@ -426,7 +426,7 @@ class Http
      */
     public static function sessionSavePath($path = null)
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             return $path ? \session_save_path($path) : \session_save_path();
         }
         if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
@@ -454,7 +454,7 @@ class Http
      */
     public static function sessionStart()
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             return \session_start();
         }
 
@@ -503,7 +503,7 @@ class Http
      */
     public static function sessionWriteClose()
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             \session_write_close();
             return true;
         }
@@ -524,7 +524,7 @@ class Http
      */
     public static function end($msg = '')
     {
-        if (PHP_SAPI !== 'cli') {
+        if (NO_CLI) {
             exit($msg);
         }
         if ($msg) {

+ 5 - 5
Worker.php

@@ -542,8 +542,8 @@ class Worker
     protected static function checkSapiEnv()
     {
         // Only for cli.
-        if (PHP_SAPI !== 'cli') {
-            exit("only run in command line mode \n");
+        if (NO_CLI) {
+            exit("Only run in command line mode \n");
         }
         if (DIRECTORY_SEPARATOR === '\\') {
             self::$_OS = OS_TYPE_WINDOWS;
@@ -2150,10 +2150,10 @@ class Worker
 
         if (static::$_OS === OS_TYPE_LINUX && version_compare(PHP_VERSION,'7.0.0', 'ge')) {
             $php_uname = strtolower(php_uname('s'));
-            // If not Mac OS then turn reusePort on.
+        // If not Mac OS then turn reusePort on.
             if ($php_uname !== 'darwin') {
-                $this->reusePort = true;
-            }
+            $this->reusePort = true;
+        }
         }
 
         // Context for socket.