@@ -35,6 +35,7 @@ descriptor based implementation with an in-memory write buffer.
3535 * [ close()] ( #close-1 )
3636 * [ DuplexStreamInterface] ( #duplexstreaminterface )
3737 * [ ReadableResourceStream] ( #readableresourcestream )
38+ * [ WritableResourceStream] ( #writableresourcestream )
3839* [ Usage] ( #usage )
3940* [ Install] ( #install )
4041* [ Tests] ( #tests )
@@ -761,12 +762,73 @@ mean it reached EOF.
761762$stream->bufferSize = 8192;
762763```
763764
765+ ### WritableResourceStream
766+
767+ The ` WritableResourceStream ` is a concrete implementation of the
768+ [ ` WritableStreamInterface ` ] ( #writablestreaminterface ) for PHP's stream resources.
769+
770+ This can be used to represent a write-only resource like a file stream opened in
771+ writable mode or a stream such as ` STDOUT ` or ` STDERR ` :
772+
773+ ``` php
774+ $stream = new WritableResourceStream(STDOUT, $loop);
775+ $stream->write('hello!');
776+ $stream->end();
777+ ```
778+
779+ See also [ ` WritableStreamInterface ` ] ( #writablestreaminterface ) for more details.
780+
781+ The first parameter given to the constructor MUST be a valid stream resource
782+ that is opened for writing.
783+ Otherwise, it will throw an ` InvalidArgumentException ` :
784+
785+ ``` php
786+ // throws InvalidArgumentException
787+ $stream = new WritableResourceStream(false, $loop);
788+ ```
789+
790+ Internally, this class tries to enable non-blocking mode on the stream resource
791+ which may not be supported for all stream resources.
792+ Most notably, this is not supported by pipes on Windows (STDOUT, STDERR etc.).
793+ If this fails, it will throw a ` RuntimeException ` :
794+
795+ ``` php
796+ // throws RuntimeException on Windows
797+ $stream = new WritableResourceStream(STDOUT, $loop);
798+ ```
799+
800+ Once the constructor is called with a valid stream resource, this class will
801+ take care of the underlying stream resource.
802+ You SHOULD only use its public API and SHOULD NOT interfere with the underlying
803+ stream resource manually.
804+ Should you need to access the underlying stream resource, you can use the public
805+ ` $stream ` property like this:
806+
807+ ``` php
808+ var_dump(stream_get_meta_data($stream->stream));
809+ ```
810+
811+ Any ` write() ` calls to this class will not be performaned instantly, but will
812+ be performaned asynchronously, once the EventLoop reports the stream resource is
813+ ready to accept data.
814+ For this, it uses an in-memory buffer string to collect all outstanding writes.
815+ This buffer has a soft-limit applied which defines how much data it is willing
816+ to accept before the caller SHOULD stop sending further data.
817+ It currently defaults to 64 KiB and can be controlled through the public
818+ ` $softLimit ` property like this:
819+
820+ ``` php
821+ $stream->softLimit = 8192;
822+ ```
823+
824+ See also [ ` write() ` ] ( #write ) for more details.
825+
764826## Usage
765827``` php
766828 $loop = React\EventLoop\Factory::create();
767829
768830 $source = new React\Stream\ReadableResourceStream(fopen('omg.txt', 'r'), $loop);
769- $dest = new React\Stream\Stream (fopen('wtf.txt', 'w'), $loop);
831+ $dest = new React\Stream\WritableResourceStream (fopen('wtf.txt', 'w'), $loop);
770832
771833 $source->pipe($dest);
772834
0 commit comments