|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Zlib; |
| 4 | + |
| 5 | +use React\Stream\DuplexStreamInterface; |
| 6 | +use Evenement\EventEmitter; |
| 7 | +use React\Stream\WritableStreamInterface; |
| 8 | +use React\Stream\Util; |
| 9 | +use Exception; |
| 10 | + |
| 11 | +/** |
| 12 | + * @internal Should not be relied upon outside of this package. Should eventually be moved to react/stream? |
| 13 | + */ |
| 14 | +class TransformStream extends EventEmitter implements DuplexStreamInterface |
| 15 | +{ |
| 16 | + private $readable = true; |
| 17 | + private $writable = true; |
| 18 | + private $closed = false; |
| 19 | + |
| 20 | + public function write($data) |
| 21 | + { |
| 22 | + if (!$this->writable || $data === '') { |
| 23 | + return; |
| 24 | + } |
| 25 | + |
| 26 | + try { |
| 27 | + $this->transformData($data); |
| 28 | + } catch (Exception $e) { |
| 29 | + $this->forwardError($e); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public function end($data = null) |
| 34 | + { |
| 35 | + if (!$this->writable) { |
| 36 | + return; |
| 37 | + } |
| 38 | + $this->writable = false; |
| 39 | + |
| 40 | + try { |
| 41 | + if ($data === null) { |
| 42 | + $data = ''; |
| 43 | + } |
| 44 | + $this->transformEnd($data); |
| 45 | + } catch (Exception $e) { |
| 46 | + $this->forwardError($e); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public function close() |
| 51 | + { |
| 52 | + if ($this->closed) { |
| 53 | + return; |
| 54 | + } |
| 55 | + |
| 56 | + $this->closed = true; |
| 57 | + $this->readable = false; |
| 58 | + $this->writable = false; |
| 59 | + |
| 60 | + $this->emit('close', array($this)); |
| 61 | + } |
| 62 | + |
| 63 | + public function isReadable() |
| 64 | + { |
| 65 | + return $this->readable; |
| 66 | + } |
| 67 | + |
| 68 | + public function isWritable() |
| 69 | + { |
| 70 | + return $this->writable; |
| 71 | + } |
| 72 | + |
| 73 | + public function pause() |
| 74 | + { |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + public function resume() |
| 79 | + { |
| 80 | + |
| 81 | + } |
| 82 | + |
| 83 | + public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 84 | + { |
| 85 | + Util::pipe($this, $dest, $options); |
| 86 | + |
| 87 | + return $dest; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Forwards a single "data" event to the reading side of the stream |
| 92 | + * |
| 93 | + * This will emit an "data" event. |
| 94 | + * |
| 95 | + * If the stream is not readable, then this is a NO-OP. |
| 96 | + * |
| 97 | + * @param string $data |
| 98 | + */ |
| 99 | + protected function forwardData($data) |
| 100 | + { |
| 101 | + if (!$this->readable && $data !== '') { |
| 102 | + return; |
| 103 | + } |
| 104 | + $this->emit('data', array($data, $this)); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Forwards an "end" event to the reading side of the stream |
| 109 | + * |
| 110 | + * This will emit an "end" event and will then close this stream. |
| 111 | + * |
| 112 | + * If the stream is not readable, then this is a NO-OP. |
| 113 | + * |
| 114 | + * @uses self::close() |
| 115 | + */ |
| 116 | + protected function forwardEnd() |
| 117 | + { |
| 118 | + if (!$this->readable) { |
| 119 | + return; |
| 120 | + } |
| 121 | + $this->readable = false; |
| 122 | + $this->writable = false; |
| 123 | + |
| 124 | + $this->emit('end', array($this)); |
| 125 | + $this->close(); |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Forwards the given $error message to the reading side of the stream |
| 130 | + * |
| 131 | + * This will emit an "error" event and will then close this stream. |
| 132 | + * |
| 133 | + * If the stream is not readable, then this is a NO-OP. |
| 134 | + * |
| 135 | + * @param Exception $error |
| 136 | + * @uses self::close() |
| 137 | + */ |
| 138 | + protected function forwardError(Exception $error) |
| 139 | + { |
| 140 | + if (!$this->readable) { |
| 141 | + return; |
| 142 | + } |
| 143 | + $this->readable = false; |
| 144 | + $this->writable = false; |
| 145 | + |
| 146 | + $this->emit('error', array($error, $this)); |
| 147 | + $this->close(); |
| 148 | + } |
| 149 | + |
| 150 | + /** |
| 151 | + * can be overwritten in order to implement custom transformation behavior |
| 152 | + * |
| 153 | + * This gets passed a single chunk of $data and should invoke `forwardData()` |
| 154 | + * with the filtered result. |
| 155 | + * |
| 156 | + * If the given data chunk is not valid, then you should invoke `forwardError()` |
| 157 | + * or throw an Exception. |
| 158 | + * |
| 159 | + * If you do not overwrite this method, then its default implementation simply |
| 160 | + * invokes `forwardData()` on the unmodified input data chunk. |
| 161 | + * |
| 162 | + * @param string $data |
| 163 | + * @see self::forwardData() |
| 164 | + */ |
| 165 | + protected function transformData($data) |
| 166 | + { |
| 167 | + $this->forwardData($data); |
| 168 | + } |
| 169 | + |
| 170 | + /** |
| 171 | + * can be overwritten in order to implement custom stream ending behavior |
| 172 | + * |
| 173 | + * This may get passed a single final chunk of $data and should invoke `forwardEnd()`. |
| 174 | + * |
| 175 | + * If the given data chunk is not valid, then you should invoke `forwardError()` |
| 176 | + * or throw an Exception. |
| 177 | + * |
| 178 | + * If you do not overwrite this method, then its default implementation simply |
| 179 | + * invokes `transformData()` on the unmodified input data chunk (if any), |
| 180 | + * which in turn defaults to invoking `forwardData()` and then finally |
| 181 | + * invokes `forwardEnd()`. |
| 182 | + * |
| 183 | + * @param string $data |
| 184 | + * @see self::transformData() |
| 185 | + * @see self::forwardData() |
| 186 | + * @see self::forwardEnd() |
| 187 | + */ |
| 188 | + protected function transformEnd($data) |
| 189 | + { |
| 190 | + if ($data !== '') { |
| 191 | + $this->transformData($data); |
| 192 | + } |
| 193 | + $this->forwardEnd(); |
| 194 | + } |
| 195 | +} |
0 commit comments