Skip to content
This repository was archived by the owner on Jan 29, 2020. It is now read-only.

Commit c20de15

Browse files
committed
Move max buffer length argument to SapiStreamEmitter property
Emitter-specific arguments should be properties.
1 parent 98f297e commit c20de15

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/Emitter/SapiStreamEmitter.php

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,23 @@ class SapiStreamEmitter implements EmitterInterface
1515
{
1616
use SapiEmitterTrait;
1717

18+
/**
19+
* @var int Maximum output buffering size for each iteration.
20+
*/
21+
private $maxBufferLength;
22+
23+
public function __construct(int $maxBufferLength = 8192)
24+
{
25+
$this->maxBufferLength = $maxBufferLength;
26+
}
27+
1828
/**
1929
* Emits a response for a PHP SAPI environment.
2030
*
2131
* Emits the status line and headers via the header() function, and the
2232
* body content via the output buffer.
23-
*
24-
* @param int $maxBufferLength Maximum output buffering size for each iteration
2533
*/
26-
public function emit(ResponseInterface $response, int $maxBufferLength = 8192) : bool
34+
public function emit(ResponseInterface $response) : bool
2735
{
2836
$this->assertNoPreviousOutput();
2937
$this->emitHeaders($response);
@@ -32,18 +40,18 @@ public function emit(ResponseInterface $response, int $maxBufferLength = 8192) :
3240
$range = $this->parseContentRange($response->getHeaderLine('Content-Range'));
3341

3442
if (is_array($range) && $range[0] === 'bytes') {
35-
$this->emitBodyRange($range, $response, $maxBufferLength);
43+
$this->emitBodyRange($range, $response);
3644
return true;
3745
}
3846

39-
$this->emitBody($response, $maxBufferLength);
47+
$this->emitBody($response);
4048
return true;
4149
}
4250

4351
/**
4452
* Emit the message body.
4553
*/
46-
private function emitBody(ResponseInterface $response, int $maxBufferLength) : void
54+
private function emitBody(ResponseInterface $response) : void
4755
{
4856
$body = $response->getBody();
4957

@@ -57,14 +65,14 @@ private function emitBody(ResponseInterface $response, int $maxBufferLength) : v
5765
}
5866

5967
while (! $body->eof()) {
60-
echo $body->read($maxBufferLength);
68+
echo $body->read($this->maxBufferLength);
6169
}
6270
}
6371

6472
/**
6573
* Emit a range of the message body.
6674
*/
67-
private function emitBodyRange(array $range, ResponseInterface $response, int $maxBufferLength) : void
75+
private function emitBodyRange(array $range, ResponseInterface $response) : void
6876
{
6977
list($unit, $first, $last, $length) = $range;
7078

@@ -86,7 +94,7 @@ private function emitBodyRange(array $range, ResponseInterface $response, int $m
8694
$remaining = $length;
8795

8896
while ($remaining >= $maxBufferLength && ! $body->eof()) {
89-
$contents = $body->read($maxBufferLength);
97+
$contents = $body->read($this->maxBufferLength);
9098
$remaining -= strlen($contents);
9199

92100
echo $contents;

test/Emitter/SapiStreamEmitterTest.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ function ($bufferLength) use (& $peakBufferLength) {
204204
->withBody($stream->reveal());
205205

206206
ob_start();
207-
$this->emitter->emit($response, $maxBufferLength);
207+
$emitter = new SapiStreamEmitter($maxBufferLength);
208+
$emitter->emit($response);
208209
$emittedContents = ob_get_clean();
209210

210211
if ($seekable) {
@@ -350,7 +351,8 @@ function ($bufferLength) use (& $peakBufferLength) {
350351
->withBody($stream->reveal());
351352

352353
ob_start();
353-
$this->emitter->emit($response, $maxBufferLength);
354+
$emitter = new SapiStreamEmitter($maxBufferLength);
355+
$emitter->emit($response);
354356
$emittedContents = ob_get_clean();
355357

356358
$stream->rewind()->shouldNotBeCalled();
@@ -493,7 +495,8 @@ function () use (& $closureTrackMemoryUsage) {
493495

494496
gc_disable();
495497

496-
$this->emitter->emit($response, $maxBufferLength);
498+
$emitter = new SapiStreamEmitter($maxBufferLength);
499+
$emitter->emit($response);
497500

498501
ob_end_flush();
499502

0 commit comments

Comments
 (0)