From 69282bdbab0bad66b2cfdaf416bd18170e913466 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Fri, 7 Sep 2018 21:13:30 +0800 Subject: [PATCH 1/3] improve isWebsocketPushPacket function --- src/Concerns/InteractsWithWebsocket.php | 17 +++++++++++++++ src/Server/Manager.php | 8 ++----- tests/Server/ManagerTest.php | 28 +++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/Concerns/InteractsWithWebsocket.php b/src/Concerns/InteractsWithWebsocket.php index c22e1cfc..8f511e90 100644 --- a/src/Concerns/InteractsWithWebsocket.php +++ b/src/Concerns/InteractsWithWebsocket.php @@ -135,6 +135,23 @@ public function onClose($server, $fd, $reactorId) } } + /** + * Indicates if a packet is websocket push action. + * + * @param mixed + */ + protected function isWebsocketPushPacket($packet) + { + if ( !is_array($packet)) { + return false; + } + + return $this->isWebsocket + && array_key_exists('action', $packet) + && $packet['action'] === Websocket::PUSH_ACTION; + } + + /** * Push websocket message to clients. * diff --git a/src/Server/Manager.php b/src/Server/Manager.php index b5715d81..a5dc72be 100644 --- a/src/Server/Manager.php +++ b/src/Server/Manager.php @@ -230,12 +230,8 @@ public function onTask($server, $taskId, $srcWorkerId, $data) try { // push websocket message - if (is_array($data)) { - if ($this->isWebsocket - && array_key_exists('action', $data) - && $data['action'] === Websocket::PUSH_ACTION) { - $this->pushMessage($server, $data['data'] ?? []); - } + if ($this->isWebsocketPushPacket($data)) { + $this->pushMessage($server, $data['data'] ?? []); // push async task to queue } elseif (is_string($data)) { $decoded = json_decode($data, true); diff --git a/tests/Server/ManagerTest.php b/tests/Server/ManagerTest.php index d0b80fc7..2f81fcff 100644 --- a/tests/Server/ManagerTest.php +++ b/tests/Server/ManagerTest.php @@ -2,6 +2,7 @@ namespace SwooleTW\Http\Tests\Server; +use Exception; use Mockery as m; use Swoole\Table; use Swoole\Http\Request; @@ -574,6 +575,9 @@ protected function getContainer($server = null, $config = null) $container->singleton('swoole.server', function () use ($server) { return $server; }); + $container->singleton(ExceptionHandler::class, function () { + return new DummyExceptionHandler; + }); return $container; } @@ -623,3 +627,27 @@ protected function mockMethod($name, \Closure $function, $namespace = null) parent::mockMethod($name, $function, 'SwooleTW\Http\Server'); } } + +class DummyExceptionHandler implements ExceptionHandler { + public function report(Exception $e) + { + $this->dump($e); + } + + public function render($request, Exception $e) + { + $this->dump($e); + } + + public function renderForConsole($output, Exception $e) + { + $this->dump($e); + } + + protected function dump($e) + { + echo "\n Server Error: "; + echo $e->getMessage(); + die; + } +} From 1b420e271b088af4b4b7e905c701903d9ebacdab Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Fri, 7 Sep 2018 22:02:05 +0800 Subject: [PATCH 2/3] improve isSwooleQueuePacket function --- src/Concerns/InteractsWithSwooleQueue.php | 25 +++++++++++++++++++++++ src/Server/Manager.php | 10 ++++----- 2 files changed, 29 insertions(+), 6 deletions(-) create mode 100644 src/Concerns/InteractsWithSwooleQueue.php diff --git a/src/Concerns/InteractsWithSwooleQueue.php b/src/Concerns/InteractsWithSwooleQueue.php new file mode 100644 index 00000000..efed9c26 --- /dev/null +++ b/src/Concerns/InteractsWithSwooleQueue.php @@ -0,0 +1,25 @@ +isWebsocketPushPacket($data)) { $this->pushMessage($server, $data['data'] ?? []); // push async task to queue - } elseif (is_string($data)) { - $decoded = json_decode($data, true); - - if (JSON_ERROR_NONE === json_last_error() && isset($decoded['job'])) { - (new SwooleTaskJob($this->container, $server, $data, $taskId, $srcWorkerId))->fire(); - } + } elseif ($this->isSwooleQueuePacket($data)) { + (new SwooleTaskJob($this->container, $server, $data, $taskId, $srcWorkerId))->fire(); } } catch (Throwable $e) { $this->logServerError($e); From 78327372aa75da559bc4715d4226ea6c193fdaf6 Mon Sep 17 00:00:00 2001 From: Albert Chen Date: Mon, 14 Jan 2019 09:52:07 +0800 Subject: [PATCH 3/3] hot fix for laravel's bootstrap in v2.6.0 --- src/Concerns/WithApplication.php | 23 ++++++++++++++++++++++- tests/fixtures/bootstrap/app.php | 20 ++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Concerns/WithApplication.php b/src/Concerns/WithApplication.php index 63f0cdcd..a7e28906 100644 --- a/src/Concerns/WithApplication.php +++ b/src/Concerns/WithApplication.php @@ -28,7 +28,8 @@ trait WithApplication protected function bootstrap() { if ($this->framework === 'laravel') { - $this->app->bootstrap(); + $bootstrappers = $this->getBootstrappers(); + $this->app->bootstrapWith($bootstrappers); } else { // for Lumen 5.7 // https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837 @@ -137,4 +138,24 @@ protected function preResolveInstances() } } } + + /** + * Get bootstrappers. + * + * @return array + * @throws \ReflectionException + */ + protected function getBootstrappers() + { + $kernel = $this->getApplication()->make(Kernel::class); + + $reflection = new \ReflectionObject($kernel); + $bootstrappersMethod = $reflection->getMethod('bootstrappers'); + $bootstrappersMethod->setAccessible(true); + $bootstrappers = $bootstrappersMethod->invoke($kernel); + + array_splice($bootstrappers, -2, 0, ['Illuminate\Foundation\Bootstrap\SetRequestForConsole']); + + return $bootstrappers; + } } diff --git a/tests/fixtures/bootstrap/app.php b/tests/fixtures/bootstrap/app.php index aafbe347..79e6824b 100644 --- a/tests/fixtures/bootstrap/app.php +++ b/tests/fixtures/bootstrap/app.php @@ -1,11 +1,19 @@ shouldReceive('bootstrap') - ->once(); + +$app->shouldReceive('make') + ->with(Kernel::class) + ->once() + ->andReturn($kernel); +$app->shouldReceive('bootstrapWith') + ->once() + ->andReturn($kernel); $app->shouldReceive('offsetExists') ->with('foo') ->once() @@ -17,3 +25,11 @@ $app->shouldReceive('alias'); return $app; + +class TestKernel +{ + public function bootstrappers() + { + return []; + } +}