vendor/guzzlehttp/psr7/src/LimitStream.php

188 lines · 5230 bytes

  1<?php
  2
  3declare(strict_types=1);
  4
  5namespace GuzzleHttp\Psr7;
  6
  7use Psr\Http\Message\StreamInterface;
  8
  9/**
 10 * Decorator used to return only a subset of a stream.
 11 */
 12final class LimitStream implements StreamInterface
 13{
 14    use StreamDecoratorTrait;
 15
 16    /** @var int Offset to start reading from */
 17    private $offset;
 18
 19    /** @var int Limit the number of bytes that can be read */
 20    private $limit;
 21
 22    /** @var StreamInterface */
 23    private $stream;
 24
 25    /**
 26     * @param StreamInterface $stream Stream to wrap
 27     * @param int             $limit  Total number of bytes to allow to be read
 28     *                                from the stream. Pass -1 for no limit.
 29     * @param int             $offset Position to seek to before reading (only
 30     *                                works on seekable streams).
 31     */
 32    public function __construct(
 33        StreamInterface $stream,
 34        int $limit = -1,
 35        int $offset = 0
 36    ) {
 37        $this->stream = $stream;
 38        $this->setLimit($limit);
 39        $this->setOffset($offset);
 40    }
 41
 42    public function eof(): bool
 43    {
 44        // Always return true if the underlying stream is EOF
 45        if ($this->stream->eof()) {
 46            return true;
 47        }
 48
 49        // No limit and the underlying stream is not at EOF
 50        if ($this->limit === -1) {
 51            return false;
 52        }
 53
 54        return $this->stream->tell() >= $this->offset + $this->limit;
 55    }
 56
 57    /**
 58     * Returns the size of the limited subset of data
 59     */
 60    public function getSize(): ?int
 61    {
 62        if (null === ($length = $this->stream->getSize())) {
 63            return null;
 64        }
 65
 66        $size = $length - $this->offset;
 67
 68        if ($this->limit !== -1) {
 69            $size = min($this->limit, $size);
 70        }
 71
 72        return max(0, $size);
 73    }
 74
 75    /**
 76     * Allow for a bounded seek on the read limited stream
 77     */
 78    public function seek($offset, $whence = SEEK_SET): void
 79    {
 80        if (!\is_int($offset)) {
 81            \trigger_deprecation(
 82                'guzzlehttp/psr7',
 83                '2.11',
 84                'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.',
 85                \get_debug_type($offset)
 86            );
 87        }
 88
 89        if (!\is_int($whence)) {
 90            \trigger_deprecation(
 91                'guzzlehttp/psr7',
 92                '2.11',
 93                'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.',
 94                \get_debug_type($whence)
 95            );
 96        }
 97
 98        if ($whence !== SEEK_SET || $offset < 0) {
 99            throw new \RuntimeException(sprintf(
100                'Cannot seek to offset %s with whence %s',
101                $offset,
102                $whence
103            ));
104        }
105
106        $offset += $this->offset;
107
108        if ($this->limit !== -1) {
109            if ($offset > $this->offset + $this->limit) {
110                $offset = $this->offset + $this->limit;
111            }
112        }
113
114        $this->stream->seek($offset);
115    }
116
117    /**
118     * Give a relative tell()
119     */
120    public function tell(): int
121    {
122        return $this->stream->tell() - $this->offset;
123    }
124
125    /**
126     * Set the offset to start limiting from
127     *
128     * @param int $offset Offset to seek to and begin byte limiting from
129     *
130     * @throws \RuntimeException if the stream cannot be seeked.
131     */
132    public function setOffset(int $offset): void
133    {
134        $current = $this->stream->tell();
135
136        if ($current !== $offset) {
137            // If the stream cannot seek to the offset position, then read to it
138            if ($this->stream->isSeekable()) {
139                $this->stream->seek($offset);
140            } elseif ($current > $offset) {
141                throw new \RuntimeException("Could not seek to stream offset $offset");
142            } else {
143                $this->stream->read($offset - $current);
144            }
145        }
146
147        $this->offset = $offset;
148    }
149
150    /**
151     * Set the limit of bytes that the decorator allows to be read from the
152     * stream.
153     *
154     * @param int $limit Number of bytes to allow to be read from the stream.
155     *                   Use -1 for no limit.
156     */
157    public function setLimit(int $limit): void
158    {
159        $this->limit = $limit;
160    }
161
162    public function read($length): string
163    {
164        if (!\is_int($length)) {
165            \trigger_deprecation(
166                'guzzlehttp/psr7',
167                '2.11',
168                'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.',
169                \get_debug_type($length)
170            );
171        }
172
173        if ($this->limit === -1) {
174            return $this->stream->read($length);
175        }
176
177        // Check if the current position is less than the total allowed
178        // bytes + original offset
179        $remaining = ($this->offset + $this->limit) - $this->stream->tell();
180        if ($remaining > 0) {
181            // Only return the amount of requested data, ensuring that the byte
182            // limit is not exceeded
183            return $this->stream->read(min($remaining, $length));
184        }
185
186        return '';
187    }
188}