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

Commit b1d62be

Browse files
authored
Merge pull request #43 from swooletw/develop
Develop
2 parents dc0cfe0 + 0d93149 commit b1d62be

File tree

7 files changed

+81
-205
lines changed

7 files changed

+81
-205
lines changed

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2017 Huang Yi
3+
Copyright (c) 2018 Huang Yi, Albert Chen
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

config/swoole_http.php

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -47,38 +47,13 @@
4747
'enabled' => env('SWOOLE_HTTP_WEBSOCKET', false),
4848
],
4949

50-
/*
51-
|--------------------------------------------------------------------------
52-
| Laravel app will be cloned on every request.
53-
|--------------------------------------------------------------------------
54-
*/
55-
'sandbox_mode' => env('SWOOLE_SANDBOX_MODE', true),
56-
5750
/*
5851
|--------------------------------------------------------------------------
5952
| Console output will be transferred to response content if enabled.
6053
|--------------------------------------------------------------------------
6154
*/
6255
'ob_output' => env('SWOOLE_OB_OUTPUT', true),
6356

64-
/*
65-
|--------------------------------------------------------------------------
66-
| Providers here will be registered on every request.
67-
|--------------------------------------------------------------------------
68-
*/
69-
'providers' => [
70-
// Illuminate\Auth\AuthServiceProvider::class,
71-
],
72-
73-
/*
74-
|--------------------------------------------------------------------------
75-
| Resolved facades here will be cleared on every request.
76-
|--------------------------------------------------------------------------
77-
*/
78-
'facades' => [
79-
'auth', 'auth.driver', 'auth.password', 'request'
80-
],
81-
8257
/*
8358
|--------------------------------------------------------------------------
8459
| Instances here will be cleared on every request.
@@ -92,7 +67,7 @@
9267
|--------------------------------------------------------------------------
9368
| Define your swoole tables here.
9469
|
95-
| @see https://wiki.swoole.com/wiki/page/p-table.html
70+
| @see https://www.swoole.co.uk/docs/modules/swoole-table
9671
|--------------------------------------------------------------------------
9772
*/
9873
'tables' => [

src/Server/Application.php

Lines changed: 9 additions & 120 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,6 @@ class Application
4141
*/
4242
protected $kernel;
4343

44-
/**
45-
* Service providers to be reset.
46-
*
47-
* @var array
48-
*/
49-
protected $providers = [];
50-
51-
/**
52-
* Instance names to be reset.
53-
*
54-
* @var array
55-
*/
56-
protected $instances = [];
57-
58-
/**
59-
* Resolved facades to be reset.
60-
*
61-
* @var array
62-
*/
63-
protected $facades = [];
64-
6544
/**
6645
* Aliases for pre-resolving.
6746
*
@@ -95,11 +74,7 @@ public function __construct($framework, $basePath = null)
9574
{
9675
$this->setFramework($framework);
9776
$this->setBasePath($basePath);
98-
9977
$this->bootstrap();
100-
$this->initProviders();
101-
$this->initFacades();
102-
$this->initInstances();
10378
}
10479

10580
/**
@@ -119,84 +94,6 @@ protected function bootstrap()
11994
$this->preResolveInstances($application);
12095
}
12196

122-
/**
123-
* Initialize customized service providers.
124-
*/
125-
protected function initProviders()
126-
{
127-
$app = $this->getApplication();
128-
$providers = $app['config']->get('swoole_http.providers', []);
129-
130-
foreach ($providers as $provider) {
131-
if (! $provider instanceof ServiceProvider) {
132-
$provider = new $provider($app);
133-
}
134-
$this->providers[get_class($provider)] = $provider;
135-
}
136-
}
137-
138-
/**
139-
* Initialize customized instances.
140-
*/
141-
protected function initInstances()
142-
{
143-
$app = $this->getApplication();
144-
$instances = $app['config']->get('swoole_http.instances', []);
145-
146-
$this->instances = array_filter($instances, function ($value) {
147-
return is_string($value);
148-
});
149-
}
150-
151-
/**
152-
* Initialize customized facades.
153-
*/
154-
protected function initFacades()
155-
{
156-
$app = $this->getApplication();
157-
$facades = $app['config']->get('swoole_http.facades', []);
158-
159-
$this->facades = array_filter($facades, function ($value) {
160-
return is_string($value);
161-
});
162-
}
163-
164-
/**
165-
* Re-register and reboot service providers.
166-
*/
167-
public function resetProviders()
168-
{
169-
foreach ($this->providers as $provider) {
170-
if (method_exists($provider, 'register')) {
171-
$provider->register();
172-
}
173-
174-
if (method_exists($provider, 'boot')) {
175-
$this->getApplication()->call([$provider, 'boot']);
176-
}
177-
}
178-
}
179-
180-
/**
181-
* Clear resolved facades.
182-
*/
183-
public function clearFacades()
184-
{
185-
foreach ($this->facades as $facade) {
186-
Facade::clearResolvedInstance($facade);
187-
}
188-
}
189-
190-
/**
191-
* Clear resolved instances.
192-
*/
193-
public function clearInstances()
194-
{
195-
foreach ($this->instances as $instance) {
196-
$this->getApplication()->forgetInstance($instance);
197-
}
198-
}
199-
20097
/**
20198
* Load application.
20299
*
@@ -260,9 +157,11 @@ public function run(Request $request)
260157
// prepare content for ob
261158
$content = '';
262159
if ($shouldUseOb) {
263-
if ($response instanceof StreamedResponse ||
264-
$response instanceof BinaryFileResponse) {
160+
if ($response instanceof BinaryFileResponse) {
265161
$shouldUseOb = false;
162+
ob_end_clean();
163+
} elseif ($isStream = $response instanceof StreamedResponse) {
164+
$response->sendContent();
266165
} elseif ($response instanceof SymfonyResponse) {
267166
$content = $response->getContent();
268167
} else {
@@ -275,7 +174,11 @@ public function run(Request $request)
275174

276175
// set ob content to response
277176
if ($shouldUseOb && strlen($content) === 0 && ob_get_length() > 0) {
278-
$response->setContent(ob_get_contents());
177+
if ($isStream) {
178+
$response->output = ob_get_contents();
179+
} else {
180+
$response->setContent(ob_get_contents());
181+
}
279182
}
280183

281184
if ($shouldUseOb) {
@@ -375,20 +278,6 @@ public function terminate(Request $request, $response)
375278
protected function terminateLaravel(Request $request, $response)
376279
{
377280
$this->getKernel()->terminate($request, $response);
378-
379-
// clean laravel session
380-
if ($request->hasSession()) {
381-
$session = $request->getSession();
382-
$session->flush();
383-
}
384-
385-
// clean laravel cookie queue
386-
if (isset($this->application['cookie'])) {
387-
$cookies = $this->application['cookie'];
388-
foreach ($cookies->getQueuedCookies() as $name => $cookie) {
389-
$cookies->unqueue($name);
390-
}
391-
}
392281
}
393282

394283
/**

src/Server/Manager.php

Lines changed: 7 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ class Manager
5454
*/
5555
protected $basePath;
5656

57-
/**
58-
* @var boolean
59-
*/
60-
protected $isSandbox;
61-
6257
/**
6358
* @var \SwooleTW\Http\Server\Sandbox
6459
*/
@@ -114,7 +109,6 @@ protected function initialize()
114109
{
115110
$this->setProcessName('manager process');
116111

117-
$this->setIsSandbox();
118112
$this->createTables();
119113
$this->prepareWebsocket();
120114
$this->createSwooleServer();
@@ -217,9 +211,7 @@ public function onWorkerStart(HttpServer $server)
217211
$this->bindToLaravelApp();
218212

219213
// set application to sandbox environment
220-
if ($this->isSandbox || $this->isWebsocket) {
221-
$this->sandbox = Sandbox::make($this->getApplication());
222-
}
214+
$this->sandbox = Sandbox::make($this->getApplication());
223215

224216
// load websocket handlers after binding websocket to laravel app
225217
if ($this->isWebsocket) {
@@ -251,29 +243,23 @@ public function onRequest($swooleRequest, $swooleResponse)
251243
return;
252244
}
253245

254-
// get laravel/lumen's application instance
255-
$application = $this->getApplication();
246+
// set currnt request to sandbox
247+
$this->sandbox->setRequest($illuminateRequest);
256248

257-
// use cloned application if sandbox mode is on
258-
if ($this->isSandbox) {
259-
$application->getApplication()->instance('request', $illuminateRequest);
260-
$application = $this->sandbox->getApplication();
261-
$this->sandbox->enable();
262-
}
249+
// enable sandbox
250+
$application = $this->sandbox->getApplication();
251+
$this->sandbox->enable();
263252

264253
// bind illuminate request to laravel/lumen
265254
$application->getApplication()->instance('request', $illuminateRequest);
266-
Facade::clearResolvedInstance('request');
267255

268256
// handle request via laravel/lumen's dispatcher
269257
$illuminateResponse = $application->run($illuminateRequest);
270258
$response = Response::make($illuminateResponse, $swooleResponse);
271259
$response->send();
272260

273261
// disable and recycle sandbox resource
274-
if ($this->isSandbox) {
275-
$this->sandbox->disable();
276-
}
262+
$this->sandbox->disable();
277263
} catch (Exception $e) {
278264
try {
279265
$exceptionResponse = $this->app[ExceptionHandler::class]->render($illuminateRequest, $e);
@@ -329,17 +315,6 @@ protected function resetOnRequest()
329315
if ($this->isWebsocket) {
330316
$this->websocket->reset(true);
331317
}
332-
333-
if ($this->isSandbox) {
334-
return;
335-
}
336-
337-
// Reset user-customized providers
338-
$this->getApplication()->resetProviders();
339-
// Clear user-customized facades
340-
$this->getApplication()->clearFacades();
341-
// Clear user-customized instances
342-
$this->getApplication()->clearInstances();
343318
}
344319

345320
/**

src/Server/Response.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,10 @@ protected function sendContent()
103103
{
104104
$illuminateResponse = $this->getIlluminateResponse();
105105

106-
if ($illuminateResponse instanceof StreamedResponse) {
107-
$illuminateResponse->sendContent();
106+
if ($illuminateResponse instanceof StreamedResponse &&
107+
property_exists($illuminateResponse, 'output')
108+
) {
109+
$this->swooleResponse->end($illuminateResponse->output);
108110
} elseif ($illuminateResponse instanceof BinaryFileResponse) {
109111
$this->swooleResponse->sendfile($illuminateResponse->getFile()->getPathname());
110112
} else {

0 commit comments

Comments
 (0)