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\PromiseInterface;
8 use React\Stream\ReadableStreamInterface;
9 use React\Stream\ThroughStream;
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<ReadableStreamInterface> $promise
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 // Forward exception as error event if not already closed
80 if (!$closed) {
81 $out->emit('error', array($e, $out));
82 $out->close();
83 }
84
85 // Both resume() and pause() may attach to this promise, so
86 // return a NOOP stream instance here.
87 $stream = new ThroughStream();
88 $stream->close();
89 return $stream;
90 }
91 );
92 }
93
94 public function isReadable()
95 {
96 return !$this->closed;
97 }
98
99 public function pause()
100 {
101 if ($this->promise !== null) {
102 $this->promise->then(function (ReadableStreamInterface $stream) {
103 $stream->pause();
104 });
105 }
106 }
107
108 public function resume()
109 {
110 if ($this->promise !== null) {
111 $this->promise->then(function (ReadableStreamInterface $stream) {
112 $stream->resume();
113 });
114 }
115 }
116
117 public function pipe(WritableStreamInterface $dest, array $options = array())
118 {
119 Util::pipe($this, $dest, $options);
120
121 return $dest;
122 }
123
124 public function close()
125 {
126 if ($this->closed) {
127 return;
128 }
129
130 $this->closed = true;
131
132 // try to cancel promise once the stream closes
133 if ($this->promise !== null && \method_exists($this->promise, 'cancel')) {
134 $this->promise->cancel();
135 }
136 $this->promise = null;
137
138 $this->emit('close');
139 $this->removeAllListeners();
140 }
141 }