/** * Copy stream to another stream. * * @param StreamInterface $src Source stream. * @param StreamInterface $dest Target stream. * * @return void */ public static function copy(StreamInterface $src, StreamInterface $dest) { if ($src->isSeekable()) { $src->rewind(); } while (!$src->eof()) { $dest->write($src->read(4096)); } }
/** * Append stream to stack. * * @param StreamInterface $stream * @return self */ public function add(StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Each stream must be readable'); } $this->seekable = $stream->isSeekable() && $this->seekable; $this->streams[] = $stream; return $this; }
/** * Parse a response from a stream. * * @param StreamInterface $stream * @return ResponseInterface * @throws InvalidArgumentException when the stream is not readable. * @throws UnexpectedValueException when errors occur parsing the message. */ public static function fromStream(StreamInterface $stream) { if (!$stream->isReadable() || !$stream->isSeekable()) { throw new InvalidArgumentException('Message stream must be both readable and seekable'); } $stream->rewind(); list($version, $status, $reasonPhrase) = self::getStatusLine($stream); list($headers, $body) = self::splitStream($stream); return (new Response($body, $status, $headers))->withProtocolVersion($version)->withStatus((int) $status, $reasonPhrase); }
/** * Add a stream to the AppendStream * * @param StreamInterface $stream Stream to append. Must be readable. * * @throws \InvalidArgumentException if the stream is not readable */ public function addStream(StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Each stream must be readable'); } // The stream is only seekable if all streams are seekable if (!$stream->isSeekable()) { $this->seekable = false; } $this->streams[] = $stream; }
/** * Deserialize a request stream to a request instance. * * @param StreamInterface $stream * @return Request * @throws UnexpectedValueException when errors occur parsing the message. */ public static function fromStream(StreamInterface $stream) { if (!$stream->isReadable() || !$stream->isSeekable()) { throw new InvalidArgumentException('Message stream must be both readable and seekable'); } $stream->rewind(); list($method, $requestTarget, $version) = self::getRequestLine($stream); $uri = self::createUriFromRequestTarget($requestTarget); list($headers, $body) = self::splitStream($stream); return (new Request($uri, $method, $body, $headers))->withProtocolVersion($version)->withRequestTarget($requestTarget); }
/** * Converts an stream into an string and returns the result. The position of * the pointer will not change if the stream is seekable. Note this copies * the complete content of the stream into the memory * * @param \Psr\Http\Message\StreamInterface $stream * @return string */ public static function toString(StreamInterface $stream) { if (!$stream->isReadable()) { return ''; } if ($stream->isSeekable()) { $pos = $stream->tell(); if ($pos > 0) { $stream->seek(0); } $content = $stream->getContents(); $stream->seek($pos); } else { $content = $stream->getContents(); } return $content; }
/** * {@inheritdoc} */ public function isSeekable() { return $this->stream->isSeekable(); }
/** * {@inheritdoc} */ public function isSeekable() { return $this->decoratedStream->isSeekable(); }
private function outputBody(StreamInterface $body) { if ($this->chunkSize > 0) { if ($body->isSeekable()) { $body->rewind(); } while (!$body->eof()) { print $body->read($this->chunkSize); } } else { print (string) $body; } }
protected function runMatches(StreamInterface $stream) { return $stream->isSeekable(); }
/** * Parses csv. * * @param StreamInterface $body * * @return array */ protected function parseCsv(StreamInterface $body) { if ($body->isSeekable()) { $body->rewind(); } $stream = $body->detach(); $data = []; while (($row = fgetcsv($stream)) !== false) { $data[] = $row; } fclose($stream); return $data; }
public function testCheckMethods() { $this->assertEquals($this->a->isReadable(), $this->b->isReadable()); $this->assertEquals($this->a->isWritable(), $this->b->isWritable()); $this->assertEquals($this->a->isSeekable(), $this->b->isSeekable()); }