Skip to content

Commit c1a0765

Browse files
committed
Use constructor parameter instead of public $bufferSize property
1 parent 585af73 commit c1a0765

6 files changed

Lines changed: 34 additions & 34 deletions

README.md

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -753,21 +753,22 @@ take care of the underlying stream resource.
753753
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
754754
stream resource manually.
755755

756-
The `$bufferSize` property controls the maximum buffer size in bytes to read
757-
at once from the stream.
756+
This class takes an optional `int|null $readChunkSize` parameter that controls
757+
the maximum buffer size in bytes to read at once from the stream.
758+
You can use a `null` value here in order to apply its default value.
758759
This value SHOULD NOT be changed unless you know what you're doing.
759760
This can be a positive number which means that up to X bytes will be read
760761
at once from the underlying stream resource. Note that the actual number
761762
of bytes read may be lower if the stream resource has less than X bytes
762763
currently available.
763-
This can be `null` which means "read everything available" from the
764+
This can be `-1` which means "read everything available" from the
764765
underlying stream resource.
765766
This should read until the stream resource is not readable anymore
766767
(i.e. underlying buffer drained), note that this does not neccessarily
767768
mean it reached EOF.
768769

769770
```php
770-
$stream->bufferSize = 8192;
771+
$stream = new ReadableResourceStream(STDIN, $loop, 8192);
771772
```
772773

773774
### WritableResourceStream
@@ -873,21 +874,23 @@ take care of the underlying stream resource.
873874
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
874875
stream resource manually.
875876

876-
The `$bufferSize` property controls the maximum buffer size in bytes to read
877-
at once from the stream.
877+
This class takes an optional `int|null $readChunkSize` parameter that controls
878+
the maximum buffer size in bytes to read at once from the stream.
879+
You can use a `null` value here in order to apply its default value.
878880
This value SHOULD NOT be changed unless you know what you're doing.
879881
This can be a positive number which means that up to X bytes will be read
880882
at once from the underlying stream resource. Note that the actual number
881883
of bytes read may be lower if the stream resource has less than X bytes
882884
currently available.
883-
This can be `null` which means "read everything available" from the
885+
This can be `-1` which means "read everything available" from the
884886
underlying stream resource.
885887
This should read until the stream resource is not readable anymore
886888
(i.e. underlying buffer drained), note that this does not neccessarily
887889
mean it reached EOF.
888890

889891
```php
890-
$stream->bufferSize = 8192;
892+
$conn = stream_socket_client('tcp://google.com:80');
893+
$stream = new DuplexResourceStream($conn, $loop, 8192);
891894
```
892895

893896
Any `write()` calls to this class will not be performaned instantly, but will

src/DuplexResourceStream.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
1616
* of bytes read may be lower if the stream resource has less than X bytes
1717
* currently available.
1818
*
19-
* This can be `null` which means read everything available from the
19+
* This can be `-1` which means read everything available from the
2020
* underlying stream resource.
2121
* This should read until the stream resource is not readable anymore
2222
* (i.e. underlying buffer drained), note that this does not neccessarily
2323
* mean it reached EOF.
2424
*
25-
* @var int|null
25+
* @var int
2626
*/
27-
public $bufferSize = 65536;
27+
private $bufferSize;
2828

2929
private $stream;
3030
protected $readable = true;
@@ -33,7 +33,7 @@ class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
3333
protected $loop;
3434
protected $buffer;
3535

36-
public function __construct($stream, LoopInterface $loop, WritableStreamInterface $buffer = null)
36+
public function __construct($stream, LoopInterface $loop, $readChunkSize = null, WritableStreamInterface $buffer = null)
3737
{
3838
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
3939
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -69,6 +69,7 @@ public function __construct($stream, LoopInterface $loop, WritableStreamInterfac
6969

7070
$this->stream = $stream;
7171
$this->loop = $loop;
72+
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
7273
$this->buffer = $buffer;
7374

7475
$that = $this;
@@ -169,7 +170,7 @@ public function handleData($stream)
169170
);
170171
});
171172

172-
$data = stream_get_contents($stream, $this->bufferSize === null ? -1 : $this->bufferSize);
173+
$data = stream_get_contents($stream, $this->bufferSize);
173174

174175
restore_error_handler();
175176

src/ReadableResourceStream.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ class ReadableResourceStream extends EventEmitter implements ReadableStreamInter
1818
* of bytes read may be lower if the stream resource has less than X bytes
1919
* currently available.
2020
*
21-
* This can be `null` which means read everything available from the
21+
* This can be `-1` which means read everything available from the
2222
* underlying stream resource.
2323
* This should read until the stream resource is not readable anymore
2424
* (i.e. underlying buffer drained), note that this does not neccessarily
2525
* mean it reached EOF.
2626
*
27-
* @var int|null
27+
* @var int
2828
*/
29-
public $bufferSize = 65536;
29+
private $bufferSize;
3030

