3030 */
3131class PSR7Worker implements PSR7WorkerInterface
3232{
33- public int $ chunk_size = 8 * 1024 ;
33+ /**
34+ * @var int Preferred chunk size for streaming output.
35+ * if not greater than 0, then streaming response is turned off
36+ */
37+ public int $ chunkSize = 0 ;
3438
3539 /**
3640 * @var HttpWorker
@@ -113,11 +117,18 @@ public function waitRequest(): ?ServerRequestInterface
113117 */
114118 public function respond (ResponseInterface $ response ): void
115119 {
116- $ this ->httpWorker ->respondStream (
117- $ response ->getStatusCode (),
118- $ this ->streamToGenerator ($ response ->getBody ()),
119- $ response ->getHeaders ()
120- );
120+ if ($ this ->chunkSize > 0 ) {
121+ $ this ->httpWorker ->respondStream (
122+ $ response ->getStatusCode (),
123+ $ this ->streamToGenerator ($ response ->getBody ()),
124+ $ response ->getHeaders ()
125+ );
126+ } else {
127+ $ this ->httpWorker ->respond (
128+ $ response ->getStatusCode (),
129+ (string )$ response ->getBody (),
130+ $ response ->getHeaders ());
131+ }
121132 }
122133
123134 /**
@@ -128,17 +139,17 @@ private function streamToGenerator(StreamInterface $stream): Generator
128139 {
129140 $ stream ->rewind ();
130141 $ size = $ stream ->getSize ();
131- if ($ size !== null && $ size < $ this ->chunk_size ) {
142+ if ($ size !== null && $ size < $ this ->chunkSize ) {
132143 return (string )$ stream ;
133144 }
134145 $ sum = 0 ;
135146 while (!$ stream ->eof ()) {
136147 if ($ size === null ) {
137- $ chunk = $ stream ->read ($ this ->chunk_size );
148+ $ chunk = $ stream ->read ($ this ->chunkSize );
138149 } else {
139150 $ left = $ size - $ sum ;
140- $ chunk = $ stream ->read (\min ($ this ->chunk_size , $ left ));
141- if ($ left <= $ this ->chunk_size && \strlen ($ chunk ) === $ left ) {
151+ $ chunk = $ stream ->read (\min ($ this ->chunkSize , $ left ));
152+ if ($ left <= $ this ->chunkSize && \strlen ($ chunk ) === $ left ) {
142153 return $ chunk ;
143154 }
144155 }
0 commit comments