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

Commit 03c9513

Browse files
authored
Merge pull request #4 from swooletw/develop
Develop
2 parents ac6e302 + ec2270c commit 03c9513

File tree

14 files changed

+279
-108
lines changed

14 files changed

+279
-108
lines changed

.travis.yml

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
language: php
2+
dist: trusty
3+
sudo: false
4+
5+
matrix:
6+
include:
7+
- php: 7.0
8+
env: FRAMEWORK_VERSION=laravel/framework:5.1.*
9+
- php: 7.0
10+
env: FRAMEWORK_VERSION=laravel/framework:5.2.*
11+
- php: 7.0
12+
env: FRAMEWORK_VERSION=laravel/framework:5.3.*
13+
- php: 7.0
14+
env: FRAMEWORK_VERSION=laravel/framework:5.4.*
15+
- php: 7.0
16+
env: FRAMEWORK_VERSION=laravel/framework:5.5.*
17+
- php: 7.0
18+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.1.*
19+
- php: 7.0
20+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.2.*
21+
- php: 7.0
22+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.3.*
23+
- php: 7.0
24+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.4.*
25+
- php: 7.0
26+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.5.*
27+
- php: 7.1
28+
env: FRAMEWORK_VERSION=laravel/framework:5.1.*
29+
- php: 7.1
30+
env: FRAMEWORK_VERSION=laravel/framework:5.2.*
31+
- php: 7.1
32+
env: FRAMEWORK_VERSION=laravel/framework:5.3.*
33+
- php: 7.1
34+
env: FRAMEWORK_VERSION=laravel/framework:5.4.*
35+
- php: 7.1
36+
env: FRAMEWORK_VERSION=laravel/framework:5.5.*
37+
- php: 7.1
38+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.1.*
39+
- php: 7.1
40+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.2.*
41+
- php: 7.1
42+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.3.*
43+
- php: 7.1
44+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.4.*
45+
- php: 7.1
46+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.5.*
47+
- php: 7.2
48+
env: FRAMEWORK_VERSION=laravel/framework:5.1.*
49+
- php: 7.2
50+
env: FRAMEWORK_VERSION=laravel/framework:5.2.*
51+
- php: 7.2
52+
env: FRAMEWORK_VERSION=laravel/framework:5.3.*
53+
- php: 7.2
54+
env: FRAMEWORK_VERSION=laravel/framework:5.4.*
55+
- php: 7.2
56+
env: FRAMEWORK_VERSION=laravel/framework:5.5.*
57+
- php: 7.2
58+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.1.*
59+
- php: 7.2
60+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.2.*
61+
- php: 7.2
62+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.3.*
63+
- php: 7.2
64+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.4.*
65+
- php: 7.2
66+
env: FRAMEWORK_VERSION=laravel/lumen-framework:5.5.*
67+
68+
before_install:
69+
- pecl install swoole
70+
71+
install:
72+
- composer require "${FRAMEWORK_VERSION}" --no-update -n
73+
- travis_retry composer install --no-suggest --prefer-dist -n -o
74+
75+
script: vendor/bin/phpunit

composer.json

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@
2020
"illuminate/support": "~5.1"
2121
},
2222
"require-dev": {
23-
"laravel/framework": "~5.1",
2423
"laravel/lumen-framework": "~5.1",
25-
"orchestra/testbench": "~3.1",
26-
"phpunit/phpunit": "~4.0|~5.0"
24+
"phpunit/phpunit": "~6.0"
2725
},
2826
"autoload": {
2927
"psr-4": {

config/swoole_http.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,7 @@
1818
'daemonize' => env('SWOOLE_HTTP_DAEMONIZE', 0),
1919
],
2020
],
21+
'providers' => [
22+
// App\Providers\AuthServiceProvider::class,
23+
]
2124
];

src/Server/Application.php

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SwooleTW\Http\Server;
44

5+
use Illuminate\Support\ServiceProvider;
56
use Illuminate\Container\Container;
67
use Illuminate\Contracts\Http\Kernel;
78
use Illuminate\Http\Request;
@@ -25,7 +26,7 @@ class Application
2526
/**
2627
* Laravel|Lumen Application.
2728
*
28-
* @var \Illuminate\Contracts\Foundation\Application
29+
* @var \Illuminate\Container\Container
2930
*/
3031
protected $application;
3132

