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

Commit b5e26d3

Browse files
committed
add support for customizing providers to be reset in every request.
1 parent afb2a60 commit b5e26d3

File tree

10 files changed

+147
-18
lines changed

10 files changed

+147
-18
lines changed

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: 41 additions & 0 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;
@@ -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
*

src/Server/Manager.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ public function onRequest($swooleRequest, $swooleResponse)
175175
{
176176
$this->container['events']->fire('http.onRequest');
177177

178+
// Reset user-customized providers
179+
$this->getApplication()->resetProviders();
180+
178181
$illuminateRequest = Request::make($swooleRequest)->toIlluminate();
179182
$illuminateResponse = $this->getApplication()->run($illuminateRequest);
180183

tests/Server/ApplicationTest.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,46 @@ public function testTerminate()
6060

6161
$this->assertTrue($flag);
6262
}
63+
64+
public function testLaravelResetProvider()
65+
{
66+
$application = Application::make('laravel', $this->basePath . '/laravel');
67+
$response = $application->run(Request::create('/'));
68+
69+
$app = $application->getApplication();
70+
71+
$this->assertSame('bar', $app['singleton.test']->foo);
72+
73+
$app->singleton('singleton.test', function () {
74+
$obj = new \stdClass;
75+
$obj->foo = 'foo';
76+
77+
return $obj;
78+
});
79+
$this->assertSame('foo', $app['singleton.test']->foo);
80+
81+
$response = $application->resetProviders();
82+
$this->assertSame('bar', $app['singleton.test']->foo);
83+
}
84+
85+
public function testLumenResetProvider()
86+
{
87+
$application = Application::make('lumen', $this->basePath . '/lumen');
88+
$response = $application->run(Request::create('/'));
89+
90+
$app = $application->getApplication();
91+
92+
$this->assertSame('bar', $app['singleton.test']->foo);
93+
94+
$app->singleton('singleton.test', function () {
95+
$obj = new \stdClass;
96+
$obj->foo = 'foo';
97+
98+
return $obj;
99+
});
100+
$this->assertSame('foo', $app['singleton.test']->foo);
101+
102+
$response = $application->resetProviders();
103+
$this->assertSame('bar', $app['singleton.test']->foo);
104+
}
63105
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace SwooleTW\Http\Tests\Fixtures\Laravel\App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class TestServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register the service provider.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
$this->app->singleton('singleton.test', function () {
17+
$obj = new \stdClass;
18+
$obj->foo = 'bar';
19+
20+
return $obj;
21+
});
22+
}
23+
}

tests/fixtures/laravel/config/app.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151

5252
SwooleTW\Http\LaravelServiceProvider::class,
5353
SwooleTW\Http\Tests\Fixtures\Laravel\App\Providers\RouteServiceProvider::class,
54+
SwooleTW\Http\Tests\Fixtures\Laravel\App\Providers\TestServiceProvider::class,
5455

5556
],
5657

tests/fixtures/lumen/config/http.php renamed to tests/fixtures/laravel/config/swoole_http.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212
*/
1313

1414
'server' => [
15-
1615
'host' => '127.0.0.1',
17-
1816
'port' => '1215',
19-
2017
'options' => [
21-
22-
'pid_file' => base_path('storage/logs/http.pid'),
23-
18+
'pid_file' => base_path('storage/logs/swoole_http.pid'),
2419
'daemonize' => 0,
25-
2620
],
27-
2821
],
29-
22+
'providers' => [
23+
SwooleTW\Http\Tests\Fixtures\Laravel\App\Providers\TestServiceProvider::class,
24+
]
3025
];
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace SwooleTW\Http\Tests\Fixtures\Lumen\App\Providers;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
7+
class TestServiceProvider extends ServiceProvider
8+
{
9+
/**
10+
* Register the service provider.
11+
*
12+
* @return void
13+
*/
14+
public function register()
15+
{
16+
$this->app->singleton('singleton.test', function () {
17+
$obj = new \stdClass;
18+
$obj->foo = 'bar';
19+
20+
return $obj;
21+
});
22+
}
23+
}

tests/fixtures/lumen/bootstrap/app.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
);
1616

1717
$app->register(SwooleTW\Http\LumenServiceProvider::class);
18+
$app->register(SwooleTW\Http\Tests\Fixtures\Lumen\App\Providers\TestServiceProvider::class);
19+
20+
$app->configure('swoole_http');
1821

1922
$app->group(['namespace' => 'App\Http\Controllers'], function ($app) {
2023
require __DIR__ . '/../routes/web.php';

tests/fixtures/laravel/config/http.php renamed to tests/fixtures/lumen/config/swoole_http.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,14 @@
1212
*/
1313

1414
'server' => [
15-
1615
'host' => '127.0.0.1',
17-
1816
'port' => '1215',
19-
2017
'options' => [
21-
22-
'pid_file' => base_path('storage/logs/http.pid'),
23-
18+
'pid_file' => base_path('storage/logs/swoole_http.pid'),
2419
'daemonize' => 0,
25-
2620
],
27-
2821
],
29-
22+
'providers' => [
23+
SwooleTW\Http\Tests\Fixtures\Lumen\App\Providers\TestServiceProvider::class,
24+
]
3025
];

0 commit comments

Comments
 (0)