|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Promise\Stream; |
| 4 | + |
| 5 | +use Evenement\EventEmitter; |
| 6 | +use React\Stream\ReadableStreamInterface; |
| 7 | +use React\Promise\PromiseInterface; |
| 8 | +use React\Stream\WritableStreamInterface; |
| 9 | +use React\Stream\Util; |
| 10 | +use React\Promise\CancellablePromiseInterface; |
| 11 | + |
| 12 | +/** |
| 13 | + * @internal |
| 14 | + * @see unwrapReadable() instead |
| 15 | + */ |
| 16 | +class UnwrapReadableStream extends EventEmitter implements ReadableStreamInterface |
| 17 | +{ |
| 18 | + private $promise; |
| 19 | + private $closed = false; |
| 20 | + |
| 21 | + /** |
| 22 | + * unwrap a `Promise` which resolves with a `ReadableStreamInterface`. |
| 23 | + * |
| 24 | + * @param PromiseInterface $promise Promise<ReadableStreamInterface, Exception> |
| 25 | + * @return ReadableStreamInterface |
| 26 | + */ |
| 27 | + public function __construct(PromiseInterface $promise) |
| 28 | + { |
| 29 | + $out = $this; |
| 30 | + |
| 31 | + // TODO: support backpressure |
| 32 | + |
| 33 | + // try to cancel promise once the stream closes |
| 34 | + if ($promise instanceof CancellablePromiseInterface) { |
| 35 | + $out->on('close', function() use ($promise) { |
| 36 | + $promise->cancel(); |
| 37 | + }); |
| 38 | + } |
| 39 | + |
| 40 | + $this->promise = $promise->then( |
| 41 | + function ($stream) { |
| 42 | + if (!($stream instanceof ReadableStreamInterface)) { |
| 43 | + throw new \InvalidArgumentException('Not a readable stream'); |
| 44 | + } |
| 45 | + return $stream; |
| 46 | + } |
| 47 | + )->then( |
| 48 | + function (ReadableStreamInterface $stream) use ($out) { |
| 49 | + if (!$stream->isReadable()) { |
| 50 | + $out->close(); |
| 51 | + return $stream; |
| 52 | + } |
| 53 | + |
| 54 | + // stream any writes into output stream |
| 55 | + $stream->on('data', function ($data) use ($out) { |
| 56 | + $out->emit('data', array($data, $out)); |
| 57 | + }); |
| 58 | + |
| 59 | + // error events cancel output stream |
| 60 | + $stream->on('error', function ($error) use ($out) { |
| 61 | + $out->emit('error', array($error, $out)); |
| 62 | + $out->close(); |
| 63 | + }); |
| 64 | + |
| 65 | + // close output stream once body closes |
| 66 | + $stream->on('close', function () use ($out) { |
| 67 | + $out->close(); |
| 68 | + }); |
| 69 | + $stream->on('end', function () use ($out) { |
| 70 | + $out->close(); |
| 71 | + }); |
| 72 | + return $stream; |
| 73 | + }, |
| 74 | + function ($e) use ($out) { |
| 75 | + $out->emit('error', array($e, $out)); |
| 76 | + $out->close(); |
| 77 | + } |
| 78 | + ); |
| 79 | + } |
| 80 | + |
| 81 | + public function isReadable() |
| 82 | + { |
| 83 | + return !$this->closed; |
| 84 | + } |
| 85 | + |
| 86 | + public function pause() |
| 87 | + { |
| 88 | + } |
| 89 | + |
| 90 | + public function resume() |
| 91 | + { |
| 92 | + } |
| 93 | + |
| 94 | + public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 95 | + { |
| 96 | + Util::pipe($this, $dest, $options); |
| 97 | + |
| 98 | + return $dest; |
| 99 | + } |
| 100 | + |
| 101 | + public function close() |
| 102 | + { |
| 103 | + if ($this->closed) { |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + $this->closed = true; |
| 108 | + |
| 109 | + $this->emit('end', array($this)); |
| 110 | + $this->emit('close', array($this)); |
| 111 | + } |
| 112 | +} |
0 commit comments