@@ -34,6 +35,13 @@ class Application
3435
*/
3536
protected $kernel;
3637

38+
/**
39+
* Preserved service providers to be reset.
40+
*
41+
* @var array
42+
*/
43+
protected $providers = [];
44+
3745
/**
3846
* Make an application.
3947
*
@@ -58,6 +66,7 @@ public function __construct($framework, $basePath = null)
5866
$this->setBasePath($basePath);
5967

6068
$this->bootstrap();
69+
$this->initProviders();
6170
}
6271

6372
/**
@@ -71,6 +80,38 @@ protected function bootstrap()
7180
}
7281
}
7382

83+
/**
84+
* Initialize customized service providers.
85+
*/
86+
protected function initProviders()
87+
{
88+
$app = $this->getApplication();
89+
$providers = $app['config']->get('swoole_http.providers');
90+
91+
foreach ($providers as $provider) {
92+
if (! $provider instanceof ServiceProvider) {
93+
$provider = new $provider($app);
94+
}
95+
$this->providers[get_class($provider)] = $provider;
96+
}
97+
}
98+
99+
/**
100+
* Re-register and reboot service providers.
101+
*/
102+
public function resetProviders()
103+
{
104+
foreach ($this->providers as $key => $provider) {
105+
if (method_exists($provider, 'register')) {
106+
$provider->register();
107+
}
108+
109+
if (method_exists($provider, 'boot')) {
110+
$this->getApplication()->call([$provider, 'boot']);
111+
}
112+
}
113+
}
114+
74115
/**
75116
* Load application.
76117
*
@@ -82,11 +123,11 @@ protected function loadApplication()
82123
}
83124

84125
/**
85-
* @return \Illuminate\Contracts\Foundation\Application
126+
* @return \Illuminate\Container\Container
86127
*/
87128
public function getApplication()
88129
{
89-
if (!$this->application instanceof Container) {
130+
if (! $this->application instanceof Container) {
90131
$this->application = $this->loadApplication();
91132
}
92133

@@ -98,7 +139,7 @@ public function getApplication()
98139
*/
99140
public function getKernel()
100141
{
101-
if (!$this->kernel instanceof Kernel) {
142+
if (! $this->kernel instanceof Kernel) {
102143
$this->kernel = $this->getApplication()->make(Kernel::class);
103144
}
104145

@@ -175,7 +216,7 @@ protected function setFramework($framework)
175216
{
176217
$framework = strtolower($framework);
177218

178-
if (!in_array($framework, ['laravel', 'lumen'])) {
219+
if (! in_array($framework, ['laravel', 'lumen'])) {
179220
throw new \Exception(sprintf('Not support framework "%s".', $this->framework));
180221
}
181222

src/Server/Manager.php

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,13 @@ class Manager
2626
*/
2727
protected $application;
2828

29+
/**
30+
* Laravel|Lumen Application.
31+
*
32+
* @var \Illuminate\Container\Container
33+
*/
34+
protected $app;
35+
2936
/**
3037
* @var string
3138
*/
@@ -154,6 +161,8 @@ public function onWorkerStart()
154161
$this->container['events']->fire('http.workerStart', func_get_args());
155162

156163
$this->createApplication();
164+
$this->setLaravelApp();
165+
$this->bindSwooleHttpServer();
157166
}
158167

159168
/**
@@ -164,12 +173,17 @@ public function onWorkerStart()
164173
*/
165174
public function onRequest($swooleRequest, $swooleResponse)
166175
{
176+
$this->container['events']->fire('http.onRequest');
177+
178+
// Reset user-customized providers
179+
$this->getApplication()->resetProviders();
180+
167181
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();
168182
$illuminateResponse = $this->getApplication()->run($illuminateRequest);
169183

170184
$response = Response::make($illuminateResponse, $swooleResponse);
171185
// To prevent 'connection[...] is closed' error.
172-
if (!$this->server->exist($response->getSwooleResponse()->fd)) {
186+
if (! $this->server->exist($swooleRequest->fd)) {
173187
return;
174188
}
175189
$response->send();
@@ -214,6 +228,24 @@ protected function getApplication()
214228
return $this->application;
215229
}
216230

231+
/**
232+
* Set Laravel app.
233+
*/
234+
protected function setLaravelApp()
235+
{
236+
$this->app = $this->getApplication()->getApplication();
237+
}
238+
239+
/**
240+
* Bind swoole server to Laravel app container.
241+
*/
242+
protected function bindSwooleHttpServer()
243+
{
244+
$this->app->singleton('swoole.server', function () {
245+
return $this->server;
246+
});
247+
}
248+
217249
/**
218250
* Gets pid file path.
219251
*

tests/Server/ApplicationTest.php

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,38 @@ class ApplicationTest extends TestCase
1313

1414
public function testMake()
1515
{
16-
$application = Application::make('laravel', $this->basePath . '/laravel');
16+
$application = $this->makeApplication();
1717

1818
$this->assertInstanceOf(Application::class, $application);
1919
}
2020

21-
public function testRunLaravel()
21+
public function testMakeInvalidFramework()
2222
{
23-
$application = Application::make('laravel', $this->basePath . '/laravel');
24-
$response = $application->run(Request::create('/'));
23+
$this->expectException(\Exception::class);
2524

26-
$this->assertInstanceOf(Response::class, $response);
27-
$this->assertSame('welcome', $response->getContent());
25+
$this->makeApplication('other');
2826
}
2927

30-
public function testRunLumen()
28+
public function testRun()
3129
{
32-
$application = Application::make('lumen', $this->basePath . '/lumen');
30+
$application = $this->makeApplication();
3331
$response = $application->run(Request::create('/'));
3432

3533
$this->assertInstanceOf(Response::class, $response);
36-
$this->assertSame('hello', $response->getContent());
37-
}
38-
39-
public function testRunOther()
40-
{
41-
$this->expectException(\Exception::class);
42-
43-
$application = Application::make('other', $this->basePath . '/laravel');
44-
$application->run(Request::create('/'));
34+
$this->assertSame('welcome', $response->getContent());
4535
}
4636

4737
public function testTerminate()
4838
{
4939
$flag = false;
5040

51-
$application = Application::make('laravel', $this->basePath . '/laravel');
41+
if (class_exists('\Laravel\Lumen\Application')) {
42+
$this->assertTrue(true);
43+
44+
return;
45+
}
46+
47+
$application = $this->makeApplication();
5248
$request = Request::create('/');
5349
$response = $application->run($request);
5450

@@ -60,4 +56,40 @@ public function testTerminate()
6056

6157
$this->assertTrue($flag);
6258
}
59+
60+
public function testResetProvider()
61+
{
62+
$application = $this->makeApplication();
63+
$response = $application->run(Request::create('/'));
64+
65+
$app = $application->getApplication();
66+
67+
$this->assertSame('bar', $app['singleton.test']->foo);
68+
69+
$app->singleton('singleton.test', function () {
70+
$obj = new \stdClass;
71+
$obj->foo = 'foo';
72+
73+
return $obj;
74+
});
75+
$this->assertSame('foo', $app['singleton.test']->foo);
76+
77+
$response = $application->resetProviders();
78+
$this->assertSame('bar', $app['singleton.test']->foo);
79+
}
80+
81+
protected function makeApplication($forceFramework = null)
82+
{
83+
if (! is_null($forceFramework)) {
84+
$framework = $forceFramework;
85+
} elseif (class_exists('\Illuminate\Foundation\Application')) {
86+
$framework = 'laravel';
87+
} elseif (class_exists('\Laravel\Lumen\Application')) {
88+
$framework = 'lumen';
89+
} else {
90+
$framework = 'other';
91+
}
92+
93+
return Application::make($framework, $this->basePath . '/' . $framework);
94+
}
6395
}

tests/TestCase.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace SwooleTW\Http\Tests;
44

5-
use Orchestra\Testbench\TestCase as BaseTestCase;
5+
use PHPUnit\Framework\TestCase as BaseTestCase;
66

77
class TestCase extends BaseTestCase
88
{

0 commit comments

Comments
 (0)