7
7
use BEAR \Resource \NullResourceObject ;
8
8
use BEAR \Resource \ResourceObject ;
9
9
use BEAR \Resource \Uri ;
10
- use Throwable ;
11
10
12
- use function array_key_exists ;
13
- use function array_pop ;
11
+ use function array_filter ;
14
12
use function array_shift ;
15
- use function assert ;
16
13
use function implode ;
17
14
use function json_decode ;
18
15
use function preg_match ;
19
- use function var_dump ;
20
16
21
17
use const PHP_EOL ;
22
18
@@ -37,67 +33,48 @@ public function __invoke(Uri $uri, array $output): ResourceObject
37
33
return $ ro ;
38
34
}
39
35
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 = [];
62
38
$ status = (string ) array_shift ($ output );
63
- do {
64
- $ line = array_shift ($ output );
65
- if ($ line === null ) {
66
- break ;
67
- }
68
39
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
+ }
76
54
77
55
$ ro = new NullResourceObject ();
78
56
$ ro ->uri = $ uri ;
79
57
$ ro ->code = $ this ->getCode ($ status );
80
58
$ ro ->headers = $ this ->getHeaders ($ headers );
59
+
60
+ // レスポンスボディの処理
81
61
$ view = $ this ->getJsonView ($ body );
82
- $ ro ->body = (array ) json_decode ($ view) ;
62
+ $ ro ->body = (array ) json_decode ($ view, true ) ?: [] ;
83
63
$ ro ->view = $ view ;
84
64
85
65
return $ ro ;
86
66
}
87
67
88
68
private function getCode (string $ status ): int
89
69
{
90
- // 空の出力の場合は500を返す
91
70
if (empty ($ status )) {
92
71
return 500 ;
93
72
}
94
73
95
- // HTTP/1.0やHTTP/1.1のステータスコードを抽出
96
74
if (preg_match ('/HTTP\/\d\.\d\s+(\d{3})/ ' , $ status , $ match )) {
97
75
return (int ) $ match [1 ];
98
76
}
99
77
100
- // ステータスコードが見つからない場合は500を返す
101
78
return 500 ;
102
79
}
103
80
@@ -109,12 +86,12 @@ private function getCode(string $status): int
109
86
private function getHeaders (array $ headers ): array
110
87
{
111
88
$ keyedHeader = [];
112
- array_pop ($ headers );
113
89
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 ];
118
95
}
119
96
120
97
return $ keyedHeader ;
@@ -125,7 +102,7 @@ private function getHeaders(array $headers): array
125
102
*/
126
103
private function getJsonView (array $ body ): string
127
104
{
128
- array_pop ($ body );
105
+ $ body = array_filter ($ body, ' strlen ' );
129
106
130
107
return implode (PHP_EOL , $ body );
131
108
}
0 commit comments