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

Commit 456c366

Browse files
authored
Merge pull request #50 from swooletw/develop
Develop
2 parents 0a56086 + e43e962 commit 456c366

File tree

6 files changed

+95
-17
lines changed

6 files changed

+95
-17
lines changed

config/swoole_http.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@
2222
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
2323
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', false),
2424
// Normally this value should be 1~4 times larger according to your cpu cores.
25-
'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num() * 2),
26-
'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num() * 2),
27-
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num() * 2),
25+
'reactor_num' => env('SWOOLE_HTTP_REACTOR_NUM', swoole_cpu_num()),
26+
'worker_num' => env('SWOOLE_HTTP_WORKER_NUM', swoole_cpu_num()),
27+
'task_worker_num' => env('SWOOLE_HTTP_TASK_WORKER_NUM', swoole_cpu_num()),
2828
// The data to receive can't be larger than buffer_output_size.
2929
'package_max_length' => 20 * 1024 * 1024,
3030
// The data to send can't be larger than buffer_output_size.
@@ -35,6 +35,9 @@
3535
'max_request' => 3000,
3636
// Enable coroutine send
3737
'send_yield' => true,
38+
// You must --enable-openssl while compiling Swoole
39+
'ssl_cert_file' => null,
40+
'ssl_key_file' => null,
3841
],
3942
],
4043

@@ -63,6 +66,15 @@
6366
//
6467
],
6568

69+
/*
70+
|--------------------------------------------------------------------------
71+
| Providers here will be registered on every request.
72+
|--------------------------------------------------------------------------
73+
*/
74+
'providers' => [
75+
Illuminate\Pagination\PaginationServiceProvider::class,
76+
],
77+
6678
/*
6779
|--------------------------------------------------------------------------
6880
| Define your swoole tables here.

src/Server/Application.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Illuminate\Container\Container;
77
use Illuminate\Contracts\Http\Kernel;
88
use Illuminate\Support\Facades\Facade;
9-
use Illuminate\Support\ServiceProvider;
109
use Symfony\Component\HttpFoundation\StreamedResponse;
1110
use Symfony\Component\HttpFoundation\BinaryFileResponse;
1211
use Symfony\Component\HttpFoundation\Response as SymfonyResponse;

src/Server/Manager.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,8 +140,11 @@ protected function createSwooleServer()
140140
$server = $this->isWebsocket ? WebsocketServer::class : HttpServer::class;
141141
$host = $this->container['config']->get('swoole_http.server.host');
142142
$port = $this->container['config']->get('swoole_http.server.port');
143+
$hasCert = $this->container['config']->get('swoole_http.server.options.ssl_cert_file');
144+
$hasKey = $this->container['config']->get('swoole_http.server.options.ssl_key_file');
145+
$args = $hasCert && $hasKey ? [SWOOLE_PROCESS, SWOOLE_SOCK_TCP | SWOOLE_SSL] : [];
143146

144-
$this->server = new $server($host, $port);
147+
$this->server = new $server($host, $port, ...$args);
145148
}
146149

147150
/**
@@ -250,9 +253,6 @@ public function onRequest($swooleRequest, $swooleResponse)
250253
$application = $this->sandbox->getApplication();
251254
$this->sandbox->enable();
252255

253-
// bind illuminate request to laravel/lumen
254-
$application->getApplication()->instance('request', $illuminateRequest);
255-
256256
// handle request via laravel/lumen's dispatcher
257257
$illuminateResponse = $application->run($illuminateRequest);
258258
$response = Response::make($illuminateResponse, $swooleResponse);

src/Server/Sandbox.php

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Container\Container;
77
use SwooleTW\Http\Server\Application;
88
use Illuminate\Support\Facades\Facade;
9+
use Illuminate\Support\ServiceProvider;
910
use Laravel\Lumen\Application as LumenApplication;
1011

1112
class Sandbox
@@ -30,6 +31,11 @@ class Sandbox
3031
*/
3132
protected $request;
3233

