Debian Codegraph @

vendor/react/promise-stream/src/UnwrapWritableStream.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\WritableStreamInterface;
9
10 /**
11 * @internal
12 * @see unwrapWritable() instead
13 */
14 class UnwrapWritableStream extends EventEmitter implements WritableStreamInterface
15 {
16 private $promise;
17 private $stream;
18 private $buffer = array();
19 private $closed = false;
20 private $ending = false;
21
22 /**
23 * Instantiate new unwrapped writable stream for given `Promise` which resolves with a `WritableStreamInterface`.
24 *
25 * @param PromiseInterface<WritableStreamInterface> $promise
26 */
27 public function __construct(PromiseInterface $promise)
28 {
29 $out = $this;
30 $store =& $this->stream;
31 $buffer =& $this->buffer;
32 $ending =& $this->ending;
33 $closed =& $this->closed;
34
35 $this->promise = $promise->then(
36 function ($stream) {
37 if (!$stream instanceof WritableStreamInterface) {
38 throw new InvalidArgumentException('Not a writable stream');
39 }
40 return $stream;
41 }
42 )->then(
43 function (WritableStreamInterface $stream) use ($out, &$store, &$buffer, &$ending, &$closed) {
44 // stream is already closed, make sure to close output stream
45 if (!$stream->isWritable()) {
46 $out->close();
47 return $stream;
48 }
49
50 // resolves but output is already closed, make sure to close stream silently
51 if ($closed) {
52 $stream->close();
53 return $stream;
54 }
55
56 // forward drain events for back pressure
57 $stream->on('drain', function () use ($out) {
58 $out->emit('drain', array($out));
59 });
60
61 // error events cancel output stream
62 $stream->on('error', function ($error) use ($out) {
63 $out->emit('error', array($error, $out));
64 $out->close();
65 });
66
67 // close both streams once either side closes
68 $stream->on('close', array($out, 'close'));
69 $out->on('close', array($stream, 'close'));
70
71 if ($buffer) {
72 // flush buffer to stream and check if its buffer is not exceeded
73 $drained = true;
74 foreach ($buffer as $chunk) {
75 if (!$stream->write($chunk)) {
76 $drained = false;
77 }
78 }
79 $buffer = array();
80
81 if ($drained) {
82 // signal drain event, because the output stream previous signalled a full buffer
83 $out->emit('drain', array($out));
84 }
85 }
86
87 if ($ending) {
88 $stream->end();
89 } else {
90 $store = $stream;
91 }
92
93 return $stream;
94 },
95 function ($e) use ($out, &$closed) {
96 if (!$closed) {
97 $out->emit('error', array($e, $out));
98 $out->close();
99 }
100 }
101 );
102 }
103
104 public function write($data)
105 {
106 if ($this->ending) {
107 return false;
108 }
109
110 // forward to inner stream if possible
111 if ($this->stream !== null) {
112 return $this->stream->write($data);
113 }
114
115 // append to buffer and signal the buffer is full
116 $this->buffer[] = $data;
117 return false;
118 }
119
120 public function end($data = null)
121 {
122 if ($this->ending) {
123 return;
124 }
125
126 $this->ending = true;
127
128 // forward to inner stream if possible
129 if ($this->stream !== null) {
130 return $this->stream->end($data);
131 }
132
133 // append to buffer
134 if ($data !== null) {
135 $this->buffer[] = $data;
136 }
137 }
138
139 public function isWritable()
140 {
141 return !$this->ending;
142 }
143
144 public function close()
145 {
146 if ($this->closed) {
147 return;
148 }
149
150 $this->buffer = array();
151 $this->ending = true;
152 $this->closed = true;
153
154 // try to cancel promise once the stream closes
155 if ($this->promise !== null && \method_exists($this->promise, 'cancel')) {
156 $this->promise->cancel();
157 }
158 $this->promise = $this->stream = null;
159
160 $this->emit('close');
161 $this->removeAllListeners();
162 }
163 }