/** * Decorate the given input stream. * * @param StreamInterface $stream * * @throws \InvalidArgumentException When the given stream is not readable. */ public function __construct(StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException(sprintf('Decorated stream must be readable, given %s', get_class($stream))); } $this->stream = $stream; }
/** * Append another input stream. * * @param StreamInterface $stream * * @throws \InvalidArgumentException When the given stream is not readable. */ public function appendStream(StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Appended input stream must be readable'); } $this->streams[] = $stream; }
public function __construct($filename, StreamInterface $stream, $mediaType = NULL) { if (!$stream->isReadable()) { throw new \InvalidArgumentException(sprintf('Input stream of file "%s" must be readable')); } $this->fileName = new UnicodeString($filename); $this->stream = $stream; $this->mediaType = $mediaType === NULL ? Filesystem::guessMimeTypeFromFilename($this->fileName) : new MediaType($mediaType); }
/** * 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); }
/** * Returns a resource representing the stream. * * @param StreamInterface $stream The stream to get a resource for * * @return resource * @throws \InvalidArgumentException if stream is not readable or writable */ public static function getResource(StreamInterface $stream) { self::register(); if ($stream->isReadable()) { $mode = $stream->isWritable() ? 'r+' : 'r'; } elseif ($stream->isWritable()) { $mode = 'w'; } else { throw new \InvalidArgumentException('The stream must be readable, ' . 'writable, or both.'); } return fopen('guzzle://stream', $mode, null, stream_context_create(['guzzle' => ['stream' => $stream]])); }
/** * 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 isReadable() { return $this->stream->isReadable(); }
public function testDetaches() { $this->b->detach(); $this->assertFalse($this->b->isReadable()); }
/** * {@inheritdoc} */ public function isReadable() { return $this->decoratedStream->isReadable(); }
/** * Create StreamInterface associated resource. * * @param StreamInterface $stream * @return resource * @throws WrapperException */ public static function getResource(StreamInterface $stream) { $mode = null; if ($stream->isReadable()) { $mode = 'r'; } if ($stream->isWritable()) { $mode = !empty($mode) ? 'r+' : 'w'; } if (empty($mode)) { throw new WrapperException("Stream is not available in read or write modes."); } return fopen(self::getUri($stream), $mode); }
protected function runMatches(StreamInterface $stream) { return $stream->isReadable(); }
protected function writeFileContents($file, StreamInterface $stream) { if (!$stream->isReadable()) { throw new \InvalidArgumentException('Resource content stream must be readable'); } $fp = Filesystem::openWriteStream($file, 'wb', 0755, LOCK_EX); try { while (!$stream->eof()) { fwrite($fp, $stream->read(4096)); } } finally { @fclose($fp); } }
/** * Pipe data from an input stream into an output stream. * * @param StreamInterface $in * @param StreamInterface $out * @param int $chunkSize Maximum chunk size being used during copy. * @return int Number of bytes being copied. * * @throws \InvalidArgumentException When input stream is not readable or output stream is not writable. */ public static function pipe(StreamInterface $in, StreamInterface $out, $chunkSize = 8192) { if (!$in->isReadable()) { throw new \InvalidArgumentException(sprintf('Input stream is not readable: %s', get_class($in))); } if (!$out->isWritable()) { throw new \InvalidArgumentException(sprintf('Output stream is not writable: %s', get_class($out))); } $size = 0; while (!$in->eof()) { $size += $out->write($in->read($chunkSize)); } return $size; }
/** * * @param PsrStreamInterface $stream * @param int $maxLen * @return int */ public function writeFrom(PsrStreamInterface $stream, $maxLen = -1) { $bytes = 0; if ($this->isWritable() && $stream->isReadable()) { if ($maxLen < 0) { while (!$stream->eof()) { $data = $stream->read(1024); $bytes += $this->write($data); } } else { if ($maxLen > 0) { while (!$stream->eof() && $maxLen > 0) { $data = $stream->read($maxLen >= 1024 ? 1024 : $maxLen); $length = \strlen($data); $maxLen -= $length; $bytes += $this->write($data); } } } } return $bytes; }