Documentation

StreamUtil
in package

FinalYes

Table of Contents

Methods

copyToStream()  : int
Copies a stream to another stream, starting from the current position of the source stream, reading to the end or until the given maxlength is hit.
getContents()  : string|null
Reads the content from a stream and make sure we rewind
modeAllowsRead()  : bool
Checks whether the given mode allows reading
modeAllowsReadOnly()  : bool
Checks whether the given mode allows only reading
modeAllowsReadWrite()  : bool
Checks whether the given mode allows reading and writing
modeAllowsWrite()  : bool
Checks whether the given mode allows writing
modeAllowsWriteOnly()  : bool
Checks whether the given mode allows only writing
tryFopen()  : resource
Safely open a PHP resource, throws instead of raising warnings and errors
tryGetContents()  : string
Safely get the contents of a stream resource, throws instead of raising warnings and errors
validateMode()  : string
Checks if the given mode is valid for fopen().

Methods

copyToStream()

Copies a stream to another stream, starting from the current position of the source stream, reading to the end or until the given maxlength is hit.

public static copyToStream(StreamInterface $source, StreamInterface $destination[, int|null $maxLength = null ]) : int

Throws if the source is not readable or the destination not writable.

Parameters
$source : StreamInterface
$destination : StreamInterface
$maxLength : int|null = null
Tags
see
https://github.com/guzzle/psr7/blob/815698d9f11c908bc59471d11f642264b533346a/src/Utils.php#L36-L69
throws
RuntimeException
Return values
int

getContents()

Reads the content from a stream and make sure we rewind

public static getContents(StreamInterface $stream) : string|null

Returns the stream content as a string, null if an error occurs, e.g. the StreamInterface throws.

Parameters
$stream : StreamInterface
Return values
string|null

modeAllowsRead()

Checks whether the given mode allows reading

public static modeAllowsRead(string $mode) : bool
Parameters
$mode : string
Return values
bool

modeAllowsReadOnly()

Checks whether the given mode allows only reading

public static modeAllowsReadOnly(string $mode) : bool
Parameters
$mode : string
Return values
bool

modeAllowsReadWrite()

Checks whether the given mode allows reading and writing

public static modeAllowsReadWrite(string $mode) : bool
Parameters
$mode : string
Return values
bool

modeAllowsWrite()

Checks whether the given mode allows writing

public static modeAllowsWrite(string $mode) : bool
Parameters
$mode : string
Return values
bool

modeAllowsWriteOnly()

Checks whether the given mode allows only writing

public static modeAllowsWriteOnly(string $mode) : bool
Parameters
$mode : string
Return values
bool

validateMode()

Checks if the given mode is valid for fopen().

public static validateMode(string $mode) : string

Returns the first 15 characters, throws if that string doesn't match the pattern.

Note: we don't care where the modifier flags are in the string, what matters is that the first character is one of "acrwx" and the rest may contain one of "bet+" from 2nd position onwards, so "aaaaaaaaaaaaaa+b" is valid.

The documentation of fopen() says that the text-mode translation flag (b/t) should be added as last character, however, it doesn't matter as PHP internally only reads the mode from the first character and 15 characters total. and does a strchr() on it for the flags, so technically "rb+" is equivalent to "r+b" and "rrrbbbb++". Also, some libraries allow a mode "rw" which is wrong and just falls back to "r" - see above. (looking at you, Guzzle)

gzopen() adds a bunch of other flags that are hardly documented, so we'll ignore these until we get a full list.

Parameters
$mode : string
Tags
see
https://www.php.net/manual/en/function.fopen
see
https://www.php.net/manual/en/function.gzopen.php
see
https://stackoverflow.com/a/44483367/3185624
see
https://github.com/php/php-src/blob/6602ddead5c81fb67ebf2b21c32b58aa1de67699/main/streams/plain_wrapper.c#L71-L121
see
https://github.com/guzzle/psr7/blob/815698d9f11c908bc59471d11f642264b533346a/src/Stream.php#L19
throws
InvalidArgumentException
Return values
string

        
On this page

Search results