@@ -49,17 +49,21 @@ public function index($options) {
4949 // Defines the content type and body of the response.
5050 if ($ opts ['attachments ' ] === true ) {
5151 $ content_type = 'multipart/mixed; boundary= ' .static ::BOUNDARY ;
52- $ body = $ this ->makeAttachmentsResult ($ statements , $ count , $ opts );
52+ $ body = function () use ($ statements , $ count , $ opts ) {
53+ return $ this ->makeAttachmentsResult ($ statements , $ count , $ opts );
54+ };
5355 } else {
5456 $ content_type = 'application/json; ' ;
55- $ body = $ this ->makeStatementsResult ($ statements , $ count , $ opts );
57+ $ body = function () use ($ statements , $ count , $ opts ) {
58+ return $ this ->makeStatementsResult ($ statements , $ count , $ opts );
59+ };
5660 }
5761
5862 // Creates the response.
59- return \Response::make ($ body , 200 , [
63+ return \Response::stream ($ body , 200 , [
6064 'Content-Type ' => $ content_type ,
6165 'X-Experience-API-Consistent-Through ' => Helpers::getCurrentDate ()
62- ]);;
66+ ]);
6367 }
6468
6569 /**
@@ -83,7 +87,7 @@ private function makeStatementsResult(array $statements, $count, array $opts) {
8387 'statements ' => $ statements
8488 ];
8589
86- return json_encode ($ statement_result );
90+ $ this -> emit ( json_encode ($ statement_result) );
8791 }
8892
8993 /**
@@ -96,24 +100,37 @@ private function makeAttachmentsResult(array $statements, $count, array $opts) {
96100 $ boundary = static ::BOUNDARY ;
97101 $ eol = static ::EOL ;
98102 $ content_type = 'multipart/mixed; boundary= ' .$ boundary ;
99- $ statement_result = "Content-Type:application/json $ eol$ eol " .$ this ->makeStatementsResult (
103+
104+ $ this ->emit ("-- $ boundary$ eol " );
105+ $ this ->emit ("Content-Type:application/json $ eol$ eol " );
106+ $ this ->makeStatementsResult (
100107 $ statements ,
101108 $ count ,
102109 $ opts
103110 );
104111
105- return " -- $ boundary $ eol " . implode (
106- " $ eol -- $ boundary $ eol " ,
107- array_merge ([ $ statement_result ], array_map ( function ( $ attachment ) use ( $ eol , $ boundary ) {
108- return (
112+ $ attachments = $ this -> statements -> getAttachments ( $ statements , $ opts );
113+ foreach ( $ attachments as $ attachment ) {
114+ $ this -> emit (
115+ " $ eol -- $ boundary $ eol " .
109116 'Content-Type: ' .$ attachment ->content_type .$ eol .
110117 'Content-Transfer-Encoding:binary ' .$ eol .
111118 'X-Experience-API-Hash: ' .$ attachment ->hash .
112- $ eol .$ eol .
113- $ attachment ->content
119+ $ eol .$ eol
114120 );
115- }, $ this ->statements ->getAttachments ($ statements , $ opts )))
116- )."$ eol-- $ boundary-- " ;
121+ while (!feof ($ attachment ->content )) {
122+ $ this ->emit (fread ($ attachment ->content , 8192 ));
123+ }
124+ fclose ($ attachment ->content );
125+ }
126+
127+ $ this ->emit ("$ eol-- $ boundary-- " );
128+ }
129+
130+ private function emit ($ value ) {
131+ echo $ value ;
132+ flush ();
133+ ob_flush ();
117134 }
118135
119136 private function getMoreLink ($ count , $ limit , $ offset ) {
0 commit comments