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