From 436f3bc2d4b48e32522d749cb59d148340dc4642 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ha=C5=82as=20Piotr?= Date: Thu, 5 Jun 2025 23:58:22 +0200 Subject: [PATCH] Use kernel as app --- composer.json | 4 ++- src/RequestHandler.php | 63 ++++++++++++++++++++++++++++++++++++++++++ src/Runner.php | 10 +++---- src/Runtime.php | 4 +-- 4 files changed, 73 insertions(+), 8 deletions(-) create mode 100644 src/RequestHandler.php diff --git a/composer.json b/composer.json index 12cbc0d..3b97e37 100644 --- a/composer.json +++ b/composer.json @@ -13,7 +13,9 @@ "php": ">=8.0.5", "psr/http-server-handler": "^1.0", "react/http": "^1.6", - "symfony/runtime": "^5.4 || ^6.0 || ^7.0" + "symfony/runtime": "^5.4 || ^6.0 || ^7.0", + "symfony/http-kernel": "^7.3", + "ext-pcntl": "*" }, "require-dev": { "phpunit/phpunit": "^9.5" diff --git a/src/RequestHandler.php b/src/RequestHandler.php new file mode 100644 index 0000000..0fd3826 --- /dev/null +++ b/src/RequestHandler.php @@ -0,0 +1,63 @@ +kernel = $kernel; + } + + public function handle(ServerRequestInterface $request): ResponseInterface + { + try { + $method = $request->getMethod(); + $headers = $request->getHeaders(); + + $query = $request->getQueryParams(); + $content = $request->getBody(); + $post = array(); + if (in_array(strtoupper($method), array('POST', 'PUT', 'DELETE', 'PATCH')) && + isset($headers['Content-Type']) && (str_starts_with($headers['Content-Type'][0], 'application/x-www-form-urlencoded')) + ) { + parse_str($content, $post); + } + $sfRequest = new Request( + $query, + $post, + array(), + array(), + $request->getUploadedFiles(), + array(), + $content + ); + $sfRequest->setMethod($method); + $sfRequest->headers->replace($headers); + + $sfRequest->server->set('REQUEST_URI', $request->getUri()->getPath()); + if (isset($headers['Host'])) { + $sfRequest->server->set('SERVER_NAME', $headers['Host'][0]); + } + $sfResponse = $this->kernel->handle($sfRequest); + + $response = new Response($sfResponse->getStatusCode(), $sfResponse->headers->all(), $sfResponse->getContent()); + $this->kernel->terminate($sfRequest, $sfResponse); + + return $response; + } catch (Throwable $exception) { + printf("Exception: %s\n, Trace: %s\n", $exception->getMessage(), $exception->getTraceAsString()); + exit(1); + } + } +} diff --git a/src/Runner.php b/src/Runner.php index 2143c12..5b2099e 100644 --- a/src/Runner.php +++ b/src/Runner.php @@ -2,23 +2,23 @@ namespace Runtime\React; -use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Runtime\RunnerInterface; class Runner implements RunnerInterface { - private RequestHandlerInterface $application; + private KernelInterface $kernel; private ServerFactory $serverFactory; - public function __construct(ServerFactory $serverFactory, RequestHandlerInterface $application) + public function __construct(ServerFactory $serverFactory, KernelInterface $kernel) { $this->serverFactory = $serverFactory; - $this->application = $application; + $this->kernel = $kernel; } public function run(): int { - $loop = $this->serverFactory->createServer($this->application); + $loop = $this->serverFactory->createServer(new RequestHandler($this->kernel)); $loop->run(); return 0; diff --git a/src/Runtime.php b/src/Runtime.php index 3cf2569..238cc82 100644 --- a/src/Runtime.php +++ b/src/Runtime.php @@ -2,7 +2,7 @@ namespace Runtime\React; -use Psr\Http\Server\RequestHandlerInterface; +use Symfony\Component\HttpKernel\KernelInterface; use Symfony\Component\Runtime\GenericRuntime; use Symfony\Component\Runtime\RunnerInterface; @@ -15,7 +15,7 @@ class Runtime extends GenericRuntime { public function getRunner(?object $application): RunnerInterface { - if ($application instanceof RequestHandlerInterface) { + if ($application instanceof KernelInterface) { return new Runner(new ServerFactory($this->options), $application); }