Exemplo n.º 1
0
 /**
  * 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;
 }
Exemplo n.º 2
0
 /**
  * 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;
 }
Exemplo n.º 3
0
 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);
 }
Exemplo n.º 4
0
 /**
  * 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;
 }
Exemplo n.º 5
0
 /**
  * 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);
 }
Exemplo n.º 6
0
 /**
  * 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;
 }
Exemplo n.º 7
0
 /**
  * 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);
 }
Exemplo n.º 8
0
 /**
  * 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]]));
 }
Exemplo n.º 9
0
Arquivo: Util.php Projeto: seytar/psx
 /**
  * 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());
 }
Exemplo n.º 12
0
 /**
  * {@inheritdoc}
  */
 public function isReadable()
 {
     return $this->decoratedStream->isReadable();
 }
Exemplo n.º 13
0
 /**
  * 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);
 }
Exemplo n.º 14
0
 protected function runMatches(StreamInterface $stream)
 {
     return $stream->isReadable();
 }
Exemplo n.º 15
0
 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);
     }
 }
Exemplo n.º 16
0
 /**
  * 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;
 }
Exemplo n.º 17
0
 /**
  * 
  * @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;
 }