3131
/**
3232
* @var resource
@@ -36,7 +36,7 @@ class ReadableResourceStream extends EventEmitter implements ReadableStreamInter
3636
private $closed = false;
3737
private $loop;
3838

39-
public function __construct($stream, LoopInterface $loop)
39+
public function __construct($stream, LoopInterface $loop, $readChunkSize = null)
4040
{
4141
if (!is_resource($stream) || get_resource_type($stream) !== "stream") {
4242
throw new InvalidArgumentException('First parameter must be a valid stream resource');
@@ -68,6 +68,7 @@ public function __construct($stream, LoopInterface $loop)
6868

6969
$this->stream = $stream;
7070
$this->loop = $loop;
71+
$this->bufferSize = ($readChunkSize === null) ? 65536 : (int)$readChunkSize;
7172

7273
$this->resume();
7374
}
@@ -123,7 +124,7 @@ public function handleData()
123124
);
124125
});
125126

126-
$data = stream_get_contents($this->stream, $this->bufferSize === null ? -1 : $this->bufferSize);
127+
$data = stream_get_contents($this->stream, $this->bufferSize);
127128

128129
restore_error_handler();
129130

tests/DuplexResourceStreamIntegrationTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,11 @@ public function testBufferReadsLargeChunks($condition, $loopFactory)
3131

3232
list($sockA, $sockB) = stream_socket_pair(STREAM_PF_UNIX, STREAM_SOCK_STREAM, 0);
3333

34-
$streamA = new DuplexResourceStream($sockA, $loop);
35-
$streamB = new DuplexResourceStream($sockB, $loop);
36-
3734
$bufferSize = 4096;
38-
$streamA->bufferSize = $bufferSize;
39-
$streamB->bufferSize = $bufferSize;
35+
$streamA = new DuplexResourceStream($sockA, $loop, $bufferSize);
36+
$streamB = new DuplexResourceStream($sockB, $loop, $bufferSize);
4037

41-
$testString = str_repeat("*", $streamA->bufferSize + 1);
38+
$testString = str_repeat("*", $bufferSize + 1);
4239

4340
$buffer = "";
4441
$streamB->on('data', function ($data) use (&$buffer) {

tests/DuplexResourceStreamTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public function testConstructorAcceptsBuffer()
7070

7171
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
7272

73-
$conn = new DuplexResourceStream($stream, $loop, $buffer);
73+
$conn = new DuplexResourceStream($stream, $loop, null, $buffer);
7474

7575
$this->assertSame($buffer, $conn->getBuffer());
7676
}
@@ -97,7 +97,7 @@ public function testEndShouldEndBuffer()
9797
$buffer = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock();
9898
$buffer->expects($this->once())->method('end')->with('foo');
9999

100-
$conn = new DuplexResourceStream($stream, $loop, $buffer);
100+
$conn = new DuplexResourceStream($stream, $loop, null, $buffer);
101101
$conn->end('foo');
102102
}
103103

@@ -149,7 +149,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
149149

150150
$capturedData = null;
151151

152-
$conn = new DuplexResourceStream($stream, $loop);
152+
$conn = new DuplexResourceStream($stream, $loop, 4321);
153153
$conn->on('data', function ($data) use (&$capturedData) {
154154
$capturedData = $data;
155155
});
@@ -160,7 +160,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
160160
$conn->handleData($stream);
161161

162162
$this->assertTrue($conn->isReadable());
163-
$this->assertEquals($conn->bufferSize, strlen($capturedData));
163+
$this->assertEquals(4321, strlen($capturedData));
164164
}
165165

166166
/**
@@ -174,8 +174,7 @@ public function testDataEventDoesEmitOneChunkUntilStreamEndsWhenBufferSizeIsInfi
174174

175175
$capturedData = null;
176176

177-
$conn = new DuplexResourceStream($stream, $loop);
178-
$conn->bufferSize = null;
177+
$conn = new DuplexResourceStream($stream, $loop, -1);
179178

180179
$conn->on('data', function ($data) use (&$capturedData) {
181180
$capturedData = $data;

tests/ReadableResourceStreamTest.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
120120

121121
$capturedData = null;
122122

123-
$conn = new ReadableResourceStream($stream, $loop);
123+
$conn = new ReadableResourceStream($stream, $loop, 4321);
124124
$conn->on('data', function ($data) use (&$capturedData) {
125125
$capturedData = $data;
126126
});
@@ -131,7 +131,7 @@ public function testDataEventDoesEmitOneChunkMatchingBufferSize()
131131
$conn->handleData($stream);
132132

133133
$this->assertTrue($conn->isReadable());
134-
$this->assertEquals($conn->bufferSize, strlen($capturedData));
134+
$this->assertEquals(4321, strlen($capturedData));
135135
}
136136

137137
/**
@@ -145,8 +145,7 @@ public function testDataEventDoesEmitOneChunkUntilStreamEndsWhenBufferSizeIsInfi
145145

146146
$capturedData = null;
147147

148-
$conn = new ReadableResourceStream($stream, $loop);
149-
$conn->bufferSize = null;
148+
$conn = new ReadableResourceStream($stream, $loop, -1);
150149

151150
$conn->on('data', function ($data) use (&$capturedData) {
152151
$capturedData = $data;

0 commit comments

Comments
 (0)