9
9
use LogicException ;
10
10
use Psr \Http \Message \ResponseInterface ;
11
11
use Psr \Http \Message \ServerRequestInterface ;
12
+ use React \Http \HttpServer ;
12
13
use React \Http \Message \ServerRequest ;
13
14
use React \Socket \SocketServer ;
14
15
use ReflectionMethod ;
@@ -33,6 +34,7 @@ final class Router {
33
34
private array $ E500Handlers = [];
34
35
private string $ baseRoute = "" ;
35
36
private Environment $ twig ;
37
+ private HttpServer $ http ;
36
38
37
39
/**
38
40
* @param SocketServer $socket
@@ -239,8 +241,12 @@ public function afterMiddleware(string $pattern, array|Closure $handler): self
239
241
/**
240
242
* @return void
241
243
*/
242
- public function listen () {
243
- $ http = new \React \Http \HttpServer (function (ServerRequestInterface $ request ) {
244
+ public function createHttpServer () {
245
+ if (isset ($ this ->http )) {
246
+ return ;
247
+ }
248
+
249
+ $ this ->http = new HttpServer (function (ServerRequestInterface $ request ) {
244
250
try {
245
251
return $ this ->handle ($ request );
246
252
} catch (\Throwable $ e ) {
@@ -267,8 +273,28 @@ public function listen() {
267
273
}
268
274
}
269
275
});
276
+ }
270
277
271
- $ http ->listen ($ this ->socket );
278
+ public function listen () {
279
+ if (!isset ($ this ->http )) {
280
+ $ this ->createHttpServer ();
281
+ }
282
+
283
+ $ this ->http ->listen ($ this ->socket );
284
+ }
285
+
286
+ public function getHttpServer (): ?HttpServer
287
+ {
288
+ if (!isset ($ this ->http )) {
289
+ $ this ->createHttpServer ();
290
+ }
291
+
292
+ return $ this ->http ;
293
+ }
294
+
295
+ public function getSocketServer (): ?SocketServer
296
+ {
297
+ return $ this ->socket ;
272
298
}
273
299
274
300
/**
0 commit comments