From e99b5563d69f0e7b0bbc21a938465f89115c24c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 27 Apr 2017 08:58:40 +0200 Subject: [PATCH] Remove public $stream property from all resource streams --- README.md | 18 ------------------ examples/benchmark-throughput.php | 7 ++++--- src/DuplexResourceStream.php | 2 +- src/ReadableResourceStream.php | 2 +- src/WritableResourceStream.php | 8 +++----- tests/WritableStreamResourceTest.php | 26 -------------------------- 6 files changed, 9 insertions(+), 54 deletions(-) diff --git a/README.md b/README.md index 4fddecb..9788581 100644 --- a/README.md +++ b/README.md @@ -753,12 +753,6 @@ Once the constructor is called with a valid stream resource, this class will take care of the underlying stream resource. You SHOULD only use its public API and SHOULD NOT interfere with the underlying stream resource manually. -Should you need to access the underlying stream resource, you can use the public -`$stream` property like this: - -```php -var_dump(stream_get_meta_data($stream->stream)); -``` The `$bufferSize` property controls the maximum buffer size in bytes to read at once from the stream. @@ -819,12 +813,6 @@ Once the constructor is called with a valid stream resource, this class will take care of the underlying stream resource. You SHOULD only use its public API and SHOULD NOT interfere with the underlying stream resource manually. -Should you need to access the underlying stream resource, you can use the public -`$stream` property like this: - -```php -var_dump(stream_get_meta_data($stream->stream)); -``` Any `write()` calls to this class will not be performaned instantly, but will be performaned asynchronously, once the EventLoop reports the stream resource is @@ -885,12 +873,6 @@ Once the constructor is called with a valid stream resource, this class will take care of the underlying stream resource. You SHOULD only use its public API and SHOULD NOT interfere with the underlying stream resource manually. -Should you need to access the underlying stream resource, you can use the public -`$stream` property like this: - -```php -var_dump(stream_get_meta_data($stream->stream)); -``` The `$bufferSize` property controls the maximum buffer size in bytes to read at once from the stream. diff --git a/examples/benchmark-throughput.php b/examples/benchmark-throughput.php index 2d8c143..caae282 100644 --- a/examples/benchmark-throughput.php +++ b/examples/benchmark-throughput.php @@ -21,7 +21,8 @@ $info->write('piping from ' . $if . ' to ' . $of . ' (for max ' . $t . ' second(s)) ...'. PHP_EOL); // setup input and output streams and pipe inbetween -$in = new React\Stream\ReadableResourceStream(fopen($if, 'r'), $loop); +$fh = fopen($if, 'r'); +$in = new React\Stream\ReadableResourceStream($fh, $loop); $out = new React\Stream\WritableResourceStream(fopen($of, 'w'), $loop); $in->pipe($out); @@ -32,11 +33,11 @@ }); // print stream position once stream closes -$in->on('close', function () use ($in, $start, $timeout, $info) { +$in->on('close', function () use ($fh, $start, $timeout, $info) { $t = microtime(true) - $start; $timeout->cancel(); - $bytes = ftell($in->stream); + $bytes = ftell($fh); $info->write('read ' . $bytes . ' byte(s) in ' . round($t, 3) . ' second(s) => ' . round($bytes / 1024 / 1024 / $t, 1) . ' MiB/s' . PHP_EOL); $info->write('peak memory usage of ' . round(memory_get_peak_usage(true) / 1024 / 1024, 1) . ' MiB' . PHP_EOL); diff --git a/src/DuplexResourceStream.php b/src/DuplexResourceStream.php index 88dba7c..5258793 100644 --- a/src/DuplexResourceStream.php +++ b/src/DuplexResourceStream.php @@ -26,7 +26,7 @@ class DuplexResourceStream extends EventEmitter implements DuplexStreamInterface */ public $bufferSize = 65536; - public $stream; + private $stream; protected $readable = true; protected $writable = true; protected $closing = false; diff --git a/src/ReadableResourceStream.php b/src/ReadableResourceStream.php index 5652ff2..6be9572 100644 --- a/src/ReadableResourceStream.php +++ b/src/ReadableResourceStream.php @@ -31,7 +31,7 @@ class ReadableResourceStream extends EventEmitter implements ReadableStreamInter /** * @var resource */ - public $stream; + private $stream; private $closed = false; private $loop; diff --git a/src/WritableResourceStream.php b/src/WritableResourceStream.php index 46e114b..2140232 100644 --- a/src/WritableResourceStream.php +++ b/src/WritableResourceStream.php @@ -7,7 +7,7 @@ class WritableResourceStream extends EventEmitter implements WritableStreamInterface { - public $stream; + private $stream; public $softLimit = 65536; private $listening = false; @@ -118,9 +118,7 @@ public function handleWrite() // Should this turn out to be a permanent error later, it will eventually // send *nothing* and we can detect this. if ($sent === 0 || $sent === false) { - if ($error === null) { - $error = new \RuntimeException('Send failed'); - } else { + if ($error !== null) { $error = new \ErrorException( $error['message'], 0, @@ -130,7 +128,7 @@ public function handleWrite() ); } - $this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error->getMessage(), 0, $error))); + $this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . ($error !== null ? $error->getMessage() : 'Unknown error'), 0, $error))); $this->close(); return; diff --git a/tests/WritableStreamResourceTest.php b/tests/WritableStreamResourceTest.php index f6823eb..606a684 100644 --- a/tests/WritableStreamResourceTest.php +++ b/tests/WritableStreamResourceTest.php @@ -138,32 +138,6 @@ public function testWriteReturnsFalseWhenWritableResourceStreamIsExactlyFull() $this->assertFalse($buffer->write("foo")); } - /** - * @covers React\Stream\WritableResourceStream::write - * @covers React\Stream\WritableResourceStream::handleWrite - */ - public function testWriteEmitsErrorWhenResourceIsNotWritable() - { - if (defined('HHVM_VERSION')) { - // via https://github.com/reactphp/stream/pull/52/files#r75493076 - $this->markTestSkipped('HHVM allows writing to read-only memory streams'); - } - - $stream = fopen('php://temp', 'r+'); - $loop = $this->createLoopMock(); - - $buffer = new WritableResourceStream($stream, $loop); - - // nasty hack to replace with reaad-only stream resource - $buffer->stream = fopen('php://temp', 'r'); - - $buffer->on('error', $this->expectCallableOnce()); - //$buffer->on('close', $this->expectCallableOnce()); - - $buffer->write('hello'); - $buffer->handleWrite(); - } - /** * @covers React\Stream\WritableResourceStream::write * @covers React\Stream\WritableResourceStream::handleWrite