示例#1
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]]));
 }
示例#2
0
 public function send(StreamInterface $stream)
 {
     if (!$stream->isWritable()) {
         throw new \InvalidArgumentException('Output stream must be writable');
     }
     if (is_array($this->callback)) {
         $ref = (new \ReflectionClass(is_object($this->callback[0]) ? get_class($this->callback[0]) : $this->callback[0]))->getMethod($this->callback[1]);
     } elseif (is_object($this->callback) && !$this->callback instanceof \Closure) {
         $ref = new \ReflectionMethod(get_class($this->callback), '__invoke');
     } else {
         $ref = new \ReflectionFunction($this->callback);
     }
     if ($ref->isGenerator()) {
         foreach (call_user_func($this->callback) as $chunk) {
             $stream->write($chunk);
         }
         return;
     }
     foreach ($ref->getParameters() as $param) {
         if (NULL !== ($type = $param->getClass())) {
             if ($type->name === StreamInterface::class || $type->implementsInterface(StreamInterface::class)) {
                 call_user_func($this->callback, $stream);
                 return;
             }
         }
         break;
     }
     $stream->write((string) call_user_func($this->callback));
 }
示例#3
0
 public function send(StreamInterface $stream)
 {
     if (!$stream->isWritable()) {
         throw new \InvalidArgumentException('Output stream must be writable');
     }
     $stream->write($this->contents);
 }
示例#4
0
 /**
  * Decorate the given output stream.
  * 
  * @param StreamInterface $stream
  * 
  * @throws \InvalidArgumentException When the given stream is not writable.
  */
 public function __construct(StreamInterface $stream)
 {
     if (!$stream->isWritable()) {
         throw new \InvalidArgumentException(sprintf('Decorated stream must be writable, given %s', get_class($stream)));
     }
     $this->stream = $stream;
 }
示例#5
0
 public function send(StreamInterface $stream)
 {
     if (!$stream->isWritable()) {
         throw new \InvalidArgumentException('Output stream must be writable');
     }
     while (!$this->stream->eof()) {
         $stream->write($this->stream->read(4096));
     }
 }
示例#6
0
 public function send(StreamInterface $stream)
 {
     if ($this->skipBinary) {
         return;
     }
     if (!$stream->isWritable()) {
         throw new \InvalidArgumentException('Output stream must be writable');
     }
     $in = ResourceInputStream::fromUrl($this->file->getPathname());
     try {
         while (!$in->eof()) {
             $stream->write($in->read(4096));
         }
     } finally {
         $in->close();
     }
 }
示例#7
0
 protected function sendData(StreamInterface $stream, $prefix, array $data)
 {
     if (!$stream->isWritable()) {
         throw new \InvalidArgumentException('Output stream must be writable');
     }
     foreach ($data as $k => $v) {
         if (is_numeric($k)) {
             $field = $prefix == '' ? $k : sprintf('%s[]', $prefix);
         } else {
             $field = $prefix == '' ? new UnicodeString($k) : sprintf('%s[%s]', $prefix, new UnicodeString($k));
         }
         if (is_array($v)) {
             $this->sendData($stream, $field, $v);
         } else {
             $stream->write(sprintf("--%s\r\n", $this->boundary));
             if ($v instanceof UploadedFileInterface) {
                 $filename = new UnicodeString($v->getFileName());
                 $type = $v->getMediaType();
                 $in = $v->getInputStream();
                 $stream->write(sprintf('Content-Disposition: form-data; name="%s"; filename="%s"; charset="utf-8"', $field, $filename));
                 $stream->write(sprintf("\r\nContent-Type: %s\r\n", $type));
                 $stream->write("Content-Transfer-Encoding: binary\r\n");
                 $stream->write("\r\n");
                 while (false !== ($chunk = $in->read())) {
                     $stream->write($chunk);
                 }
                 $stream->write("\r\n");
             } else {
                 $stream->write(sprintf('Content-Disposition: form-data; name="%s"; charset="utf-8"', $field));
                 $stream->write("\r\n\r\n");
                 $stream->write(new UnicodeString($v));
                 $stream->write("\r\n");
             }
         }
     }
 }
示例#8
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;
 }
 /**
  * {@inheritdoc}
  */
 public function isWritable()
 {
     return $this->stream->isWritable();
 }
 /**
  * {@inheritdoc}
  */
 public function isWritable()
 {
     return $this->decoratedStream->isWritable();
 }
示例#11
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);
 }
示例#12
0
 protected function runMatches(StreamInterface $stream)
 {
     return $stream->isWritable();
 }
 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());
 }