Skip to content

Commit 585af73

Browse files
authored
Merge pull request #90 from clue-labs/stream-property
Remove public $stream property from all resource streams
2 parents eb90121 + e99b556 commit 585af73

6 files changed

Lines changed: 9 additions & 54 deletions

File tree

README.md

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -752,12 +752,6 @@ Once the constructor is called with a valid stream resource, this class will
752752
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.
755-
Should you need to access the underlying stream resource, you can use the public
756-
`$stream` property like this:
757-
758-
```php
759-
var_dump(stream_get_meta_data($stream->stream));
760-
```
761755

762756
The `$bufferSize` property controls the maximum buffer size in bytes to read
763757
at once from the stream.
@@ -818,12 +812,6 @@ Once the constructor is called with a valid stream resource, this class will
818812
take care of the underlying stream resource.
819813
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
820814
stream resource manually.
821-
Should you need to access the underlying stream resource, you can use the public
822-
`$stream` property like this:
823-
824-
```php
825-
var_dump(stream_get_meta_data($stream->stream));
826-
```
827815

828816
Any `write()` calls to this class will not be performaned instantly, but will
829817
be performaned asynchronously, once the EventLoop reports the stream resource is
@@ -884,12 +872,6 @@ Once the constructor is called with a valid stream resource, this class will
884872
take care of the underlying stream resource.
885873
You SHOULD only use its public API and SHOULD NOT interfere with the underlying
886874
stream resource manually.
887-
Should you need to access the underlying stream resource, you can use the public
888-
`$stream` property like this:
889-
890-
```php
891-
var_dump(stream_get_meta_data($stream->stream));
892-
```
893875

894876
The `$bufferSize` property controls the maximum buffer size in bytes to read
895877
at once from the stream.

examples/benchmark-throughput.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@
2121
$info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...'. PHP_EOL);
2222

2323
// setup input and output streams and pipe inbetween
24-
$in = new React\Stream\ReadableResourceStream(fopen($if, 'r'), $loop);
24+
$fh = fopen($if, 'r');
25+
$in = new React\Stream\ReadableResourceStream($fh, $loop);
2526
$out = new React\Stream\WritableResourceStream(fopen($of, 'w'), $loop);
2627
$in->pipe($out);
2728

@@ -32,11 +33,11 @@
3233
});
3334

3435
// print stream position once stream closes
35-
$in->on('close', function () use ($in, $start, $timeout, $info) {
36+
$in->on('close', function () use ($fh, $start, $timeout, $info) {
3637
$t = microtime(true) - $start;
3738
$timeout->cancel();
3839

39-
$bytes = ftell($in->stream);
40+
$bytes = ftell($fh);
4041

4142
$info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL);
4243
$info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL);

src/DuplexResourceStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface
2626
*/
2727
public $bufferSize = 65536;
2828

29-
public $stream;
29+
private $stream;
3030
protected $readable = true;
3131
protected $writable = true;
3232
protected $closing = false;

src/ReadableResourceStream.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class ReadableResourceStream extends EventEmitter implements ReadableStreamInter
3131
/**
3232
* @var resource
3333
*/
34-
public $stream;
34+
private $stream;
3535

3636
private $closed = false;
3737
private $loop;

src/WritableResourceStream.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class WritableResourceStream extends EventEmitter implements WritableStreamInterface
99
{
10-
public $stream;
10+
private $stream;
1111
public $softLimit = 65536;
1212

1313
private $listening = false;
@@ -118,9 +118,7 @@ public function handleWrite()
118118
// Should this turn out to be a permanent error later, it will eventually
119119
// send *nothing* and we can detect this.
120120
if ($sent === 0 || $sent === false) {
121-
if ($error === null) {
122-
$error = new \RuntimeException('Send failed');
123-
} else {
121+
if ($error !== null) {
124122
$error = new \ErrorException(
125123
$error['message'],
126124
0,
@@ -130,7 +128,7 @@ public function handleWrite()
130128
);
131129
}
132130

133-
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error->getMessage(), 0, $error)));
131+
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . ($error !== null ? $error->getMessage() : 'Unknown error'), 0, $error)));
134132
$this->close();
135133

136134
return;

tests/WritableStreamResourceTest.php

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -138,32 +138,6 @@ public function testWriteReturnsFalseWhenWritableResourceStreamIsExactlyFull()
138138
$this->assertFalse($buffer->write("foo"));
139139
}
140140

141-
/**
142-
* @covers React\Stream\WritableResourceStream::write
143-
* @covers React\Stream\WritableResourceStream::handleWrite
144-
*/
145-
public function testWriteEmitsErrorWhenResourceIsNotWritable()
146-
{
147-
if (defined('HHVM_VERSION')) {
148-
// via https://github.com/reactphp/stream/pull/52/files#r75493076
149-
$this->markTestSkipped('HHVM allows writing to read-only memory streams');
150-
}
151-
152-
$stream = fopen('php://temp', 'r+');
153-
$loop = $this->createLoopMock();
154-
155-
$buffer = new WritableResourceStream($stream, $loop);
156-
157-
// nasty hack to replace with reaad-only stream resource
158-
$buffer->stream = fopen('php://temp', 'r');
159-
160-
$buffer->on('error', $this->expectCallableOnce());
161-
//$buffer->on('close', $this->expectCallableOnce());
162-
163-
$buffer->write('hello');
164-
$buffer->handleWrite();
165-
}
166-
167141
/**
168142
* @covers React\Stream\WritableResourceStream::write
169143
* @covers React\Stream\WritableResourceStream::handleWrite

0 commit comments

Comments
 (0)