Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Utils.php

function Utils::streamFor

Create a new stream based on the input type.

Options is an associative array that can contain the following keys:

  • metadata: Array of custom metadata.
  • size: Size of the stream.

This method accepts the following `$resource` types:

  • `Psr\Http\Message\StreamInterface`: Returns the value as-is.
  • `string`: Creates a stream object that uses the given string as the contents.
  • `resource`: Creates a stream object that wraps the given PHP stream resource.
  • `Iterator`: If the provided value implements `Iterator`, then a read-only stream object will be created that wraps the given iterable. Each time the stream is read from, data from the iterator will fill a buffer and will be continuously called until the buffer is equal to the requested read size. Subsequent read calls will first read from the buffer and then call `next` on the underlying iterator until it is exhausted.
  • `object` with `__toString()`: If the object has the `__toString()` method, the object will be cast to a string and then a stream will be returned that uses the string value.
  • `NULL`: When `null` is passed, an empty stream object is returned.
  • `callable` When a callable is passed, a read-only stream object will be created that invokes the given callable. The callable is invoked with the number of suggested bytes to read. The callable can return any number of bytes, but MUST return `false` when there is no more data to return. The stream object that wraps the callable will invoke the callable until the number of requested bytes are available. Any additional bytes will be buffered and used in subsequent reads.

Parameters

resource|string|int|float|bool|StreamInterface|callable|\Iterator|null $resource Entity body data:

array{size?: int, metadata?: array} $options Additional options:

Throws

\InvalidArgumentException if the $resource arg is not valid.

10 calls to Utils::streamFor()
CurlFactory::applyHandlerOptions in vendor/guzzlehttp/guzzle/src/Handler/CurlFactory.php
HttpFactory::createStream in vendor/guzzlehttp/psr7/src/HttpFactory.php
Create a new stream from a string.
HttpFactory::createStreamFromFile in vendor/guzzlehttp/psr7/src/HttpFactory.php
Create a stream from an existing file.
HttpFactory::createStreamFromResource in vendor/guzzlehttp/psr7/src/HttpFactory.php
Create a new stream from an existing resource.
LazyOpenStream::createStream in vendor/guzzlehttp/psr7/src/LazyOpenStream.php
Creates the underlying stream lazily when required.

... See full list

File

vendor/guzzlehttp/psr7/src/Utils.php, line 301

Class

Utils

Namespace

GuzzleHttp\Psr7

Code

public static function streamFor($resource = '', array $options = []) : StreamInterface {
    if (is_scalar($resource)) {
        $stream = self::tryFopen('php://temp', 'r+');
        if ($resource !== '') {
            fwrite($stream, (string) $resource);
            fseek($stream, 0);
        }
        return new Stream($stream, $options);
    }
    switch (gettype($resource)) {
        case 'resource':
            
            /*
             * The 'php://input' is a special stream with quirks and inconsistencies.
             * We avoid using that stream by reading it into php://temp
             */
            
            /** @var resource $resource */
            if ((\stream_get_meta_data($resource)['uri'] ?? '') === 'php://input') {
                $stream = self::tryFopen('php://temp', 'w+');
                stream_copy_to_stream($resource, $stream);
                fseek($stream, 0);
                $resource = $stream;
            }
            return new Stream($resource, $options);
        case 'object':
            
            /** @var object $resource */
            if ($resource instanceof StreamInterface) {
                return $resource;
            }
            elseif ($resource instanceof \Iterator) {
                return new PumpStream(function () use ($resource) {
                    if (!$resource->valid()) {
                        return false;
                    }
                    $result = $resource->current();
                    $resource->next();
                    return $result;
                }, $options);
            }
            elseif (method_exists($resource, '__toString')) {
                return self::streamFor((string) $resource, $options);
            }
            break;
        case 'NULL':
            return new Stream(self::tryFopen('php://temp', 'r+'), $options);
    }
    if (is_callable($resource)) {
        return new PumpStream($resource, $options);
    }
    throw new \InvalidArgumentException('Invalid resource type: ' . gettype($resource));
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal