← All changes
|
vendor_prefixed/guzzlehttp/psr7/src/AppendStream.php
+0
-221
28.0
→
trunk
View file →
| @@ -1,221 +1,0 @@ | ||
| 1 | -<?php | |
| 2 | - | |
| 3 | -declare (strict_types=1); | |
| 4 | -namespace YoastSEO_Vendor\GuzzleHttp\Psr7; | |
| 5 | - | |
| 6 | -use YoastSEO_Vendor\Psr\Http\Message\StreamInterface; | |
| 7 | -/** | |
| 8 | - * Reads from multiple streams, one after the other. | |
| 9 | - * | |
| 10 | - * This is a read-only stream decorator. | |
| 11 | - */ | |
| 12 | -final class AppendStream implements \YoastSEO_Vendor\Psr\Http\Message\StreamInterface | |
| 13 | -{ | |
| 14 | - /** @var StreamInterface[] Streams being decorated */ | |
| 15 | - private $streams = []; | |
| 16 | - /** @var bool */ | |
| 17 | - private $seekable = \true; | |
| 18 | - /** @var int */ | |
| 19 | - private $current = 0; | |
| 20 | - /** @var int */ | |
| 21 | - private $pos = 0; | |
| 22 | - /** | |
| 23 | - * @param StreamInterface[] $streams Streams to decorate. Each stream must | |
| 24 | - * be readable. | |
| 25 | - */ | |
| 26 | - public function __construct(array $streams = []) | |
| 27 | - { | |
| 28 | - foreach ($streams as $stream) { | |
| 29 | - $this->addStream($stream); | |
| 30 | - } | |
| 31 | - } | |
| 32 | - public function __toString() : string | |
| 33 | - { | |
| 34 | - try { | |
| 35 | - $this->rewind(); | |
| 36 | - return $this->getContents(); | |
| 37 | - } catch (\Throwable $e) { | |
| 38 | - if (\PHP_VERSION_ID >= 70400) { | |
| 39 | - throw $e; | |
| 40 | - } | |
| 41 | - \trigger_error(\sprintf('%s::__toString exception: %s', self::class, (string) $e), \E_USER_ERROR); | |
| 42 | - return ''; | |
| 43 | - } | |
| 44 | - } | |
| 45 | - /** | |
| 46 | - * Add a stream to the AppendStream | |
| 47 | - * | |
| 48 | - * @param StreamInterface $stream Stream to append. Must be readable. | |
| 49 | - * | |
| 50 | - * @throws \InvalidArgumentException if the stream is not readable | |
| 51 | - */ | |
| 52 | - public function addStream(\YoastSEO_Vendor\Psr\Http\Message\StreamInterface $stream) : void | |
| 53 | - { | |
| 54 | - if (!$stream->isReadable()) { | |
| 55 | - throw new \InvalidArgumentException('Each stream must be readable'); | |
| 56 | - } | |
| 57 | - // The stream is only seekable if all streams are seekable | |
| 58 | - if (!$stream->isSeekable()) { | |
| 59 | - $this->seekable = \false; | |
| 60 | - } | |
| 61 | - $this->streams[] = $stream; | |
| 62 | - } | |
| 63 | - public function getContents() : string | |
| 64 | - { | |
| 65 | - return \YoastSEO_Vendor\GuzzleHttp\Psr7\Utils::copyToString($this); | |
| 66 | - } | |
| 67 | - /** | |
| 68 | - * Closes each attached stream. | |
| 69 | - */ | |
| 70 | - public function close() : void | |
| 71 | - { | |
| 72 | - $this->pos = $this->current = 0; | |
| 73 | - $this->seekable = \true; | |
| 74 | - foreach ($this->streams as $stream) { | |
| 75 | - $stream->close(); | |
| 76 | - } | |
| 77 | - $this->streams = []; | |
| 78 | - } | |
| 79 | - /** | |
| 80 | - * Detaches each attached stream. | |
| 81 | - * | |
| 82 | - * Returns null as it's not clear which underlying stream resource to return. | |
| 83 | - */ | |
| 84 | - public function detach() | |
| 85 | - { | |
| 86 | - $this->pos = $this->current = 0; | |
| 87 | - $this->seekable = \true; | |
| 88 | - foreach ($this->streams as $stream) { | |
| 89 | - $stream->detach(); | |
| 90 | - } | |
| 91 | - $this->streams = []; | |
| 92 | - return null; | |
| 93 | - } | |
| 94 | - public function tell() : int | |
| 95 | - { | |
| 96 | - return $this->pos; | |
| 97 | - } | |
| 98 | - /** | |
| 99 | - * Tries to calculate the size by adding the size of each stream. | |
| 100 | - * | |
| 101 | - * If any of the streams do not return a valid number, then the size of the | |
| 102 | - * append stream cannot be determined and null is returned. | |
| 103 | - */ | |
| 104 | - public function getSize() : ?int | |
| 105 | - { | |
| 106 | - $size = 0; | |
| 107 | - foreach ($this->streams as $stream) { | |
| 108 | - $s = $stream->getSize(); | |
| 109 | - if ($s === null) { | |
| 110 | - return null; | |
| 111 | - } | |
| 112 | - $size += $s; | |
| 113 | - } | |
| 114 | - return $size; | |
| 115 | - } | |
| 116 | - public function eof() : bool | |
| 117 | - { | |
| 118 | - return !$this->streams || $this->current >= \count($this->streams) - 1 && $this->streams[$this->current]->eof(); | |
| 119 | - } | |
| 120 | - public function rewind() : void | |
| 121 | - { | |
| 122 | - $this->seek(0); | |
| 123 | - } | |
| 124 | - /** | |
| 125 | - * Attempts to seek to the given position. Only supports SEEK_SET. | |
| 126 | - */ | |
| 127 | - public function seek($offset, $whence = \SEEK_SET) : void | |
| 128 | - { | |
| 129 | - if (!\is_int($offset)) { | |
| 130 | - \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $offset.', \get_debug_type($offset)); | |
| 131 | - } | |
| 132 | - if (!\is_int($whence)) { | |
| 133 | - \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::seek() is deprecated; guzzlehttp/psr7 3.0 requires int for $whence.', \get_debug_type($whence)); | |
| 134 | - } | |
| 135 | - if (!$this->seekable) { | |
| 136 | - throw new \RuntimeException('This AppendStream is not seekable'); | |
| 137 | - } elseif ($whence !== \SEEK_SET) { | |
| 138 | - throw new \RuntimeException('The AppendStream can only seek with SEEK_SET'); | |
| 139 | - } | |
| 140 | - $this->pos = $this->current = 0; | |
| 141 | - // Rewind each stream | |
| 142 | - foreach ($this->streams as $i => $stream) { | |
| 143 | - try { | |
| 144 | - $stream->rewind(); | |
| 145 | - } catch (\Exception $e) { | |
| 146 | - throw new \RuntimeException('Unable to seek stream ' . $i . ' of the AppendStream', 0, $e); | |
| 147 | - } | |
| 148 | - } | |
| 149 | - // Seek to the actual position by reading from each stream | |
| 150 | - while ($this->pos < $offset && !$this->eof()) { | |
| 151 | - $result = $this->read(\min(8096, $offset - $this->pos)); | |
| 152 | - if ($result === '') { | |
| 153 | - break; | |
| 154 | - } | |
| 155 | - } | |
| 156 | - } | |
| 157 | - /** | |
| 158 | - * Reads from all of the appended streams until the length is met or EOF. | |
| 159 | - */ | |
| 160 | - public function read($length) : string | |
| 161 | - { | |
| 162 | - if (!\is_int($length)) { | |
| 163 | - \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::read() is deprecated; guzzlehttp/psr7 3.0 requires int for $length.', \get_debug_type($length)); | |
| 164 | - } | |
| 165 | - if ($this->streams === []) { | |
| 166 | - return ''; | |
| 167 | - } | |
| 168 | - $buffer = ''; | |
| 169 | - $total = \count($this->streams) - 1; | |
| 170 | - $remaining = $length; | |
| 171 | - $progressToNext = \false; | |
| 172 | - while ($remaining > 0) { | |
| 173 | - // Progress to the next stream if needed. | |
| 174 | - if ($progressToNext || $this->streams[$this->current]->eof()) { | |
| 175 | - $progressToNext = \false; | |
| 176 | - if ($this->current === $total) { | |
| 177 | - break; | |
| 178 | - } | |
| 179 | - ++$this->current; | |
| 180 | - } | |
| 181 | - $result = $this->streams[$this->current]->read($remaining); | |
| 182 | - if ($result === '') { | |
| 183 | - $progressToNext = \true; | |
| 184 | - continue; | |
| 185 | - } | |
| 186 | - $buffer .= $result; | |
| 187 | - $remaining = $length - \strlen($buffer); | |
| 188 | - } | |
| 189 | - $this->pos += \strlen($buffer); | |
| 190 | - return $buffer; | |
| 191 | - } | |
| 192 | - public function isReadable() : bool | |
| 193 | - { | |
| 194 | - return \true; | |
| 195 | - } | |
| 196 | - public function isWritable() : bool | |
| 197 | - { | |
| 198 | - return \false; | |
| 199 | - } | |
| 200 | - public function isSeekable() : bool | |
| 201 | - { | |
| 202 | - return $this->seekable; | |
| 203 | - } | |
| 204 | - public function write($string) : int | |
| 205 | - { | |
| 206 | - if (!\is_string($string)) { | |
| 207 | - \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::write() is deprecated; guzzlehttp/psr7 3.0 requires string for $string.', \get_debug_type($string)); | |
| 208 | - } | |
| 209 | - throw new \RuntimeException('Cannot write to an AppendStream'); | |
| 210 | - } | |
| 211 | - /** | |
| 212 | - * @return mixed | |
| 213 | - */ | |
| 214 | - public function getMetadata($key = null) | |
| 215 | - { | |
| 216 | - if ($key !== null && !\is_string($key)) { | |
| 217 | - \YoastSEO_Vendor\trigger_deprecation('guzzlehttp/psr7', '2.11', 'Passing %s to StreamInterface::getMetadata() is deprecated; guzzlehttp/psr7 3.0 requires string|null for $key.', \get_debug_type($key)); | |
| 218 | - } | |
| 219 | - return $key ? null : []; | |
| 220 | - } | |
| 221 | -} | |