| 1 |
<?php |
| 2 |
|
| 3 |
namespace Clue\React\Buzz\Message; |
| 4 |
|
| 5 |
use Evenement\EventEmitter; |
| 6 |
use Psr\Http\Message\StreamInterface; |
| 7 |
use React\Stream\ReadableStreamInterface; |
| 8 |
use React\Stream\Util; |
| 9 |
use React\Stream\WritableStreamInterface; |
| 10 |
|
| 11 |
/** |
| 12 |
* @internal |
| 13 |
*/ |
| 14 |
class ReadableBodyStream extends EventEmitter implements ReadableStreamInterface, StreamInterface |
| 15 |
{ |
| 16 |
private $input; |
| 17 |
private $closed = false; |
| 18 |
|
| 19 |
public function __construct(ReadableStreamInterface $input) |
| 20 |
{ |
| 21 |
$this->input = $input; |
| 22 |
|
| 23 |
$that = $this; |
| 24 |
$input->on('data', function ($data) use ($that) { |
| 25 |
$that->emit('data', array($data)); |
| 26 |
}); |
| 27 |
$input->on('error', function ($error) use ($that) { |
| 28 |
$that->emit('error', array($error)); |
| 29 |
$that->close(); |
| 30 |
}); |
| 31 |
$input->on('end', function () use ($that) { |
| 32 |
$that->emit('end'); |
| 33 |
$that->close(); |
| 34 |
}); |
| 35 |
$input->on('close', array($that, 'close')); |
| 36 |
} |
| 37 |
|
| 38 |
public function close() |
| 39 |
{ |
| 40 |
if (!$this->closed) { |
| 41 |
$this->closed = true; |
| 42 |
$this->input->close(); |
| 43 |
|
| 44 |
$this->emit('close'); |
| 45 |
$this->removeAllListeners(); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public function isReadable() |
| 50 |
{ |
| 51 |
return $this->input->isReadable(); |
| 52 |
} |
| 53 |
|
| 54 |
public function pause() |
| 55 |
{ |
| 56 |
$this->input->pause(); |
| 57 |
} |
| 58 |
|
| 59 |
public function resume() |
| 60 |
{ |
| 61 |
$this->input->resume(); |
| 62 |
} |
| 63 |
|
| 64 |
public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 65 |
{ |
| 66 |
Util::pipe($this, $dest, $options); |
| 67 |
|
| 68 |
return $dest; |
| 69 |
} |
| 70 |
|
| 71 |
public function eof() |
| 72 |
{ |
| 73 |
return !$this->isReadable(); |
| 74 |
} |
| 75 |
|
| 76 |
public function __toString() |
| 77 |
{ |
| 78 |
return ''; |
| 79 |
} |
| 80 |
|
| 81 |
public function detach() |
| 82 |
{ |
| 83 |
throw new \BadMethodCallException(); |
| 84 |
} |
| 85 |
|
| 86 |
public function getSize() |
| 87 |
{ |
| 88 |
return null; |
| 89 |
} |
| 90 |
|
| 91 |
public function tell() |
| 92 |
{ |
| 93 |
throw new \BadMethodCallException(); |
| 94 |
} |
| 95 |
|
| 96 |
public function isSeekable() |
| 97 |
{ |
| 98 |
return false; |
| 99 |
} |
| 100 |
|
| 101 |
public function seek($offset, $whence = SEEK_SET) |
| 102 |
{ |
| 103 |
throw new \BadMethodCallException(); |
| 104 |
} |
| 105 |
|
| 106 |
public function rewind() |
| 107 |
{ |
| 108 |
throw new \BadMethodCallException(); |
| 109 |
} |
| 110 |
|
| 111 |
public function isWritable() |
| 112 |
{ |
| 113 |
return false; |
| 114 |
} |
| 115 |
|
| 116 |
public function write($string) |
| 117 |
{ |
| 118 |
throw new \BadMethodCallException(); |
| 119 |
} |
| 120 |
|
| 121 |
public function read($length) |
| 122 |
{ |
| 123 |
throw new \BadMethodCallException(); |
| 124 |
} |
| 125 |
|
| 126 |
public function getContents() |
| 127 |
{ |
| 128 |
throw new \BadMethodCallException(); |
| 129 |
} |
| 130 |
|
| 131 |
public function getMetadata($key = null) |
| 132 |
{ |
| 133 |
return ($key === null) ? array() : null; |
| 134 |
} |
| 135 |
} |