Эх сурвалжийг харах

Upgrade workerman to 4.x (#5535)

* upgrade workerman to 4.0.0

* fix server-async.php

* fix server-async.php

Co-authored-by: walkor <[email protected]>
walkor 5 жил өмнө
parent
commit
049f230c8c

+ 44 - 34
frameworks/PHP/workerman/app.php

@@ -1,5 +1,6 @@
 <?php
-use Workerman\Protocols\Http;
+use Workerman\Protocols\Http\Response;
+use Workerman\Protocols\Http\Request;
 
 function init()
 {
@@ -15,18 +16,20 @@ function init()
     $update    = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
 }
 
-function router()
+function router(Request $request)
 {
-    switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
+    switch ($request->path()) {
         case '/plaintext':
-            Http::header('Content-Type: text/plain');
-
-            return 'Hello, World!';
+            return new Response(200, [
+                'Content-Type' => 'text/plain',
+                'Date'         => Header::$date
+            ], 'Hello, World!');
 
         case '/json':
-            Http::header('Content-Type: application/json');
-
-            return json_encode(['message' => 'Hello, World!']);
+            return new Response(200, [
+                'Content-Type' => 'application/json',
+                'Date'         => Header::$date
+            ], json_encode(['message' => 'Hello, World!']));
 
         case '/db':
             return db();
@@ -36,41 +39,41 @@ function router()
             return fortune();
 
         case '/query':
-            return query();
+            return query($request);
 
         case '/update':
-            return updateraw();
-
-/*       case '/info':
-            Http::header('Content-Type: text/plain');
+            return updateraw($request);
+/*
+       case '/info':
             ob_start();
             phpinfo();
-            return ob_get_clean();
- */ 
+            return new Response(200, ['Content-Type' => 'text/plain'], ob_get_clean());
+*/
         default:
-            Http::responseCode(404);
-            return 'Error 404';
+            return new Response(404, [], 'Error 404');
     }
 }
 
 function db()
 {
     global $statement;
-    Http::header('Content-Type: application/json');
 
     $statement->execute([mt_rand(1, 10000)]);
 
-    return json_encode($statement->fetch());
+    return new Response(200, [
+        'Content-Type' => 'application/json',
+        'Date'         => Header::$date
+    ], json_encode($statement->fetch()));
 }
 
-function query()
+function query($request)
 {
     global $statement;
-    Http::header('Content-Type: application/json');
 
     $query_count = 1;
-    if ($_GET['q'] > 1) {
-        $query_count = min($_GET['q'], 500);
+    $q = $request->get('q');
+    if ($q > 1) {
+        $query_count = min($q, 500);
     }
 
     while ($query_count--) {
@@ -78,17 +81,20 @@ function query()
         $arr[] = $statement->fetch();
     }
 
-    return json_encode($arr);
+    return new Response(200, [
+        'Content-Type' => 'application/json',
+        'Date'         => Header::$date
+    ], json_encode($arr));
 }
 
-function updateraw()
+function updateraw($request)
 {
     global $random, $update;
-    Http::header('Content-Type: application/json');
 
     $query_count = 1;
-    if ($_GET['q'] > 1) {
-        $query_count = min($_GET['q'], 500);
+    $q = $request->get('q');
+    if ($q > 1) {
+        $query_count = min($q, 500);
     }
 
     while ($query_count--) {
@@ -107,8 +113,10 @@ function updateraw()
     //     $update->execute([$world['randomNumber'], $world['id']]);
     // }
     // $pdo->commit();
-
-    return json_encode($arr);
+    return new Response(200, [
+        'Content-Type' => 'application/json',
+        'Date'         => Header::$date
+    ], json_encode($arr));
 }
 
 function fortune()
@@ -127,7 +135,9 @@ function fortune()
         $html .= "<tr><td>$id</td><td>$message</td></tr>";
     }
 
-    return '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
-            .$html.
-            '</table></body></html>';
+    return new Response(200, [
+        'Date'         => Header::$date
+    ], '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>'
+        .$html.
+        '</table></body></html>');
 }

+ 9 - 10
frameworks/PHP/workerman/server-async.php

@@ -1,7 +1,8 @@
 <?php
 require_once __DIR__.'/vendor/autoload.php';
 
-use Workerman\Protocols\Http;
+use Workerman\Protocols\Http\Response;
+use Workerman\Protocols\Http\Request;
 use Workerman\Worker;
 
 $http_worker                = new Worker('http://0.0.0.0:8080');
@@ -25,18 +26,15 @@ $http_worker->onWorkerStart = static function() {
     $mysql->connect(function ($e) {});
 };
 
-$http_worker->onMessage = static function ($connection) {
+$http_worker->onMessage = static function ($connection, $request) {
 
     global $mysql;
 
-    Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
-
-    switch (parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH)) {
+    switch ($request->path()) {
         case '/db':
-            Http::header('Content-Type: application/json');
             $mysql->query('SELECT id,randomNumber FROM World WHERE id='.mt_rand(1, 10000),
                 static function ($command) use ($connection) {
-                    $connection->send(json_encode($command->resultRows, JSON_NUMERIC_CHECK));
+                    $connection->send(new Response(200, ['Content-Type' => 'application/json', 'Date' => gmdate('D, d M Y H:i:s').' GMT'], json_encode($command->resultRows, JSON_NUMERIC_CHECK)));
                 }
             );
             return;
@@ -57,7 +55,8 @@ $http_worker->onMessage = static function ($connection) {
                         $html .= "<tr><td>$id</td><td>$message</td></tr>";
                     }
 
-                    $connection->send($html.'</table></body></html>');
+                    $connection->send(new Response(200, ['Date' => gmdate('D, d M Y H:i:s').' GMT'], $html.'</table></body></html>'));
+
                 }
             );
             return;
@@ -73,8 +72,8 @@ $http_worker->onMessage = static function ($connection) {
         //   return $connection->send(ob_get_clean());
 
         default:
-            Http::responseCode(404);
-            $connection->send('Error 404');
+            $connection->send(new Response(200, [], 'Error 404'));
+
     }
 };
 

+ 5 - 8
frameworks/PHP/workerman/server.php

@@ -2,25 +2,22 @@
 require_once __DIR__.'/vendor/autoload.php';
 require_once __DIR__.'/app.php';
 
-use Workerman\Protocols\Http;
 use Workerman\Worker;
-use Workerman\Lib\Timer;
+use Workerman\Timer;
 
 $http_worker                = new Worker('http://0.0.0.0:8080');
 $http_worker->count         = (int) shell_exec('nproc') * 4;
 $http_worker->onWorkerStart = function () {
-    Header::$date = 'Date: '.gmdate('D, d M Y H:i:s').' GMT';
+    Header::$date = gmdate('D, d M Y H:i:s').' GMT';
     Timer::add(1, function() {
-        Header::$date = 'Date: '.gmdate('D, d M Y H:i:s').' GMT';
+        Header::$date = gmdate('D, d M Y H:i:s').' GMT';
     });
     init();
 };
 
-$http_worker->onMessage = static function ($connection) {
+$http_worker->onMessage = static function ($connection, $request) {
 
-    Http::header(Header::$date);
-
-    $connection->send(router());
+    $connection->send(router($request));
     
 };