6
6
use Illuminate \Container \Container ;
7
7
use SwooleTW \Http \Server \Application ;
8
8
use Illuminate \Support \Facades \Facade ;
9
+ use Illuminate \Support \ServiceProvider ;
9
10
use Laravel \Lumen \Application as LumenApplication ;
10
11
11
12
class Sandbox
@@ -30,6 +31,11 @@ class Sandbox
30
31
*/
31
32
protected $ request ;
32
33
34
+ /**
35
+ * @var array
36
+ */
37
+ protected $ providers = [];
38
+
33
39
/**
34
40
* @var boolean
35
41
*/
@@ -52,7 +58,8 @@ public static function make(Application $application)
52
58
public function __construct (Application $ application )
53
59
{
54
60
$ this ->setApplication ($ application );
55
- $ this ->setInitialConfig ($ application );
61
+ $ this ->setInitialConfig ();
62
+ $ this ->setInitialProviders ();
56
63
}
57
64
58
65
/**
@@ -77,12 +84,26 @@ public function setRequest(Request $request)
77
84
78
85
/**
79
86
* Set config snapshot.
80
- *
81
- * @param \SwooleTW\Http\Server\Application
82
87
*/
83
- protected function setInitialConfig (Application $ application )
88
+ protected function setInitialConfig ()
89
+ {
90
+ $ this ->config = clone $ this ->application ->getApplication ()->make ('config ' );
91
+ }
92
+
93
+ /**
94
+ * Initialize customized service providers.
95
+ */
96
+ protected function setInitialProviders ()
84
97
{
85
- $ this ->config = clone $ application ->getApplication ()['config ' ];
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
+ }
86
107
}
87
108
88
109
/**
@@ -111,8 +132,10 @@ protected function resetLaravelApp($application)
111
132
$ this ->resetSession ($ application );
112
133
$ this ->resetCookie ($ application );
113
134
$ this ->clearInstances ($ application );
135
+ $ this ->bindRequest ($ application );
114
136
$ this ->rebindRouterContainer ($ application );
115
137
$ this ->rebindViewContainer ($ application );
138
+ $ this ->resetProviders ($ application );
116
139
}
117
140
118
141
/**
@@ -126,6 +149,45 @@ protected function clearInstances($application)
126
149
}
127
150
}
128
151
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
+ /**
179
+ * Rebind service provider's container.
180
+ */
181
+ protected function rebindProviderContainer ($ provider , $ application )
182
+ {
183
+ $ closure = function () use ($ application ) {
184
+ $ this ->app = $ application ;
185
+ };
186
+
187
+ $ resetProvider = $ closure ->bindTo ($ provider , $ provider );
188
+ $ resetProvider ();
189
+ }
190
+
129
191
/**
130
192
* Reset laravel/lumen's config to initial values.
131
193
*/
0 commit comments