Debian Codegraph @

vendor/react/promise-stream/src/UnwrapReadableStream.php

raw
1 <?php
2
3 namespace React\Promise\Stream;
4
5 use Evenement\EventEmitter;
6 use InvalidArgumentException;
7 use React\Promise\CancellablePromiseInterface;
8 use React\Promise\PromiseInterface;
9 use React\Stream\ReadableStreamInterface;
10 use React\Stream\Util;
11 use React\Stream\WritableStreamInterface;
12
13 /**
14 * @internal
15 * @see unwrapReadable() instead
16 */
17 class UnwrapReadableStream extends EventEmitter implements ReadableStreamInterface
18 {
19 private $promise;
20 private $closed = false;
21
22 /**
23 * Instantiate new unwrapped readable stream for given `Promise` which resolves with a `ReadableStreamInterface`.
24 *
25 * @param PromiseInterface $promise Promise<ReadableStreamInterface, Exception>
26 */
27 public function __construct(PromiseInterface $promise)
28 {
29 $out = $this;
30 $closed =& $this->closed;
31
32 $this->promise = $promise->then(
33 function ($stream) {
34 if (!$stream instanceof ReadableStreamInterface) {
35 throw new InvalidArgumentException('Not a readable stream');
36 }
37 return $stream;
38 }
39 )->then(
40 function (ReadableStreamInterface $stream) use ($out, &$closed) {
41 // stream is already closed, make sure to close output stream
42 if (!$stream->isReadable()) {
43 $out->close();
44 return $stream;
45 }
46
47 // resolves but output is already closed, make sure to close stream silently
48 if ($closed) {
49 $stream->close();
50 return $stream;
51 }
52
53 // stream any writes into output stream
54 $stream->on('data', function ($data) use ($out) {
55 $out->emit('data', array($data, $out));
56 });
57
58 // forward end events and close
59 $stream->on('end', function () use ($out, &$closed) {
60 if (!$closed) {
61 $out->emit('end', array($out));
62 $out->close();
63 }
64 });
65
66 // error events cancel output stream
67 $stream->on('error', function ($error) use ($out) {
68 $out->emit('error', array($error, $out));
69 $out->close();
70 });
71
72 // close both streams once either side closes
73 $stream->on('close', array($out, 'close'));
74 $out->on('close', array($stream, 'close'));
75
76 return $stream;
77 },
78 function ($e) use ($out, &$closed) {
79 if (!$closed) {
80 $out->emit('error', array($e, $out));
81 $out->close();
82 }
83
84 // resume() and pause() may attach to this promise, so ensure we actually reject here
85 throw $e;
86 }
87 );
88 }
89
90 public function isReadable()
91 {
92 return !$this->closed;
93 }
94
95 public function pause()
96 {
97 if ($this->promise !== null) {
98 $this->promise->then(function (ReadableStreamInterface $stream) {
99 $stream->pause();
100 });
101 }
102 }
103
104 public function resume()
105 {
106 if ($this->promise !== null) {
107 $this->promise->then(function (ReadableStreamInterface $stream) {
108 $stream->resume();
109 });
110 }
111 }
112
113 public function pipe(WritableStreamInterface $dest, array $options = array())
114 {
115 Util::pipe($this, $dest, $options);
116
117 return $dest;
118 }
119
120 public function close()
121 {
122 if ($this->closed) {
123 return;
124 }
125
126 $this->closed = true;
127
128 // try to cancel promise once the stream closes
129 if ($this->promise instanceof CancellablePromiseInterface) {
130 $this->promise->cancel();
131 }
132 $this->promise = null;
133
134 $this->emit('close');
135 $this->removeAllListeners();
136 }
137 }