|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Swlib\Http; |
| 4 | + |
| 5 | +use Exception; |
| 6 | +use Psr\Http\Message\StreamInterface; |
| 7 | +use RuntimeException; |
| 8 | + |
| 9 | +/** |
| 10 | + * Provides a read only stream that pumps data from a PHP callable. |
| 11 | + * |
| 12 | + * When invoking the provided callable, the PumpStream will pass the amount of |
| 13 | + * data requested to read to the callable. The callable can choose to ignore |
| 14 | + * this value and return fewer or more bytes than requested. Any extra data |
| 15 | + * returned by the provided callable is buffered internally until drained using |
| 16 | + * the read() function of the PumpStream. The provided callable MUST return |
| 17 | + * false when there is no more data to read. |
| 18 | + */ |
| 19 | +class PumpStream implements StreamInterface |
| 20 | +{ |
| 21 | + /** @var callable */ |
| 22 | + private $source; |
| 23 | + |
| 24 | + /** @var int */ |
| 25 | + private $size; |
| 26 | + |
| 27 | + /** @var int */ |
| 28 | + private $tellPos = 0; |
| 29 | + |
| 30 | + /** @var array */ |
| 31 | + private $metadata; |
| 32 | + |
| 33 | + /** @var BufferStream */ |
| 34 | + private $buffer; |
| 35 | + |
| 36 | + /** |
| 37 | + * @param callable $source Source of the stream data. The callable MAY |
| 38 | + * accept an integer argument used to control the |
| 39 | + * amount of data to return. The callable MUST |
| 40 | + * return a string when called, or false on error |
| 41 | + * or EOF. |
| 42 | + * @param array $options Stream options: |
| 43 | + * - metadata: Hash of metadata to use with stream. |
| 44 | + * - size: Size of the stream, if known. |
| 45 | + */ |
| 46 | + public function __construct(callable $source, array $options = []) |
| 47 | + { |
| 48 | + $this->source = $source; |
| 49 | + $this->size = isset($options['size']) ? $options['size'] : null; |
| 50 | + $this->metadata = isset($options['metadata']) ? $options['metadata'] : []; |
| 51 | + $this->buffer = new BufferStream(); |
| 52 | + } |
| 53 | + |
| 54 | + public function __toString() |
| 55 | + { |
| 56 | + try { |
| 57 | + return copy_to_string($this); |
| 58 | + } catch (Exception $e) { |
| 59 | + return ''; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + public function close() |
| 64 | + { |
| 65 | + $this->detach(); |
| 66 | + } |
| 67 | + |
| 68 | + public function detach() |
| 69 | + { |
| 70 | + $this->tellPos = false; |
| 71 | + $this->source = null; |
| 72 | + } |
| 73 | + |
| 74 | + public function getSize() |
| 75 | + { |
| 76 | + return $this->size; |
| 77 | + } |
| 78 | + |
| 79 | + public function tell() |
| 80 | + { |
| 81 | + return $this->tellPos; |
| 82 | + } |
| 83 | + |
| 84 | + public function eof() |
| 85 | + { |
| 86 | + return !$this->source; |
| 87 | + } |
| 88 | + |
| 89 | + public function isSeekable() |
| 90 | + { |
| 91 | + return false; |
| 92 | + } |
| 93 | + |
| 94 | + public function rewind() |
| 95 | + { |
| 96 | + $this->seek(0); |
| 97 | + } |
| 98 | + |
| 99 | + public function seek($offset, $whence = SEEK_SET) |
| 100 | + { |
| 101 | + throw new RuntimeException('Cannot seek a PumpStream'); |
| 102 | + } |
| 103 | + |
| 104 | + public function isWritable() |
| 105 | + { |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + public function write($string) |
| 110 | + { |
| 111 | + throw new RuntimeException('Cannot write to a PumpStream'); |
| 112 | + } |
| 113 | + |
| 114 | + public function isReadable() |
| 115 | + { |
| 116 | + return true; |
| 117 | + } |
| 118 | + |
| 119 | + public function read($length) |
| 120 | + { |
| 121 | + $data = $this->buffer->read($length); |
| 122 | + $readLen = strlen($data); |
| 123 | + $this->tellPos += $readLen; |
| 124 | + $remaining = $length - $readLen; |
| 125 | + |
| 126 | + if ($remaining) { |
| 127 | + $this->pump($remaining); |
| 128 | + $data .= $this->buffer->read($remaining); |
| 129 | + $this->tellPos += strlen($data) - $readLen; |
| 130 | + } |
| 131 | + |
| 132 | + return $data; |
| 133 | + } |
| 134 | + |
| 135 | + public function getContents() |
| 136 | + { |
| 137 | + $result = ''; |
| 138 | + while (!$this->eof()) { |
| 139 | + $result .= $this->read(1000000); |
| 140 | + } |
| 141 | + |
| 142 | + return $result; |
| 143 | + } |
| 144 | + |
| 145 | + public function getMetadata($key = null) |
| 146 | + { |
| 147 | + if (!$key) { |
| 148 | + return $this->metadata; |
| 149 | + } |
| 150 | + |
| 151 | + return isset($this->metadata[$key]) ? $this->metadata[$key] : null; |
| 152 | + } |
| 153 | + |
| 154 | + private function pump($length) |
| 155 | + { |
| 156 | + if ($this->source) { |
| 157 | + do { |
| 158 | + $data = call_user_func($this->source, $length); |
| 159 | + if ($data === false || $data === null) { |
| 160 | + $this->source = null; |
| 161 | + return; |
| 162 | + } |
| 163 | + $this->buffer->write($data); |
| 164 | + $length -= strlen($data); |
| 165 | + } while ($length > 0); |
| 166 | + } |
| 167 | + } |
| 168 | +} |
0 commit comments