Skip to content

PSR 17: StreamFactory Example

Terry L edited this page Jun 21, 2020 · 3 revisions
Clone this wiki locally

PSR 17 Factories

Namespace

Shieldon\Psr17\StreamFactory

StreamFactory

__construct

None

createStream($content)

  • param string content = "" String content with which to populate the stream.
  • return StreamInterface

Example:

$streamFactory = new StreamFactory();
$stream = $streamFactory->createStream('Foo Bar');

echo $stream;
// Outputs: Foo Bar

createStreamFromFile($filename, $mode)

  • param string filename * The filename or stream URI to use as basis of stream.
  • param string mode r The mode with which to open the underlying filename/stream.
  • return StreamInterface

Example:

$sourceFile = BOOTSTRAP_DIR . '/sample/shieldon_logo.png';

$streamFactory = new StreamFactory();
$stream = $streamFactory->createStreamFromFile($sourceFile);

echo $stream->getSize();
// Outputs: 15166

createStreamFromResource($resource)

  • param string resource * The PHP resource to use as the basis for the stream.
  • return StreamInterface

Example:

$streamFactory = new StreamFactory();
$stream = $streamFactory->createStreamResource(
    fopen('php://temp', 'r+')
);