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

Commit 2898ae9

Browse files
authored
Merge pull request #39 from swooletw/develop
Develop
2 parents d150b8e + 1cef393 commit 2898ae9

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

.github/ISSUE_TEMPLATE.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,18 @@ Please answer these questions before submitting your issue. Thanks!
88

99

1010

11-
3. What did you do? If possible, provide a recipe for reproducing the error.
11+
3. Which release version of this package are you using?
1212

1313

1414

15-
4. What did you expect to see?
15+
4. What did you do? If possible, provide a recipe for reproducing the error.
1616

1717

1818

19-
5. What did you see instead?
19+
5. What did you expect to see?
20+
21+
22+
23+
6. What did you see instead?
2024

2125

config/swoole_http.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
'host' => env('SWOOLE_HTTP_HOST', '127.0.0.1'),
1616
'port' => env('SWOOLE_HTTP_PORT', '1215'),
1717
'public_path' => base_path('public'),
18+
// If use swoole to respond request for static files
19+
'handle_static_files' => true,
1820
'options' => [
1921
'pid_file' => env('SWOOLE_HTTP_PID_FILE', base_path('storage/logs/swoole_http.pid')),
2022
'log_file' => env('SWOOLE_HTTP_LOG_FILE', base_path('storage/logs/swoole_http.log')),
@@ -31,6 +33,8 @@
3133
'socket_buffer_size' => 128 * 1024 * 1024,
3234
// Worker will restart after processing this number of request
3335
'max_request' => 3000,
36+
// Enable coroutine send
37+
'send_yield' => true,
3438
],
3539
],
3640

src/Server/Manager.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,14 @@ public function onRequest($swooleRequest, $swooleResponse)
240240

241241
$this->resetOnRequest();
242242

243+
$handleStatic = $this->container['config']->get('swoole_http.handle_static_files', true);
244+
243245
try {
244246
// transform swoole request to illuminate request
245247
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();
246248

247249
// handle static file request first
248-
if ($this->handleStaticRequest($illuminateRequest, $swooleResponse)) {
250+
if ($handleStatic && $this->handleStaticRequest($illuminateRequest, $swooleResponse)) {
249251
return;
250252
}
251253

@@ -301,7 +303,7 @@ protected function handleStaticRequest($illuminateRequest, $swooleResponse)
301303
$publicPath = $this->container['config']->get('swoole_http.server.public_path', base_path('public'));
302304
$filename = $publicPath . $uri;
303305

304-
if (! is_file($filename)) {
306+
if (! is_file($filename) || filesize($filename) === 0) {
305307
return;
306308
}
307309

src/Server/Sandbox.php

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ class Sandbox
1919
*/
2020
protected $snapshot;
2121

22+
/**
23+
* @var \Illuminate\Config\Repository
24+
*/
25+
protected $config;
26+
2227
/**
2328
* @var boolean
2429
*/
@@ -41,6 +46,7 @@ public static function make(Application $application)
4146
public function __construct(Application $application)
4247
{
4348
$this->setApplication($application);
49+
$this->setInitialConfig($application);
4450
}
4551

4652
/**
@@ -53,6 +59,16 @@ public function setApplication(Application $application)
5359
$this->application = $application;
5460
}
5561

62+
/**
63+
* Set config snapshot.
64+
*
65+
* @param \SwooleTW\Http\Server\Application
66+
*/
67+
protected function setInitialConfig(Application $application)
68+
{
69+
$this->config = clone $application->getApplication()['config'];
70+
}
71+
5672
/**
5773
* Get an application snapshot
5874
*
@@ -75,20 +91,19 @@ public function getApplication()
7591
*/
7692
protected function resetLaravelApp($application)
7793
{
78-
if ($this->isFramework('laravel')) {
79-
$application->bootstrapWith([
80-
'Illuminate\Foundation\Bootstrap\LoadConfiguration'
81-
]);
82-
} elseif ($this->isFramework('lumen')) {
83-
$reflector = new \ReflectionMethod(LumenApplication::class, 'registerConfigBindings');
84-
$reflector->setAccessible(true);
85-
$reflector->invoke($application);
86-
}
87-
94+
$this->resetConfigInstance($application);
8895
$this->rebindRouterContainer($application);
8996
$this->rebindViewContainer($application);
9097
}
9198

99+
/**
100+
* Reset laravel/lumen's config to initial values.
101+
*/
102+
protected function resetConfigInstance($application)
103+
{
104+
$application->instance('config', clone $this->config);
105+
}
106+
92107
/**
93108
* Rebind laravel's container in router.
94109
*/
@@ -98,6 +113,14 @@ protected function rebindRouterContainer($application)
98113
$router = $application->make('router');
99114
$closure = function () use ($application) {
100115
$this->container = $application;
116+
if (! isset($application['request'])) {
117+
return;
118+
}
119+
$route = $this->routes->match($application['request']);
120+
// clear resolved controller
121+
if (property_exists($route, 'container')) {
122+
$route->controller = null;
123+
}
101124
};
102125

103126
$resetRouter = $closure->bindTo($router, $router);

tests/Server/SandboxTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public function testGetSandbox()
1919

2020
public function testMakeSandbox()
2121
{
22-
$application = m::mock(Application::class);
23-
$sandbox = Sandbox::make($application);
22+
$sandbox = Sandbox::make($this->getApplication());
2423

2524
$this->assertTrue($sandbox instanceof Sandbox);
2625
}
@@ -55,8 +54,8 @@ protected function getSandbox()
5554
protected function getContainer()
5655
{
5756
$container = m::mock(Container::class);
58-
$container->shouldReceive('bootstrapWith')
59-
->andReturnNull();
57+
$container->shouldReceive('offsetGet')
58+
->andReturn((object)[]);
6059

6160
return $container;
6261
}

0 commit comments

Comments
 (0)