44
55class Output
66{
7- public function response ($ response , $ context = null )
8- {
9- $ this ->outputStatusHeader (
10- $ response ->statusCode (),
11- $ response ->reasonPhrase ()
12- );
13-
14- $ this ->headers ($ response ->headers ()->asArray ());
15- if ($ context !== null ) {
16- $ this ->outputCookies ($ context ->cookies );
17- }
18- $ this ->outputStream ($ response ->body ());
19- }
20-
217 public function responseMessage ($ response )
228 {
23- $ this ->outputStatusHeader (
9+ $ this ->statusHeader (
2410 $ response ->getStatusCode (),
2511 $ response ->getReasonPhrase (),
2612 $ response ->getProtocolVersion ()
2713 );
2814
2915 $ this ->headers ($ response ->getHeaders ());
30- $ this ->outputStream ($ response ->getBody ());
16+ $ this ->body ($ response ->getBody ());
3117 }
3218
3319 protected function headers ($ headers )
3420 {
3521 foreach ($ headers as $ name => $ lines ) {
3622 foreach ($ lines as $ key => $ line ) {
37- $ this ->header ("$ name: $ line " , $ key === 0 );
23+ $ this ->header ("$ name: $ line " , false );
3824 }
3925 }
4026 }
@@ -44,36 +30,33 @@ protected function header($header, $replace = true)
4430 header ($ header , $ replace );
4531 }
4632
47- protected function outputCookies ( $ cookies )
33+ protected function output ( $ header )
4834 {
49- foreach ($ cookies ->getUpdates () as $ update ) {
50- setcookie (
51- $ update ->name (),
52- $ update ->value (),
53- $ update ->expires (),
54- $ update ->path (),
55- $ update ->domain (),
56- $ update ->secure (),
57- $ update ->httpOnly ()
58- );
59- }
35+ echo $ header ;
6036 }
6137
62- protected function outputStream ( $ stream )
38+ protected function fpassthru ( $ handle )
6339 {
64- if ($ body instanceof \PHPixie \HTTP \Messages \Stream \Implementation) {
65- fpassthru ($ body ->detach ());
40+ fpassthru ($ handle );
41+ }
42+
43+ protected function body ($ stream )
44+ {
45+ if ($ stream instanceof \PHPixie \HTTP \Messages \Stream \Implementation) {
46+ $ stream ->rewind ();
47+ $ this ->fpassthru ($ stream ->resource ());
6648
6749 }else {
68- echo ( string ) $ body ;
50+ $ this -> output (( string ) $ stream ) ;
6951 }
7052 }
7153
72- protected function outputStatusHeader ($ statusCode , $ reasonPhrase , $ protocolVersion = '1.1 ' )
54+ protected function statusHeader ($ statusCode , $ reasonPhrase , $ protocolVersion = '1.1 ' )
7355 {
7456 if ($ protocolVersion === null ) {
7557 $ protocolVersion = '1.1 ' ;
76- return "HTTP/ {$ protocolVersion } $ statusCode $ reasonPhrase " ;
7758 }
59+
60+ $ this ->header ("HTTP/ {$ protocolVersion } $ statusCode $ reasonPhrase " );
7861 }
7962}
0 commit comments