|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Clue\React\Docker\Io; |
| 4 | + |
| 5 | +use Clue\JsonStream\StreamingJsonParser; |
| 6 | +use React\Stream\ReadableStreamInterface; |
| 7 | +use React\Stream\Util; |
| 8 | +use React\Stream\WritableStreamInterface; |
| 9 | +use Evenement\EventEmitter; |
| 10 | + |
| 11 | +/** |
| 12 | + * Parser for Docker's JSON stream format used for log messages etc. |
| 13 | + * |
| 14 | + * @internal |
| 15 | + */ |
| 16 | +class ReadableJsonStream extends EventEmitter implements ReadableStreamInterface |
| 17 | +{ |
| 18 | + private $closed = false; |
| 19 | + private $input; |
| 20 | + private $parser; |
| 21 | + |
| 22 | + public function __construct(ReadableStreamInterface $input) |
| 23 | + { |
| 24 | + $this->input = $input; |
| 25 | + $this->parser = $parser = new StreamingJsonParser(); |
| 26 | + if (!$input->isReadable()) { |
| 27 | + $this->close(); |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + // pass all input data chunks through the parser |
| 32 | + $input->on('data', array($this, 'handleData')); |
| 33 | + |
| 34 | + // forward end event to output |
| 35 | + $out = $this; |
| 36 | + $closed =& $this->closed; |
| 37 | + $input->on('end', function () use ($out, $parser, &$closed) { |
| 38 | + // ignore duplicate end events |
| 39 | + if ($closed) { |
| 40 | + return; |
| 41 | + } |
| 42 | + |
| 43 | + if ($parser->isEmpty()) { |
| 44 | + $out->emit('end'); |
| 45 | + } else { |
| 46 | + $out->emit('error', array(new \RuntimeException('Stream ended within incomplete JSON data'))); |
| 47 | + } |
| 48 | + $out->close(); |
| 49 | + }); |
| 50 | + |
| 51 | + // forward error event to output |
| 52 | + $input->on('error', function ($error) use ($out) { |
| 53 | + $out->emit('error', array($error)); |
| 54 | + $out->close(); |
| 55 | + }); |
| 56 | + |
| 57 | + // forward close event to output |
| 58 | + $input->on('close', function () use ($out) { |
| 59 | + $out->close(); |
| 60 | + }); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * push the given stream chunk into the parser buffer and try to extract all JSON messages |
| 65 | + * |
| 66 | + * @internal |
| 67 | + * @param string $data |
| 68 | + */ |
| 69 | + public function handleData($data) |
| 70 | + { |
| 71 | + // forward each data chunk to the streaming JSON parser |
| 72 | + try { |
| 73 | + $objects = $this->parser->push($data); |
| 74 | + } catch (\Exception $e) { |
| 75 | + $this->emit('error', array($e)); |
| 76 | + $this->close(); |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + foreach ($objects as $object) { |
| 81 | + // stop emitting data if stream is already closed |
| 82 | + if ($this->closed) { |
| 83 | + return; |
| 84 | + } |
| 85 | + |
| 86 | + if (isset($object['error'])) { |
| 87 | + $this->emit('error', array(new \RuntimeException($object['error']))); |
| 88 | + $this->close(); |
| 89 | + return; |
| 90 | + } |
| 91 | + $this->emit('data', array($object)); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + public function pause() |
| 96 | + { |
| 97 | + $this->input->pause(); |
| 98 | + } |
| 99 | + |
| 100 | + public function resume() |
| 101 | + { |
| 102 | + $this->input->resume(); |
| 103 | + } |
| 104 | + |
| 105 | + public function isReadable() |
| 106 | + { |
| 107 | + return $this->input->isReadable(); |
| 108 | + } |
| 109 | + |
| 110 | + public function pipe(WritableStreamInterface $dest, array $options = array()) |
| 111 | + { |
| 112 | + return Util::pipe($this, $dest, $options); |
| 113 | + } |
| 114 | + |
| 115 | + public function close() |
| 116 | + { |
| 117 | + if ($this->closed) { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + $this->closed = true; |
| 122 | + |
| 123 | + // closing output stream closes input stream |
| 124 | + $this->input->close(); |
| 125 | + |
| 126 | + $this->emit('close'); |
| 127 | + $this->removeAllListeners(); |
| 128 | + } |
| 129 | +} |
0 commit comments