Skip to content

Commit c1c64c5

Browse files
committed
support swoole driver
1 parent 8389cae commit c1c64c5

2 files changed

Lines changed: 30 additions & 29 deletions

File tree

src/Http/ServerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
namespace MicroPHP\Framework\Http;
66

7-
use MicroPHP\Framework\Config\Config;
87
use MicroPHP\Framework\Http\Contract\HttpServerInterface;
98
use MicroPHP\Framework\Http\Enum\Driver;
109
use MicroPHP\RoadRunner\RoadRunnerHttpServer;
@@ -17,6 +16,7 @@ class ServerFactory
1716
public static function newServer(): HttpServerInterface
1817
{
1918
$serverConfig = new ServerConfig();
19+
2020
return match ($serverConfig->getDriver()) {
2121
Driver::WORKERMAN => new WorkermanHttpServer(),
2222
Driver::ROADRUNNER => new RoadRunnerHttpServer(),

src/Http/ServerRequest.php

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -38,18 +38,45 @@ public static function fromSwoole(Request $swooleRequest): ServerRequestInterfac
3838
$body = $httpFactory->createStream((string) $swooleRequest->rawContent());
3939
$protocol = isset($server['server_protocol']) ? str_replace('HTTP/', '', $server['server_protocol']) : '1.1';
4040
$request = new ServerRequest($method, $uri, $headers, $body, $protocol, $server);
41+
4142
return $request->withCookieParams($swooleRequest->cookie ?? [])
4243
->withQueryParams($swooleRequest->get ?? [])
4344
->withParsedBody(self::normalizeParsedBody($swooleRequest->post ?? [], $request))
4445
->withUploadedFiles(self::normalizeFiles($swooleRequest->files ?? []));
4546
}
4647

48+
protected static function normalizeParsedBody(array $data = [], RequestInterface $request = null): array
49+
{
50+
if (! $request) {
51+
return $data;
52+
}
53+
54+
$rawContentType = $request->getHeaderLine('content-type');
55+
if (($pos = strpos($rawContentType, ';')) !== false) {
56+
$contentType = strtolower(substr($rawContentType, 0, $pos));
57+
} else {
58+
$contentType = strtolower($rawContentType);
59+
}
60+
switch ($contentType) {
61+
case 'application/json':
62+
case 'text/json':
63+
$data = json_decode((string) $request->getBody(), true);
64+
break;
65+
case 'application/xml':
66+
case 'text/xml':
67+
$data = (array) simplexml_load_string((string) $request->getBody());
68+
break;
69+
}
70+
71+
return $data;
72+
}
73+
4774
private static function getUriFromSwooleRequest(Request $swooleRequest): UriInterface
4875
{
4976
$server = $swooleRequest->server;
5077
$header = $swooleRequest->header;
5178
$uri = new Uri();
52-
$uri = $uri->withScheme(! empty($server['https']) && $server['https'] !== 'off' ? 'https' : 'http');
79+
$uri = $uri->withScheme(! empty($server['https']) && 'off' !== $server['https'] ? 'https' : 'http');
5380

5481
$hasPort = false;
5582
if (isset($server['http_host'])) {
@@ -109,32 +136,6 @@ private static function parseHost(string $httpHost): array
109136

110137
private static function getUriDefaultPort(UriInterface $uri): ?int
111138
{
112-
return $uri->getScheme() === 'https' ? 443 : ($uri->getScheme() === 'http' ? 80 : null);
113-
}
114-
115-
protected static function normalizeParsedBody(array $data = [], ?RequestInterface $request = null): array
116-
{
117-
if (! $request) {
118-
return $data;
119-
}
120-
121-
$rawContentType = $request->getHeaderLine('content-type');
122-
if (($pos = strpos($rawContentType, ';')) !== false) {
123-
$contentType = strtolower(substr($rawContentType, 0, $pos));
124-
} else {
125-
$contentType = strtolower($rawContentType);
126-
}
127-
switch ($contentType) {
128-
case 'application/json':
129-
case 'text/json':
130-
$data = json_decode((string) $request->getBody(), true);
131-
break;
132-
case 'application/xml':
133-
case 'text/xml':
134-
$data = (array) simplexml_load_string((string) $request->getBody());
135-
break;
136-
}
137-
138-
return $data;
139+
return 'https' === $uri->getScheme() ? 443 : ('http' === $uri->getScheme() ? 80 : null);
139140
}
140141
}

0 commit comments

Comments
 (0)