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

Commit 3cfed44

Browse files
committed
bring reset service providers back
1 parent 260a4aa commit 3cfed44

File tree

5 files changed

+77
-13
lines changed

5 files changed

+77
-13
lines changed

config/swoole_http.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@
6666
//
6767
],
6868

69+
/*
70+
|--------------------------------------------------------------------------
71+
| Providers here will be registered on every request.
72+
|--------------------------------------------------------------------------
73+
*/
74+
'providers' => [
75+
Illuminate\Pagination\PaginationServiceProvider::class,
76+
],
77+
6978
/*
7079
|--------------------------------------------------------------------------
7180
| 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: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,6 @@ public function onRequest($swooleRequest, $swooleResponse)
253253
$application = $this->sandbox->getApplication();
254254
$this->sandbox->enable();
255255

256-
// bind illuminate request to laravel/lumen
257-
$application->getApplication()->instance('request', $illuminateRequest);
258-
259256
// handle request via laravel/lumen's dispatcher
260257
$illuminateResponse = $application->run($illuminateRequest);
261258
$response = Response::make($illuminateResponse, $swooleResponse);

src/Server/Sandbox.php

Lines changed: 64 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()
8489
{
85-
$this->config = clone $application->getApplication()['config'];
90+
$this->config = clone $this->application->getApplication()->make('config');
91+
}
92+
93+
/**
94+
* Initialize customized service providers.
95+
*/
96+
protected function setInitialProviders()
97+
{
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,42 @@ 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+
protected function rebindProviderContainer($provider, $application)
179+
{
180+
$closure = function () use ($application) {
181+
$this->app = $application;
182+
};
183+
184+
$resetProvider = $closure->bindTo($provider, $provider);
185+
$resetProvider();
186+
}
187+
129188
/**
130189
* Reset laravel/lumen's config to initial values.
131190
*/

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'])) {

0 commit comments

Comments
 (0)