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

Commit feff91b

Browse files
authored
Merge pull request #193 from swooletw/develop
Develop
2 parents 92d93f9 + 210d9a2 commit feff91b

File tree

5 files changed

+84
-3
lines changed

5 files changed

+84
-3
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace SwooleTW\Http\Concerns;
4+
5+
use Swoole\Table;
6+
use SwooleTW\Http\Table\SwooleTable;
7+
8+
trait InteractsWithSwooleQueue
9+
{
10+
/**
11+
* Indicates if a packet is swoole's queue job.
12+
*
13+
* @param mixed
14+
*/
15+
protected function isSwooleQueuePacket($packet)
16+
{
17+
if (! is_string($packet)) {
18+
return false;
19+
}
20+
21+
$decoded = json_decode($packet, true);
22+
23+
return JSON_ERROR_NONE === json_last_error() && isset($decoded['job']);
24+
}
25+
}

src/Concerns/InteractsWithWebsocket.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,23 @@ public function onClose($server, $fd, $reactorId)
151151
}
152152
}
153153

154+
/**
155+
* Indicates if a packet is websocket push action.
156+
*
157+
* @param mixed
158+
*/
159+
protected function isWebsocketPushPacket($packet)
160+
{
161+
if ( !is_array($packet)) {
162+
return false;
163+
}
164+
165+
return $this->isWebsocket
166+
&& array_key_exists('action', $packet)
167+
&& $packet['action'] === Websocket::PUSH_ACTION;
168+
}
169+
170+
154171
/**
155172
* Push websocket message to clients.
156173
*

src/Concerns/WithApplication.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ trait WithApplication
2828
protected function bootstrap()
2929
{
3030
if ($this->framework === 'laravel') {
31-
$this->app->bootstrap();
31+
$bootstrappers = $this->getBootstrappers();
32+
$this->app->bootstrapWith($bootstrappers);
3233
} else {
3334
// for Lumen 5.7
3435
// https://github.com/laravel/lumen-framework/commit/42cbc998375718b1a8a11883e033617024e57260#diff-c9248b3167fc44af085b81db2e292837
@@ -137,4 +138,24 @@ protected function preResolveInstances()
137138
}
138139
}
139140
}
141+
142+
/**
143+
* Get bootstrappers.
144+
*
145+
* @return array
146+
* @throws \ReflectionException
147+
*/
148+
protected function getBootstrappers()
149+
{
150+
$kernel = $this->getApplication()->make(Kernel::class);
151+
152+
$reflection = new \ReflectionObject($kernel);
153+
$bootstrappersMethod = $reflection->getMethod('bootstrappers');
154+
$bootstrappersMethod->setAccessible(true);
155+
$bootstrappers = $bootstrappersMethod->invoke($kernel);
156+
157+
array_splice($bootstrappers, -2, 0, ['Illuminate\Foundation\Bootstrap\SetRequestForConsole']);
158+
159+
return $bootstrappers;
160+
}
140161
}

src/Server/Manager.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Illuminate\Contracts\Container\Container;
1919
use Illuminate\Contracts\Debug\ExceptionHandler;
2020
use SwooleTW\Http\Concerns\InteractsWithWebsocket;
21+
use SwooleTW\Http\Concerns\InteractsWithSwooleQueue;
2122
use SwooleTW\Http\Concerns\InteractsWithSwooleTable;
2223
use Symfony\Component\Debug\Exception\FatalThrowableError;
2324

@@ -28,6 +29,7 @@ class Manager
2829
{
2930
use InteractsWithWebsocket,
3031
InteractsWithSwooleTable,
32+
InteractsWithSwooleQueue,
3133
WithApplication;
3234

3335
/**

tests/fixtures/bootstrap/app.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
11
<?php
22

33
use Mockery as m;
4+
use Illuminate\Contracts\Http\Kernel;
45
use Illuminate\Contracts\Container\Container;
56

7+
$kernel = new TestKernel;
68
$app = m::mock(Container::class);
7-
$app->shouldReceive('bootstrap')
8-
->once();
9+
10+
$app->shouldReceive('make')
11+
->with(Kernel::class)
12+
->once()
13+
->andReturn($kernel);
14+
$app->shouldReceive('bootstrapWith')
15+
->once()
16+
->andReturn($kernel);
917
$app->shouldReceive('offsetExists')
1018
->with('foo')
1119
->once()
@@ -17,3 +25,11 @@
1725
$app->shouldReceive('alias');
1826

1927
return $app;
28+
29+
class TestKernel
30+
{
31+
public function bootstrappers()
32+
{
33+
return [];
34+
}
35+
}

0 commit comments

Comments
 (0)