34+
/**
35+
* @var array
36+
*/
37+
protected $providers = [];
38+
3339
/**
3440
* @var boolean
3541
*/
@@ -52,7 +58,8 @@ public static function make(Application $application)
5258
public function __construct(Application $application)
5359
{
5460
$this->setApplication($application);
55-
$this->setInitialConfig($application);
61+
$this->setInitialConfig();
62+
$this->setInitialProviders();
5663
}
5764

5865
/**
@@ -77,12 +84,26 @@ public function setRequest(Request $request)
7784

7885
/**
7986
* Set config snapshot.
80-
*
81-
* @param \SwooleTW\Http\Server\Application
8287
*/
83-
protected function setInitialConfig(Application $application)
88+
protected function setInitialConfig()
89+
{
90+
$this->config = clone $this->application->getApplication()->make('config');
91+
}
92+
93+
/**
94+
* Initialize customized service providers.
95+
*/
96+
protected function setInitialProviders()
8497
{
85-
$this->config = clone $application->getApplication()['config'];
98+
$application = $this->application->getApplication();
99+
$providers = $this->config->get('swoole_http.providers', []);
100+
101+
foreach ($providers as $provider) {
102+
if (class_exists($provider)) {
103+
$provider = new $provider($application);
104+
$this->providers[get_class($provider)] = $provider;
105+
}
106+
}
86107
}
87108

88109
/**
@@ -111,8 +132,10 @@ protected function resetLaravelApp($application)
111132
$this->resetSession($application);
112133
$this->resetCookie($application);
113134
$this->clearInstances($application);
135+
$this->bindRequest($application);
114136
$this->rebindRouterContainer($application);
115137
$this->rebindViewContainer($application);
138+
$this->resetProviders($application);
116139
}
117140

118141
/**
@@ -126,6 +149,45 @@ protected function clearInstances($application)
126149
}
127150
}
128151

152+
/**
153+
* Bind illuminate request to laravel/lumen application.
154+
*/
155+
protected function bindRequest($application)
156+
{
157+
if ($this->request instanceof Request) {
158+
$application->instance('request', $this->request);
159+
}
160+
}
161+
162+
/**
163+
* Re-register and reboot service providers.
164+
*/
165+
protected function resetProviders($application)
166+
{
167+
foreach ($this->providers as $provider) {
168+
$this->rebindProviderContainer($provider, $application);
169+
if (method_exists($provider, 'register')) {
170+
$provider->register();
171+
}
172+
if (method_exists($provider, 'boot')) {
173+
$application->call([$provider, 'boot']);
174+
}
175+
}
176+
}
177+
178+
/**
179+
* Rebind service provider's container.
180+
*/
181+
protected function rebindProviderContainer($provider, $application)
182+
{
183+
$closure = function () use ($application) {
184+
$this->app = $application;
185+
};
186+
187+
$resetProvider = $closure->bindTo($provider, $provider);
188+
$resetProvider();
189+
}
190+
129191
/**
130192
* Reset laravel/lumen's config to initial values.
131193
*/

src/Websocket/CanWebsocket.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,11 +285,11 @@ protected function normalizePushData(array $data)
285285
*/
286286
protected function callOnConnect($illuminateRequest)
287287
{
288-
$application = $this->sandbox->getLaravelApp();
288+
// set currnt request to sandbox
289+
$this->sandbox->setRequest($illuminateRequest);
289290

290-
// bind illuminate request to laravel/lumen
291-
$application->instance('request', $illuminateRequest);
292-
Facade::clearResolvedInstance('request');
291+
// get application from sandbox
292+
$application = $this->sandbox->getLaravelApp();
293293

294294
// reset session
295295
if (isset($application['session'])) {

tests/Server/SandboxTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,14 @@ protected function getSandbox()
5353

5454
protected function getContainer()
5555
{
56+
$config = m::mock(Illuminate\Config\Repository::class);
57+
$config->shouldReceive('get')
58+
->andReturn([]);
5659
$container = m::mock(Container::class);
5760
$container->shouldReceive('offsetGet')
5861
->andReturn((object)[]);
62+
$container->shouldReceive('make')
63+
->andReturn($config);
5964

6065
return $container;
6166
}

0 commit comments

Comments
 (0)