Skip to content

Commit 50927ec

Browse files
committed
Refactor CreateResponse for cleaner header and body parsing.
Reorganized header and body processing for clarity and removed unused or redundant code such as `var_dump` and `array_pop`. Improved error handling and ensured consistent parsing of headers. Simplified status code extraction and response body management by leveraging better practices.
1 parent 2eb33fc commit 50927ec

File tree

1 file changed

+26
-49
lines changed

1 file changed

+26
-49
lines changed

src/Http/CreateResponse.php

Lines changed: 26 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,12 @@
77
use BEAR\Resource\NullResourceObject;
88
use BEAR\Resource\ResourceObject;
99
use BEAR\Resource\Uri;
10-
use Throwable;
1110

12-
use function array_key_exists;
13-
use function array_pop;
11+
use function array_filter;
1412
use function array_shift;
15-
use function assert;
1613
use function implode;
1714
use function json_decode;
1815
use function preg_match;
19-
use function var_dump;
2016

2117
use const PHP_EOL;
2218

@@ -37,67 +33,48 @@ public function __invoke(Uri $uri, array $output): ResourceObject
3733
return $ro;
3834
}
3935

40-
var_dump([
41-
'uri' => $uri,
42-
'output' => $output
43-
]);
44-
try {
45-
$ro = $this->invoke($uri, $output);
46-
} catch (Throwable $e) {
47-
$ro = new NullResourceObject();
48-
$ro->uri = $uri;
49-
$ro->code = 500;
50-
$ro->body = ['error' => $e->getMessage()];
51-
}
52-
53-
return $ro;
54-
}
55-
56-
/**
57-
* @param array<string> $output
58-
*/
59-
public function invoke(Uri $uri, array $output): ResourceObject
60-
{
61-
$headers = $body = [];
36+
$headers = [];
37+
$body = [];
6238
$status = (string) array_shift($output);
63-
do {
64-
$line = array_shift($output);
65-
if ($line === null) {
66-
break;
67-
}
6839

69-
$headers[] = $line;
70-
} while ($line !== '');
71-
72-
do {
73-
$line = array_shift($output);
74-
$body[] = (string) $line;
75-
} while ($line !== null);
40+
// ヘッダー部分の処理
41+
$headerComplete = false;
42+
foreach ($output as $line) {
43+
if (! $headerComplete) {
44+
if ($line === '') {
45+
$headerComplete = true;
46+
continue;
47+
}
48+
49+
$headers[] = $line;
50+
} else {
51+
$body[] = $line;
52+
}
53+
}
7654

7755
$ro = new NullResourceObject();
7856
$ro->uri = $uri;
7957
$ro->code = $this->getCode($status);
8058
$ro->headers = $this->getHeaders($headers);
59+
60+
// レスポンスボディの処理
8161
$view = $this->getJsonView($body);
82-
$ro->body = (array) json_decode($view);
62+
$ro->body = (array) json_decode($view, true) ?: [];
8363
$ro->view = $view;
8464

8565
return $ro;
8666
}
8767

8868
private function getCode(string $status): int
8969
{
90-
// 空の出力の場合は500を返す
9170
if (empty($status)) {
9271
return 500;
9372
}
9473

95-
// HTTP/1.0やHTTP/1.1のステータスコードを抽出
9674
if (preg_match('/HTTP\/\d\.\d\s+(\d{3})/', $status, $match)) {
9775
return (int) $match[1];
9876
}
9977

100-
// ステータスコードが見つからない場合は500を返す
10178
return 500;
10279
}
10380

@@ -109,12 +86,12 @@ private function getCode(string $status): int
10986
private function getHeaders(array $headers): array
11087
{
11188
$keyedHeader = [];
112-
array_pop($headers);
11389
foreach ($headers as $header) {
114-
preg_match('/(.+):\s(.+)/', $header, $matched);
115-
assert(array_key_exists(1, $matched));
116-
assert(array_key_exists(2, $matched));
117-
$keyedHeader[$matched[1]] = $matched[2];
90+
if (! preg_match('/^([^:]+):\s*(.*)$/', $header, $matches)) {
91+
continue;
92+
}
93+
94+
$keyedHeader[$matches[1]] = $matches[2];
11895
}
11996

12097
return $keyedHeader;
@@ -125,7 +102,7 @@ private function getHeaders(array $headers): array
125102
*/
126103
private function getJsonView(array $body): string
127104
{
128-
array_pop($body);
105+
$body = array_filter($body, 'strlen');
129106

130107
return implode(PHP_EOL, $body);
131108
}

0 commit comments

Comments
 (0)