Luminova Framework

PHP Luminova: Build Custom PHP Stream Wrappers

Last updated: 2026-11-28 20:57:12

Register a custom PHP stream protocol, inspect its registration, and override callbacks for the operations your wrapper supports.

Extend the abstract Stream Wrapper to connect a URI scheme to PHP functions such as fopen() and file_get_contents(). PHP creates a wrapper instance to handle an operation. The base class tracks protocols by concrete class and provides default callbacks. See the File Stream Wrapper for local files and PHP Evaluation Stream for framework-generated PHP source.


Usage

use Luminova\Storage\StreamWrapper;

$scheme = 'lmv-example-local';

if (StreamWrapper::register($scheme)) {
    try {
        var_dump(StreamWrapper::isRegistered($scheme)); // bool(true)
        echo StreamWrapper::protocol(); // lmv-example-local
    } finally {
        StreamWrapper::unregister($scheme);
    }
}

Class Definition

  • Full namespace: Luminova\Base\AbstractStreamWrapper
  • This class is abstract.

Properties

protected static ?string $protocol = null;

/** @var resource|null */
public mixed $context = null;

PHP supplies $context when a stream context is available. The base registration methods store the active protocol in a separate per-class registry. Use protocol() to read it.


Methods

__construct

Initialize a wrapper instance. The default constructor has no setup logic.

public function __construct()

register

Register the calling class for a protocol. If PHP already has that protocol, return whether the calling class tracks it. Successful new registrations set the active protocol for the class.

