Skip to content

Commit 78ee694

Browse files
authored
Merge pull request #62 from clue-labs/inject-buffer
The Buffer can now be injected into the Stream (or be used standalone)
2 parents 12859f7 + 23d51f0 commit 78ee694

2 files changed

Lines changed: 27 additions & 6 deletions

File tree

src/Stream.php

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class Stream extends EventEmitter implements DuplexStreamInterface
3333
protected $loop;
3434
protected $buffer;
3535

36-
public function __construct($stream, LoopInterface $loop)
36+
public function __construct($stream, LoopInterface $loop, WritableStreamInterface $buffer = null)
3737
{
3838
$this->stream = $stream;
3939
if (!is_resource($this->stream) || get_resource_type($this->stream) !== "stream") {
@@ -52,16 +52,21 @@ public function __construct($stream, LoopInterface $loop)
5252
stream_set_read_buffer($this->stream, 0);
5353
}
5454

55+
if ($buffer === null) {
56+
$buffer = new Buffer($stream, $loop);
57+
}
58+
5559
$this->loop = $loop;
56-
$this->buffer = new Buffer($this->stream, $this->loop);
60+
$this->buffer = $buffer;
5761

5862
$that = $this;
5963

6064
$this->buffer->on('error', function ($error) use ($that) {
6165
$that->emit('error', array($error, $that));
62-
$that->close();
6366
});
6467

68+
$this->buffer->on('close', array($this, 'close'));
69+
6570
$this->buffer->on('drain', function () use ($that) {
6671
$that->emit('drain', array($that));
6772
});
@@ -114,7 +119,7 @@ public function close()
114119
$this->emit('end', array($this));
115120
$this->emit('close', array($this));
116121
$this->loop->removeStream($this->stream);
117-
$this->buffer->removeAllListeners();
122+
$this->buffer->close();
118123
$this->removeAllListeners();
119124

120125
$this->handleClose();
@@ -131,8 +136,6 @@ public function end($data = null)
131136
$this->readable = false;
132137
$this->writable = false;
133138

134-
$this->buffer->on('close', array($this, 'close'));
135-
136139
$this->buffer->end($data);
137140
}
138141

@@ -182,6 +185,9 @@ public function handleClose()
182185
}
183186
}
184187

188+
/**
189+
* @return WritableStreamInterface|Buffer
190+
*/
185191
public function getBuffer()
186192
{
187193
return $this->buffer;

tests/StreamTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,21 @@ public function testConstructorThrowsExceptionOnInvalidStream()
2828
$conn = new Stream('breakme', $loop);
2929
}
3030

31+
/**
32+
* @covers React\Stream\Stream::__construct
33+
*/
34+
public function testConstructorAcceptsBuffer()
35+
{
36+
$stream = fopen('php://temp', 'r+');
37+
$loop = $this->createLoopMock();
38+
39+
$buffer = $this->getMock('React\Stream\WritableStreamInterface');
40+
41+
$conn = new Stream($stream, $loop, $buffer);
42+
43+
$this->assertSame($buffer, $conn->getBuffer());
44+
}
45+
3146
/**
3247
* @covers React\Stream\Stream::__construct
3348
* @covers React\Stream\Stream::handleData

0 commit comments

Comments
 (0)