本文整理汇总了PHP中Psr\Http\Message\StreamInterface::getContents方法的典型用法代码示例。如果您正苦于以下问题:PHP StreamInterface::getContents方法的具体用法?PHP StreamInterface::getContents怎么用?PHP StreamInterface::getContents使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\StreamInterface
的用法示例。
在下文中一共展示了StreamInterface::getContents方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: testGetsContents
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: __toString
/**
* {@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;
}
示例3: fromGlobals
/**
* {@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);
}
示例4: getContents
/**
* {@inheritdoc}
*/
public function getContents()
{
if ($this->tell() < 0) {
throw new RuntimeException('Invalid pointer position');
}
return $this->decoratedStream->getContents();
}
示例5: getContents
public function getContents()
{
if ($this->stream === NULL) {
throw new \RuntimeException(sprintf('Cannot read contents of detached stream'));
}
return $this->stream->getContents();
}
示例6: toString
/**
* 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;
}
示例7: __invoke
/**
* {@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: testReadAndWrite
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: moveTo
/**
* {@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;
}
示例10: makeCredentials
/**
* 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');
}
}
}
示例11: getContents
public function getContents()
{
return $this->stream->getContents();
}
示例12: getContents
/**
* {@inheritdoc}
*/
public function getContents()
{
return $this->decoratedStream->getContents();
}
示例13: parseBody
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: sendBody
/**
* Sends Body.
*
* @param Psr\Http\Message\StreamInterface $body The body to send as stream
*
* @author Benjamin Carl <opensource@clickalicious.de>
* @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;
}
}
}
示例15: updateBinary
/**
* 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());
}