66use Illuminate \Container \Container ;
77use SwooleTW \Http \Server \Application ;
88use Illuminate \Support \Facades \Facade ;
9+ use Illuminate \Support \ServiceProvider ;
910use Laravel \Lumen \Application as LumenApplication ;
1011
1112class 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 ()
89+ {
90+ $ this ->config = clone $ this ->application ->getApplication ()->make ('config ' );
91+ }
92+
93+ /**
94+ * Initialize customized service providers.
95+ */
96+ protected function setInitialProviders ()
8497 {
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+ }
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,45 @@ 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+ /**
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+
129191 /**
130192 * Reset laravel/lumen's config to initial values.
131193 */
0 commit comments