Skip to content
This repository was archived by the owner on May 23, 2023. It is now read-only.

Commit aaaae22

Browse files
committed
Added getHttpServer and getSocketServer methods
1 parent f79352d commit aaaae22

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

src/Router.php

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use LogicException;
1010
use Psr\Http\Message\ResponseInterface;
1111
use Psr\Http\Message\ServerRequestInterface;
12+
use React\Http\HttpServer;
1213
use React\Http\Message\ServerRequest;
1314
use React\Socket\SocketServer;
1415
use ReflectionMethod;
@@ -33,6 +34,7 @@ final class Router {
3334
private array $E500Handlers = [];
3435
private string $baseRoute = "";
3536
private Environment $twig;
37+
private HttpServer $http;
3638

3739
/**
3840
* @param SocketServer $socket
@@ -239,8 +241,12 @@ public function afterMiddleware(string $pattern, array|Closure $handler): self
239241
/**
240242
* @return void
241243
*/
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) {
244250
try {
245251
return $this->handle($request);
246252
} catch (\Throwable $e) {
@@ -267,8 +273,28 @@ public function listen() {
267273
}
268274
}
269275
});
276+
}
270277

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;
272298
}
273299

274300
/**

0 commit comments

Comments
 (0)