京东云

This commit is contained in:
“wanyongkang”
2024-05-28 17:04:50 +08:00
parent f644932afb
commit f3c7432100
672 changed files with 209344 additions and 6593 deletions

View File

@@ -1,7 +1,5 @@
<?php
declare(strict_types=1);
namespace GuzzleHttp\Psr7;
use Psr\Http\Message\StreamInterface;
@@ -10,19 +8,16 @@ use Psr\Http\Message\StreamInterface;
* Reads from multiple streams, one after the other.
*
* This is a read-only stream decorator.
*
* @final
*/
final class AppendStream implements StreamInterface
class AppendStream implements StreamInterface
{
/** @var StreamInterface[] Streams being decorated */
private $streams = [];
/** @var bool */
private $seekable = true;
/** @var int */
private $current = 0;
/** @var int */
private $pos = 0;
/**
@@ -36,18 +31,12 @@ final class AppendStream implements StreamInterface
}
}
public function __toString(): string
public function __toString()
{
try {
$this->rewind();
return $this->getContents();
} catch (\Throwable $e) {
if (\PHP_VERSION_ID >= 70400) {
throw $e;
}
trigger_error(sprintf('%s::__toString exception: %s', self::class, (string) $e), E_USER_ERROR);
} catch (\Exception $e) {
return '';
}
}
@@ -59,7 +48,7 @@ final class AppendStream implements StreamInterface
*
* @throws \InvalidArgumentException if the stream is not readable
*/
public function addStream(StreamInterface $stream): void
public function addStream(StreamInterface $stream)
{
if (!$stream->isReadable()) {
throw new \InvalidArgumentException('Each stream must be readable');
@@ -73,15 +62,17 @@ final class AppendStream implements StreamInterface
$this->streams[] = $stream;
}
public function getContents(): string
public function getContents()
{
return Utils::copyToString($this);
}
/**
* Closes each attached stream.
*
* {@inheritdoc}
*/
public function close(): void
public function close()
{
$this->pos = $this->current = 0;
$this->seekable = true;
@@ -97,6 +88,8 @@ final class AppendStream implements StreamInterface
* Detaches each attached stream.
*
* Returns null as it's not clear which underlying stream resource to return.
*
* {@inheritdoc}
*/
public function detach()
{
@@ -112,7 +105,7 @@ final class AppendStream implements StreamInterface
return null;
}
public function tell(): int
public function tell()
{
return $this->pos;
}
@@ -122,8 +115,10 @@ final class AppendStream implements StreamInterface
*
* If any of the streams do not return a valid number, then the size of the
* append stream cannot be determined and null is returned.
*
* {@inheritdoc}
*/
public function getSize(): ?int
public function getSize()
{
$size = 0;
@@ -138,22 +133,24 @@ final class AppendStream implements StreamInterface
return $size;
}
public function eof(): bool
public function eof()
{
return !$this->streams
|| ($this->current >= count($this->streams) - 1
&& $this->streams[$this->current]->eof());
return !$this->streams ||
($this->current >= count($this->streams) - 1 &&
$this->streams[$this->current]->eof());
}
public function rewind(): void
public function rewind()
{
$this->seek(0);
}
/**
* Attempts to seek to the given position. Only supports SEEK_SET.
*
* {@inheritdoc}
*/
public function seek($offset, $whence = SEEK_SET): void
public function seek($offset, $whence = SEEK_SET)
{
if (!$this->seekable) {
throw new \RuntimeException('This AppendStream is not seekable');
@@ -169,7 +166,7 @@ final class AppendStream implements StreamInterface
$stream->rewind();
} catch (\Exception $e) {
throw new \RuntimeException('Unable to seek stream '
.$i.' of the AppendStream', 0, $e);
. $i . ' of the AppendStream', 0, $e);
}
}
@@ -184,8 +181,10 @@ final class AppendStream implements StreamInterface
/**
* Reads from all of the appended streams until the length is met or EOF.
*
* {@inheritdoc}
*/
public function read($length): string
public function read($length)
{
$buffer = '';
$total = count($this->streams) - 1;
@@ -193,18 +192,20 @@ final class AppendStream implements StreamInterface
$progressToNext = false;
while ($remaining > 0) {
// Progress to the next stream if needed.
if ($progressToNext || $this->streams[$this->current]->eof()) {
$progressToNext = false;
if ($this->current === $total) {
break;
}
++$this->current;
$this->current++;
}
$result = $this->streams[$this->current]->read($remaining);
if ($result === '') {
// Using a loose comparison here to match on '', false, and null
if ($result == null) {
$progressToNext = true;
continue;
}
@@ -218,29 +219,26 @@ final class AppendStream implements StreamInterface
return $buffer;
}
public function isReadable(): bool
public function isReadable()
{
return true;
}
public function isWritable(): bool
public function isWritable()
{
return false;
}
public function isSeekable(): bool
public function isSeekable()
{
return $this->seekable;
}
public function write($string): int
public function write($string)
{
throw new \RuntimeException('Cannot write to an AppendStream');
}
/**
* @return mixed
*/
public function getMetadata($key = null)
{
return $key ? null : [];