Skip to content

Commit

Permalink
Inject Buffer into Stream
Browse files Browse the repository at this point in the history
  • Loading branch information
clue committed Nov 13, 2016
1 parent d475b9e commit 8cc22ba
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Stream extends EventEmitter implements DuplexStreamInterface
protected $loop;
protected $buffer;

public function __construct($stream, LoopInterface $loop)
public function __construct($stream, LoopInterface $loop, WritableStreamInterface $buffer = null)
{
$this->stream = $stream;
if (!is_resource($this->stream) || get_resource_type($this->stream) !== "stream") {
Expand All @@ -52,8 +52,12 @@ public function __construct($stream, LoopInterface $loop)
stream_set_read_buffer($this->stream, 0);
}

if ($buffer === null) {
$buffer = new Buffer($stream, $loop);
}

$this->loop = $loop;
$this->buffer = new Buffer($this->stream, $this->loop);
$this->buffer = $buffer;

$that = $this;

Expand Down Expand Up @@ -182,6 +186,9 @@ public function handleClose()
}
}

/**
* @return WritableStreamInterface|Buffer
*/
public function getBuffer()
{
return $this->buffer;
Expand Down
15 changes: 15 additions & 0 deletions tests/StreamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,21 @@ public function testConstructorThrowsExceptionOnInvalidStream()
$conn = new Stream('breakme', $loop);
}

/**
* @covers React\Stream\Stream::__construct
*/
public function testConstructorAcceptsBuffer()
{
$stream = fopen('php://temp', 'r+');
$loop = $this->createLoopMock();

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

$conn = new Stream($stream, $loop, $buffer);

$this->assertSame($buffer, $conn->getBuffer());
}

/**
* @covers React\Stream\Stream::__construct
* @covers React\Stream\Stream::handleData
Expand Down

0 comments on commit 8cc22ba

Please sign in to comment.