vendor/guzzlehttp/psr7/src/PumpStream.php

230 lines · 7042 bytes

  1<?php
  2
  3declare(strict_types=1);
  4
  5namespace GuzzleHttp\Psr7;
  6
  7use Psr\Http\Message\StreamInterface;
  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 suggested
 13 * number of bytes 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 callable is buffered internally until drained using the
 16 * read() function of the PumpStream. The callable MUST return false or null
 17 * when there is no more data to read.
 18 *
 19 * Userland callables that declare no parameters are tolerated by PHP, but
 20 * length-aware callables remain the recommended formal shape.
 21 */
 22final class PumpStream implements StreamInterface
 23{
 24    /** @var callable|null */
 25    private $source;
 26
 27    /** @var int|null */
 28    private $size;
 29
 30    /** @var int */
 31    private $tellPos = 0;
 32
 33    /** @var array */
 34    private $metadata;
 35
 36    /** @var BufferStream */
 37    private $buffer;
 38
 39    /**
 40     * @param (callable(): (string|false|null))|(callable(int): (string|false|null)) $source  Source of the stream data. The callable receives
 41     *                                                                                        the suggested number of bytes to read, may ignore
 42     *                                                                                        that value, and may return fewer or more bytes.
 43     *                                                                                        Extra bytes are buffered. The callable MUST return
 44     *                                                                                        a string when called, or false|null on error or EOF.
 45     *                                                                                        Userland callables that declare no parameters are
 46     *                                                                                        tolerated by PHP, but length-aware callables remain
 47     *                                                                                        the recommended formal shape.
 48     * @param array{size?: int, metadata?: array}                                    $options Stream options:
 49     *                                                                                        - metadata: Hash of metadata to use with stream.
 50     *                                                                                        - size: Size of the stream, if known.
 51     */
 52    public function __construct(callable $source, array $options = [])
 53    {
 54        $this->source = $source;
 55        $this->size = $options['size'] ?? null;
 56        $this->metadata = $options['metadata'] ?? [];
 57        $this->buffer = new BufferStream();
 58    }
 59
 60    public function __toString(): string
 61    {
 62        try {
 63            return Utils::copyToString($this);
 64        } catch (\Throwable $e) {
 65            if (\PHP_VERSION_ID >= 70400) {
 66                throw $e;
 67            }
 68            trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);
 69
 70            return '';
 71        }
 72    }
 73
 74    public function close(): void
 75    {
 76        $this->detach();
 77    }
 78
 79    public function detach()
 80    {
 81        $this->tellPos = 0;
 82        $this->source = null;
 83
 84        return null;
 85    }
 86
 87    public function getSize(): ?int
 88    {
 89        return $this->size;
 90    }
 91
 92    public function tell(): int
 93    {
 94        return $this->tellPos;
 95    }
 96
 97    public function eof(): bool
 98    {
 99        return $this->source === null;
100    }
101
102    public function isSeekable(): bool
103    {
104        return false;
105    }
106
107    public function rewind(): void
108    {
109        $this->seek(0);
110    }
111
112    public function seek($offset, $whence = SEEK_SET): void
113    {
114        if (!\is_int($offset)) {
115            \trigger_deprecation(
116                'guzzlehttp/psr7',
117                '2.11',
118                'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.',
119                \get_debug_type($offset)
120            );
121        }
122
123        if (!\is_int($whence)) {
124            \trigger_deprecation(
125                'guzzlehttp/psr7',
126                '2.11',
127                'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.',
128                \get_debug_type($whence)
129            );
130        }
131
132        throw new \RuntimeException('Cannot seek a PumpStream');
133    }
134
135    public function isWritable(): bool
136    {
137        return false;
138    }
139
140    public function write($string): int
141    {
142        if (!\is_string($string)) {
143            \trigger_deprecation(
144                'guzzlehttp/psr7',
145                '2.11',
146                'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.',
147                \get_debug_type($string)
148            );
149        }
150
151        throw new \RuntimeException('Cannot write to a PumpStream');
152    }
153
154    public function isReadable(): bool
155    {
156        return true;
157    }
158
159    public function read($length): string
160    {
161        if (!\is_int($length)) {
162            \trigger_deprecation(
163                'guzzlehttp/psr7',
164                '2.11',
165                'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
166                \get_debug_type($length)
167            );
168        }
169
170        $data = $this->buffer->read($length);
171        $readLen = strlen($data);
172        $this->tellPos += $readLen;
173        $remaining = $length - $readLen;
174
175        if ($remaining) {
176            $this->pump($remaining);
177            $data .= $this->buffer->read($remaining);
178            $this->tellPos += strlen($data) - $readLen;
179        }
180
181        return $data;
182    }
183
184    public function getContents(): string
185    {
186        $result = '';
187        while (!$this->eof()) {
188            $result .= $this->read(1000000);
189        }
190
191        return $result;
192    }
193
194    /**
195     * @return mixed
196     */
197    public function getMetadata($key = null)
198    {
199        if ($key !== null && !\is_string($key)) {
200            \trigger_deprecation(
201                'guzzlehttp/psr7',
202                '2.11',
203                'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.',
204                \get_debug_type($key)
205            );
206        }
207
208        if (!$key) {
209            return $this->metadata;
210        }
211
212        return $this->metadata[$key] ?? null;
213    }
214
215    private function pump(int $length): void
216    {
217        if ($this->source !== null) {
218            do {
219                $data = ($this->source)($length);
220                if ($data === false || $data === null) {
221                    $this->source = null;
222
223                    return;
224                }
225                $this->buffer->write($data);
226                $length -= strlen($data);
227            } while ($length > 0);
228        }
229    }
230}