Skip to content

Commit 8050ad1

Browse files
committed
fix #558 and #559
1 parent bc16e9a commit 8050ad1

File tree

3 files changed

+62
-54
lines changed

3 files changed

+62
-54
lines changed

api.php

+31-27
Original file line numberDiff line numberDiff line change
@@ -7452,9 +7452,38 @@ public function __construct(Config $config)
74527452
$this->debug = $config->getDebug();
74537453
}
74547454

7455+
private function parseBody(string $body) /*: ?object*/
7456+
{
7457+
$first = substr($body, 0, 1);
7458+
if ($first == '[' || $first == '{') {
7459+
$object = json_decode($body);
7460+
$causeCode = json_last_error();
7461+
if ($causeCode !== JSON_ERROR_NONE) {
7462+
$object = null;
7463+
}
7464+
} else {
7465+
parse_str($body, $input);
7466+
foreach ($input as $key => $value) {
7467+
if (substr($key, -9) == '__is_null') {
7468+
$input[substr($key, 0, -9)] = null;
7469+
unset($input[$key]);
7470+
}
7471+
}
7472+
$object = (object) $input;
7473+
}
7474+
return $object;
7475+
}
7476+
74557477
public function handle(ServerRequestInterface $request): ResponseInterface
74567478
{
74577479
$response = null;
7480+
$body = $request->getBody();
7481+
if ($body->isReadable() && $body->isSeekable()) {
7482+
$contents = $body->getContents();
7483+
$body->rewind();
7484+
$parsedBody = $this->parseBody($contents);
7485+
$request = $request->withParsedBody($parsedBody);
7486+
}
74587487
try {
74597488
$response = $this->router->route($request);
74607489
} catch (\Throwable $e) {
@@ -7636,37 +7665,13 @@ public function getOpenApiBase(): array
76367665

76377666
class RequestFactory
76387667
{
7639-
private static function parseBody(string $body) /*: ?object*/
7640-
{
7641-
$first = substr($body, 0, 1);
7642-
if ($first == '[' || $first == '{') {
7643-
$object = json_decode($body);
7644-
$causeCode = json_last_error();
7645-
if ($causeCode !== JSON_ERROR_NONE) {
7646-
$object = null;
7647-
}
7648-
} else {
7649-
parse_str($body, $input);
7650-
foreach ($input as $key => $value) {
7651-
if (substr($key, -9) == '__is_null') {
7652-
$input[substr($key, 0, -9)] = null;
7653-
unset($input[$key]);
7654-
}
7655-
}
7656-
$object = (object) $input;
7657-
}
7658-
return $object;
7659-
}
7660-
76617668
public static function fromGlobals(): ServerRequestInterface
76627669
{
76637670
$psr17Factory = new Psr17Factory();
76647671
$creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
76657672
$serverRequest = $creator->fromGlobals();
7666-
$body = file_get_contents('php://input');
7667-
if ($body) {
7668-
$serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
7669-
}
7673+
$stream = $psr17Factory->createStreamFromResource('php://input');
7674+
$serverRequest = $serverRequest->withBody($stream);
76707675
return $serverRequest;
76717676
}
76727677

@@ -7689,7 +7694,6 @@ public static function fromString(string $request): ServerRequestInterface
76897694
$stream = $psr17Factory->createStream($body);
76907695
$stream->rewind();
76917696
$serverRequest = $serverRequest->withBody($stream);
7692-
$serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
76937697
}
76947698
return $serverRequest;
76957699
}

src/Tqdev/PhpCrudApi/Api.php

+29
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,38 @@ public function __construct(Config $config)
131131
$this->debug = $config->getDebug();
132132
}
133133

134+
private function parseBody(string $body) /*: ?object*/
135+
{
136+
$first = substr($body, 0, 1);
137+
if ($first == '[' || $first == '{') {
138+
$object = json_decode($body);
139+
$causeCode = json_last_error();
140+
if ($causeCode !== JSON_ERROR_NONE) {
141+
$object = null;
142+
}
143+
} else {
144+
parse_str($body, $input);
145+
foreach ($input as $key => $value) {
146+
if (substr($key, -9) == '__is_null') {
147+
$input[substr($key, 0, -9)] = null;
148+
unset($input[$key]);
149+
}
150+
}
151+
$object = (object) $input;
152+
}
153+
return $object;
154+
}
155+
134156
public function handle(ServerRequestInterface $request): ResponseInterface
135157
{
136158
$response = null;
159+
$body = $request->getBody();
160+
if ($body->isReadable() && $body->isSeekable()) {
161+
$contents = $body->getContents();
162+
$body->rewind();
163+
$parsedBody = $this->parseBody($contents);
164+
$request = $request->withParsedBody($parsedBody);
165+
}
137166
try {
138167
$response = $this->router->route($request);
139168
} catch (\Throwable $e) {

src/Tqdev/PhpCrudApi/RequestFactory.php

+2-27
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,13 @@
77

88
class RequestFactory
99
{
10-
private static function parseBody(string $body) /*: ?object*/
11-
{
12-
$first = substr($body, 0, 1);
13-
if ($first == '[' || $first == '{') {
14-
$object = json_decode($body);
15-
$causeCode = json_last_error();
16-
if ($causeCode !== JSON_ERROR_NONE) {
17-
$object = null;
18-
}
19-
} else {
20-
parse_str($body, $input);
21-
foreach ($input as $key => $value) {
22-
if (substr($key, -9) == '__is_null') {
23-
$input[substr($key, 0, -9)] = null;
24-
unset($input[$key]);
25-
}
26-
}
27-
$object = (object) $input;
28-
}
29-
return $object;
30-
}
31-
3210
public static function fromGlobals(): ServerRequestInterface
3311
{
3412
$psr17Factory = new Psr17Factory();
3513
$creator = new ServerRequestCreator($psr17Factory, $psr17Factory, $psr17Factory, $psr17Factory);
3614
$serverRequest = $creator->fromGlobals();
37-
$body = file_get_contents('php://input');
38-
if ($body) {
39-
$serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
40-
}
15+
$stream = $psr17Factory->createStreamFromResource('php://input');
16+
$serverRequest = $serverRequest->withBody($stream);
4117
return $serverRequest;
4218
}
4319

@@ -60,7 +36,6 @@ public static function fromString(string $request): ServerRequestInterface
6036
$stream = $psr17Factory->createStream($body);
6137
$stream->rewind();
6238
$serverRequest = $serverRequest->withBody($stream);
63-
$serverRequest = $serverRequest->withParsedBody(self::parseBody($body));
6439
}
6540
return $serverRequest;
6641
}

0 commit comments

Comments
 (0)