public function testGetsContents()
 {
     $this->assertEquals('foo', $this->b->getContents());
     $this->assertEquals('', $this->b->getContents());
     $this->b->seek(1);
     $this->assertEquals('oo', $this->b->getContents());
 }
示例#2
0
 /**
  * {@inheritdoc}
  */
 public function __toString() : string
 {
     $str = $this->getStartLine();
     foreach ($this->getHeaders() as $name => $values) {
         $str .= sprintf("%s: %s\r\n", $name, implode(', ', $values));
     }
     $str .= "\r\n";
     $this->body->rewind();
     $str .= $this->body->getContents();
     return $str;
 }
 /**
  * {@inheritdoc}
  *
  * @param StreamInterface $content
  *   (Optional) The content to override the input stream with. This is mainly
  *   here for testing purposes.
  *
  */
 public static function fromGlobals(array $server = null, array $query = null, array $body = null, array $cookies = null, array $files = null, StreamInterface $content = null)
 {
     $server = static::normalizeServer($server ?: $_SERVER);
     $files = static::normalizeFiles($files ?: $_FILES);
     $headers = static::marshalHeaders($server);
     // static::get() has a default parameter, however, if the header is set but
     // the value is NULL, e.g. during a drush operation, the NULL result is
     // returned, instead of the default.
     $method = strtoupper(static::get('REQUEST_METHOD', $server) ?: 'GET');
     $request = new JsonRequest($server, $files, static::marshalUriFromServer($server, $headers), $method, $content ?: 'php://input', $headers);
     $is_json = strpos(static::get('CONTENT_TYPE', $server), 'application/json') !== FALSE;
     $vars_in_body = in_array($method, ['PUT', 'POST', 'PATCH', 'DELETE']);
     if ($vars_in_body) {
         $data = $content ? $content->getContents() : file_get_contents('php://input');
         $body = $body ?: [];
         $new = [];
         if ($is_json) {
             $new = json_decode($data, TRUE);
         } else {
             parse_str($data, $new);
         }
         // Merge in data to $body.
         $body = array_merge($body, $new);
     }
     return $request->withCookieParams($cookies ?: $_COOKIE)->withQueryParams($query ?: $_GET)->withParsedBody($body ?: $_POST);
 }
 /**
  * {@inheritdoc}
  */
 public function getContents()
 {
     if ($this->tell() < 0) {
         throw new RuntimeException('Invalid pointer position');
     }
     return $this->decoratedStream->getContents();
 }
示例#5
0
 public function getContents()
 {
     if ($this->stream === NULL) {
         throw new \RuntimeException(sprintf('Cannot read contents of detached stream'));
     }
     return $this->stream->getContents();
 }
示例#6
0
文件: Util.php 项目: 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 __invoke(StreamInterface $stream)
 {
     $request = \GuzzleHttp\Psr7\parse_request($stream->getContents());
     $response = $this->run($request);
     if ($this->getAssertionCallback()) {
         call_user_func($this->getAssertionCallback(), $request);
     }
     return \GuzzleHttp\Psr7\stream_for(\GuzzleHttp\Psr7\str($response));
 }
示例#8
0
 public function testReadAndWrite()
 {
     $this->assertEquals(0, $this->stream->getSize());
     $this->stream->write("Hello World, And All Developers!");
     $this->assertEquals(32, $this->stream->getSize());
     // size
     $this->assertEquals(32, $this->stream->tell());
     // pointer
     $this->stream->rewind();
     $this->assertEquals(0, $this->stream->tell());
     $this->assertFalse($this->stream->eof());
     $this->assertEquals("Hell", $this->stream->read(4));
     $this->assertEquals("o World, ", $this->stream->read(9));
     $this->assertEquals("And All Developers!", $this->stream->getContents());
     $this->assertTrue($this->stream->eof());
     $this->stream->seek(12);
     $this->assertEquals(6, $this->stream->write('Hum...'));
     $this->assertEquals("ll Developers!", $this->stream->getContents());
     $this->assertEquals("Hello World,Hum...ll Developers!", $this->stream->__toString());
 }
