Skip to content
This repository was archived by the owner on Mar 24, 2025. It is now read-only.

Commit 768465b

Browse files
authored
Merge pull request #261 from swooletw/develop
Develop
2 parents 5ba982e + 8b74ab9 commit 768465b

File tree

10 files changed

+61
-59
lines changed

10 files changed

+61
-59
lines changed

.travis.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,22 @@ sudo: false
44

55
matrix:
66
include:
7-
- php: 7.2
8-
env: FRAMEWORK_VERSION=laravel/framework:5.4.*
97
- php: 7.2
108
env: FRAMEWORK_VERSION=laravel/framework:5.5.*
119
- php: 7.2
1210
env: FRAMEWORK_VERSION=laravel/framework:5.6.*
1311
- php: 7.2
1412
env: FRAMEWORK_VERSION=laravel/framework:5.7.*
15-
# - php: 7.2
16-
# env: FRAMEWORK_VERSION=laravel/framework:5.8.*
1713
- php: 7.2
18-
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.4.*
14+
env: FRAMEWORK_VERSION=laravel/framework:5.8.*
1915
- php: 7.2
2016
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.5.*
2117
- php: 7.2
2218
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.6.*
2319
- php: 7.2
2420
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.7.*
21+
# - php: 7.2
22+
# env: FRAMEWORK_VERSION=laravel/lumen-framework:5.8.*
2523

2624
before_install:
2725
- printf "\n" | pecl install swoole

routes/lumen_routes.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,5 @@
1111
|
1212
*/
1313

14-
$app->get('socket.io', [
15-
'as' => 'io.get', 'uses' => 'SocketIOController@upgrade',
16-
]);
17-
18-
$app->post('socket.io', [
19-
'as' => 'io.post', 'uses' => 'SocketIOController@reject',
20-
]);
14+
$router->get('socket.io', ['uses' => 'SocketIOController@upgrade']);
15+
$router->post('socket.io', ['uses' => 'SocketIOController@reject']);