public static function register(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true on success, or false on failure.


reassign

Unregister an existing wrapper for the protocol if present, then register the calling class. Use this when you intend to replace a wrapper. If the second registration fails, the previous wrapper is already removed.

public static function reassign(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true on success, or false on failure.


unregister

Remove the protocol from PHP’s registry. On success, untrack it for the calling class.

public static function unregister(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true on success, or false on failure.


untrack

Remove a protocol from the calling class’s tracking data. Clear the active protocol if it matches. PHP’s registration stays in place.

public static function untrack(string $protocol): void

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

restore

Restore a built-in PHP wrapper and record the restored protocol in the calling class’s registry. A tracked name therefore does not prove which PHP class handles it.

public static function restore(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true on success, or false on failure.


scheme

Parse a wrapper URL into its protocol and the portion after ://. This helper does not verify registration.

protected static function scheme(string $path): array|false

Parameters:

ParameterTypeDescription
$pathstringStream URL.

Return:

array{protocol:string,uri:string}|false - Parsed protocol and URI, or false if the URL does not match.


isRegistered

Check whether PHP has the protocol and the calling class tracks its name.

public static function isRegistered(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true if both checks pass.


hasProtocol

Check the PHP runtime’s list of registered wrappers.

public static function hasProtocol(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true if PHP lists the protocol.


isTracked

Check the calling class’s registry without consulting PHP.

public static function isTracked(string $protocol): bool

Parameters:

ParameterTypeDescription
$protocolstringProtocol name without ://.

Return:

bool - true if the protocol is tracked.


wrappers

List stream protocols registered in the current PHP runtime.

public static function wrappers(): array

Return:

string[] - Available protocol names.


protocols

Read all protocols tracked for the calling class.

public static function protocols(): array

Return:

array<string,true> - Names keyed to true, or an empty array.


protocol

Read the active protocol stored in the calling class’s registry.

public static function protocol(): ?string

Return:

string|null - Active protocol name or null.


updateRegistry

Update the calling class’s registry without changing PHP’s wrapper registrations. To set the active protocol use protocol with a string or null. To merge tracked names use protocols with an array.

public static function updateRegistry(
    string $context,
    array|string|null $value = null
): bool

Parameters:

ParameterTypeDescription
$contextstringRegistry key, either protocol or protocols.
$valuearray<string,true>\|string\|nullNew active name, map of tracked names, or null to clear the active name (default: null).

Return:

bool - true when updated, or false for an unknown key or a null protocol map.

Throws:

  • TypeError if a string is supplied as the protocols map.

Stream Callbacks

Override the callbacks needed for your wrapper. The default results below describe the base implementations.

stream_open

Open the stream.

public function stream_open(
    string $path,
    string $mode,
    int $options = 0,
    ?string &$openedPath = null
): bool

Parameters:

ParameterTypeDescription
$pathstringStream URL.
$modestringPHP access mode.
$optionsintStream opening flags (default: 0).
$openedPathstring\|nullOutput argument for the resolved path (default: null).

Return:

bool - The base implementation returns false.


stream_read

Read bytes from the stream.

public function stream_read(int $count): string|false

Parameters:

ParameterTypeDescription
$countintMaximum bytes to read.

Return:

string|false - The base implementation returns false.


stream_write

Write bytes to the stream.

public function stream_write(string $data): int

Parameters:

ParameterTypeDescription
$datastringBytes to write.

Return:

int - The base implementation returns 0.


stream_eof

Report whether the stream has ended.

public function stream_eof(): bool

Return:

bool - The base implementation returns true.


stream_tell

Report the current byte offset.

public function stream_tell(): int

Return:

int - The base implementation returns 0.


stream_seek

Move the stream cursor.

public function stream_seek(
    int $offset,
    int $whence = SEEK_SET
): bool

Parameters:

ParameterTypeDescription
$offsetintByte offset.
$whenceintSeek origin, such as SEEK_SET, SEEK_CUR, or SEEK_END (default: SEEK_SET).

Return:

bool - The base implementation returns false.


stream_close

Close the stream. The base callback performs no action.

public function stream_close(): void

stream_flush

Flush buffered writes.

public function stream_flush(): bool

Return:

bool - The base implementation returns true.


stream_stat

Read metadata for an open stream.

public function stream_stat(): array|false

Return:

array<int|string,int>|false - Override to supply stat fields. The base implementation returns false.


stream_cast

Provide a native stream resource when PHP requests one.

public function stream_cast(int $as): mixed

Parameters:

ParameterTypeDescription
$asintPHP stream cast mode.

Return:

mixed - The base implementation returns false.


stream_set_option

Handle a stream option change.

public function stream_set_option(
    int $option,
    int $arg1,
    int $arg2
): bool

Parameters:

ParameterTypeDescription
$optionintOption identifier.
$arg1intFirst option-specific argument.
$arg2intSecond option-specific argument.

Return:

bool - The base implementation returns false.


stream_lock

Lock or unlock the stream.

public function stream_lock(int $operation): bool

Parameters:

ParameterTypeDescription
$operationintPHP lock operation flags.

Return:

bool - The base implementation returns false.


stream_truncate

Change the stream length.

public function stream_truncate(int $newSize): bool

Parameters:

ParameterTypeDescription
$newSizeintTarget size in bytes.

Return:

bool - The base implementation returns false.


url_stat

Read metadata for a URL.

public function url_stat(string $path, int $flags): array|false

Parameters:

ParameterTypeDescription
$pathstringStream URL.
$flagsintMetadata request flags.

Return:

array<int|string,int>|false - Override to supply stat fields. The base implementation returns false.


Delete a file.

public function unlink(string $path): bool

Parameters:

ParameterTypeDescription
$pathstringStream URL.

Return:

bool - The base implementation returns false.


rename

Rename or move a file or directory.

public function rename(string $from, string $to): bool

Parameters:

ParameterTypeDescription
$fromstringSource URL.
$tostringDestination URL.

Return:

bool - The base implementation returns false.


mkdir

Create a directory.

public function mkdir(
    string $path,
    int $mode = 0775,
    int $options = 0
): bool

Parameters:

ParameterTypeDescription
$pathstringStream URL.
$modeintDirectory mode (default: 0775).
$optionsintCreation flags such as STREAM_MKDIR_RECURSIVE (default: 0).

Return:

bool - The base implementation returns false.


rmdir

Remove a directory.

public function rmdir(string $path, int $options = 0): bool

Parameters:

ParameterTypeDescription
$pathstringStream URL.
$optionsintRemoval flags passed by PHP (default: 0).

Return:

bool - The base implementation returns false.


copy

Provide an explicit copy operation for subclasses.

public function copy(string $from, string $to): bool

Parameters:

ParameterTypeDescription
$fromstringSource URL.
$tostringDestination URL.

Return:

bool - The base implementation returns false.


stream_metadata

Change file or directory metadata.

public function stream_metadata(
    string $path,
    int $option,
    mixed $value
): bool

Parameters:

ParameterTypeDescription
$pathstringStream URL.
$optionintMetadata operation identifier.
$valuemixedValue for the metadata operation.

Return:

bool - The base implementation returns false.


dir_opendir

Open a directory for iteration.

public function dir_opendir(string $path, int $options = 0): bool

Parameters:

ParameterTypeDescription
$pathstringStream URL.
$optionsintDirectory opening flags (default: 0).

Return:

bool - The base implementation returns false.


dir_readdir

Read the next directory entry.

public function dir_readdir(): string|false

Return:

string|false - The base implementation returns false.


dir_rewinddir

Reset the directory cursor.

public function dir_rewinddir(): bool

Return:

bool - The base implementation returns false.


dir_closedir

Close the directory handle.

public function dir_closedir(): bool

Return:

bool - The base implementation returns true.