Skip to content

Use kernel as app #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
63 changes: 63 additions & 0 deletions src/RequestHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Runtime\React;

use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use React\Http\Message\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\KernelInterface;
use Throwable;

class RequestHandler implements RequestHandlerInterface
{
private KernelInterface $kernel;

public function __construct(KernelInterface $kernel)
{
$this->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);
}
}
}
10 changes: 5 additions & 5 deletions src/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Runtime.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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);
}

Expand Down