示例#9
0
 /**
  * {@inheritdoc}
  *
  * @see http://php.net/is_uploaded_file
  * @see http://php.net/move_uploaded_file
  * @param string $targetPath Path to which to move the uploaded file.
  * @throws \InvalidArgumentException if the $path specified is invalid.
  * @throws \RuntimeException on any error during the move operation, or on
  *     the second or subsequent call to the method.
  */
 public function moveTo($targetPath)
 {
     if ($this->moved === true) {
         throw new RuntimeException('This file has already been moved.');
     }
     if (is_writable($targetPath) === false) {
         throw new InvalidArgumentException('Unable to write to target path');
     }
     if (strpos(PHP_SAPI, 'cli') === 0) {
         $stream = new Stream($targetPath, 'wb+');
         $this->getStream()->rewind();
         $stream->write($this->stream->getContents());
     } else {
         // @codeCoverageIgnoreStart
         if (move_uploaded_file($this->stream, $targetPath) === false) {
             throw new RuntimeException('There was a problem moving your uploaded file.');
         }
         // @codeCoverageIgnoreEnd
     }
     $this->moved = true;
 }
 /**
  * Create a new Credentials instance.
  *
  * @param string|array scope the scope of the access request, expressed
  *   either as an Array or as a space-delimited String.
  *
  * @param StreamInterface jsonKeyStream read it to get the JSON credentials.
  *
  */
 public static function makeCredentials($scope, StreamInterface $jsonKeyStream)
 {
     $jsonKey = json_decode($jsonKeyStream->getContents(), true);
     if (!array_key_exists('type', $jsonKey)) {
         throw new \InvalidArgumentException('json key is missing the type field');
     }
     if ($jsonKey['type'] == 'service_account') {
         return new ServiceAccountCredentials($scope, $jsonKey);
     } else {
         if ($jsonKey['type'] == 'authorized_user') {
             return new UserRefreshCredentials($scope, $jsonKey);
         } else {
             throw new \InvalidArgumentException('invalid value in the type field');
         }
     }
 }
 public function getContents()
 {
     return $this->stream->getContents();
 }
示例#12
0
 /**
  * {@inheritdoc}
  */
 public function getContents()
 {
     return $this->decoratedStream->getContents();
 }
示例#13
0
文件: Slim.php 项目: datado/slim
 private static function parseBody(StreamInterface $body)
 {
     $result = json_decode($body->getContents());
     if (json_last_error() !== JSON_ERROR_NONE) {
         throw new \InvalidArgumentException('Invalid json');
     }
     return $result;
 }
示例#14
0
 /**
  * Sends Body.
  *
  * @param Psr\Http\Message\StreamInterface $body The body to send as stream
  *
  * @author Benjamin Carl <*****@*****.**>
  * @return void
  * @access protected
  */
 protected function sendBody(Stream $body)
 {
     // I don't trust that this will be at the beginning of the stream, so reset.
     $body->rewind();
     // writing to an arbitrary stream.
     // @todo Use stream operations to make this more robust and allow
     $bytes = 0;
     if ($bytes = $body->getSize() && $bytes < 500) {
         print $body->getContents();
     } else {
         while (!$body->eof()) {
             $data = $body->read(1024);
             print $data;
         }
     }
 }
 /**
  * Uploads the contents of the resource (this could be a file handle) to Communibase
  *
  * @param StreamInterface $resource
  * @param string $name
  * @param string $destinationPath
  * @param string $id
  *
  * @return array|mixed
  *
  * @throws \RuntimeException | Exception
  */
 public function updateBinary(StreamInterface $resource, $name, $destinationPath, $id = '')
 {
     $metaData = ['path' => $destinationPath];
     if (!empty($id)) {
         if (!static::isIdValid($id)) {
             throw new Exception('Id is invalid, please use a correctly formatted id');
         }
         return $this->doPut('File.json/crud/' . $id, [], ['filename' => $name, 'length' => $resource->getSize(), 'uploadDate' => date('c'), 'metadata' => $metaData, 'content' => base64_encode($resource->getContents())]);
     }
     $options = ['multipart' => [['name' => 'File', 'filename' => $name, 'contents' => $resource], ['name' => 'metadata', 'contents' => json_encode($metaData)]]];
     $response = $this->call('post', ['File.json/binary', $options]);
     return $this->parseResult($response->getBody(), $response->getStatusCode());
 }
示例#16
0
 public function withBody(\Psr\Http\Message\StreamInterface $body)
 {
     $this->withBodyRaw($body->getContents());
 }
示例#17
0
 /**
  * Return an instance with the specified message body.
  *
  * The body MUST be a StreamInterface object.
  *
  * This method MUST be implemented in such a way as to retain the
  * immutability of the message, and MUST return a new instance that has the
  * new body stream.
  *
  * @param StreamInterface $body Body.
  * @return self
  * @throws \InvalidArgumentException When the body is not valid.
  */
 public function withBody(StreamInterface $body)
 {
     $new = clone $this;
     $new->body($body->getContents());
     return $new;
 }
示例#18
0
 /**
  * @param StreamInterface $stream
  *
  * @return string
  */
 protected function streamToString(StreamInterface $stream)
 {
     $stream->rewind();
     return $stream->getContents();
 }