src/Commands/HttpServerCommand.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
use Throwable;
66
use Swoole\Process;
77
use Illuminate\Support\Arr;
8+
use SwooleTW\Http\Helpers\OS;
89
use Illuminate\Console\Command;
910
use SwooleTW\Http\Server\Manager;
1011
use Illuminate\Console\OutputStyle;
1112
use SwooleTW\Http\HotReload\FSEvent;
12-
use SwooleTW\Http\Server\AccessOutput;
1313
use SwooleTW\Http\HotReload\FSOutput;
1414
use SwooleTW\Http\HotReload\FSProcess;
15+
use SwooleTW\Http\Server\AccessOutput;
1516
use SwooleTW\Http\Middleware\AccessLog;
1617
use SwooleTW\Http\Server\Facades\Server;
1718
use Illuminate\Contracts\Container\Container;
@@ -372,20 +373,20 @@ protected function isDaemon(): bool
372373
*/
373374
protected function checkEnvironment()
374375
{
375-
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
376-
$this->error("Swoole extension doesn't support Windows OS yet.");
376+
if (OS::is(OS::WIN)) {
377+
$this->error('Swoole extension doesn\'t support Windows OS.');
377378

378379
exit(1);
379380
}
380381

381382
if (! extension_loaded('swoole')) {
382-
$this->error("Can't detect Swoole extension installed.");
383+
$this->error('Can\'t detect Swoole extension installed.');
383384

384385
exit(1);
385386
}
386387

387-
if (! version_compare(swoole_version(), '4.0.0', 'ge')) {
388-
$this->error("Your Swoole version must be higher than 4.0 to use coroutine.");
388+
if (! version_compare(swoole_version(), '4.3.1', 'ge')) {
389+
$this->error('Your Swoole version must be higher than `4.3.1`.');
389390

390391
exit(1);
391392
}

src/Concerns/WithApplication.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,7 @@ protected function bootstrap()
3636
if (method_exists($this->app, 'boot')) {
3737
$this->app->boot();
3838
}
39-
if (is_null(Facade::getFacadeApplication())) {
40-
$this->app->withFacades();
41-
}
39+
$this->app->withFacades();
4240
}
4341

4442
$this->preResolveInstances();

src/Helpers/Dumper.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace SwooleTW\Http\Helpers;
4+
5+
use Symfony\Component\VarDumper\Cloner\VarCloner;
6+
use Symfony\Component\VarDumper\Dumper\CliDumper;
7+
use Symfony\Component\VarDumper\Dumper\HtmlDumper;
8+
9+
class VarDumper
10+
{
11+
protected static $cloner;
12+
13+
public static function dump(...$args)
14+
{
15+
if (! static::$cloner instanceOf VarCloner) {
16+
static::$cloner = new VarCloner;
17+
}
18+
19+
$dumper = static::getDumper();
20+
21+
foreach ($args as $arg) {
22+
$dumper->dump(
23+
static::$cloner->cloneVar($arg)
24+
);
25+
}
26+
27+
return true;
28+
}
29+
30+
public static function getDumper()
31+
{
32+
$dumper = defined('IN_PHPUNIT') || ! config('swoole_http.ob_output')
33+
? CliDumper::class
34+
: HtmlDumper::class;
35+
36+
return new $dumper;
37+
}
38+
}

src/Helpers/FW.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ final class FW
4747
*/
4848
public static function is(string ...$types): bool
4949
{
50-
return \in_array(static::current(), $types, true);
50+
return in_array(static::current(), $types, true);
5151
}
5252

5353
/**

src/LumenServiceProvider.php

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,10 @@ protected function registerManager()
3131
*/
3232
protected function bootWebsocketRoutes()
3333
{
34-
$app = $this->app;
35-
36-
// router only exists after lumen 5.5
37-
if (property_exists($app, 'router')) {
38-
$app->router->group(['namespace' => 'SwooleTW\Http\Controllers'], function ($app) {
39-
require __DIR__ . '/../routes/lumen_routes.php';
40-
});
41-
} else {
42-
$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
34+
$this->app->router
35+
->group(['namespace' => 'SwooleTW\Http\Controllers'], function ($router) {
4336
require __DIR__ . '/../routes/lumen_routes.php';
4437
});
45-
}
4638
}
4739

4840
/**

src/Websocket/Pusher.php

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -212,18 +212,6 @@ public function shouldBroadcast(): bool
212212
return $this->broadcast && empty($this->descriptors) && ! $this->assigned;
213213
}
214214

215-
/**
216-
* Check if it's a websocket fd.
217-
*
218-
* @param int $fd
219-
*
220-
* @return bool
221-
*/
222-
public function isServerWebsocket(int $fd): bool
223-
{
224-
return (bool) $this->server->connection_info($fd)['websocket_status'] ?? false;
225-
}
226-
227215
/**
228216
* Returns all descriptors that are websocket
229217
*
@@ -234,7 +222,7 @@ public function isServerWebsocket(int $fd): bool
234222
protected function getWebsocketConnections(): array
235223
{
236224
return array_filter(iterator_to_array($this->server->connections), function ($fd) {
237-
return $this->isServerWebsocket($fd);
225+
return $this->server->isEstablished($fd);
238226
});
239227
}
240228

@@ -245,7 +233,7 @@ protected function getWebsocketConnections(): array
245233
*/
246234
public function shouldPushToDescriptor(int $fd): bool
247235
{
248-
if (! $this->server->exist($fd)) {
236+
if (! $this->server->isEstablished($fd)) {
249237
return false;
250238
}
251239

tests/Server/ManagerTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,7 @@ public function testPushMessage()
565565
->andReturn(false);
566566

567567
$server = m::mock('server');
568-
$server->shouldReceive('exist')
568+
$server->shouldReceive('isEstablished')
569569
->andReturn(true);
570570
$server->shouldReceive('push')
571571
->twice();

tests/Websocket/PusherTest.php

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -89,31 +89,23 @@ public function testHasDescriptor()
8989

9090
public function testShouldBroadcast()
9191
{
92-
$server = m::mock(Server::class);
93-
$server->shouldReceive('connection_info')
94-
->with($fd = 1)
95-
->once()
96-
->andReturn([
97-
'websocket_status' => true
98-
]);
99-
10092
$pusher = Pusher::make([
10193
'opcode' => 1,
102-
'sender' => 3,
94+
'sender' => 1,
10395
'fds' => [],
10496
'broadcast' => true,
10597
'assigned' => false,
10698
'event' => 'event',
10799
'message' => 'message'
108-
], $server);
100+
], null);
109101

110-
$this->assertTrue($pusher->isServerWebsocket($fd));
102+
$this->assertTrue($pusher->shouldBroadcast());
111103
}
112104

113105
public function testShouldPushToDescriptor()
114106
{
115107
$server = m::mock(Server::class);
116-
$server->shouldReceive('exist')
108+
$server->shouldReceive('isEstablished')
117109
->with($fd = 1)
118110
->times(3)
119111
->andReturn(true);

0 commit comments

Comments
 (0)