PHP Luminova: Build Custom PHP Stream Wrappers
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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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): voidParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$protocol | string | Protocol name without ://. |
Return:
bool - true if the protocol is tracked.
wrappers
List stream protocols registered in the current PHP runtime.
public static function wrappers(): arrayReturn:
string[] - Available protocol names.
protocols
Read all protocols tracked for the calling class.
public static function protocols(): arrayReturn:
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(): ?stringReturn:
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
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$context | string | Registry key, either protocol or protocols. |
$value | array<string,true>\|string\|null | New 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:
TypeErrorif a string is supplied as theprotocolsmap.
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
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
$mode | string | PHP access mode. |
$options | int | Stream opening flags (default: 0). |
$openedPath | string\|null | Output 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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$count | int | Maximum bytes to read. |
Return:
string|false - The base implementation returns false.
stream_write
Write bytes to the stream.
public function stream_write(string $data): intParameters:
| Parameter | Type | Description |
|---|---|---|
$data | string | Bytes to write. |
Return:
int - The base implementation returns 0.
stream_eof
Report whether the stream has ended.
public function stream_eof(): boolReturn:
bool - The base implementation returns true.
stream_tell
Report the current byte offset.
public function stream_tell(): intReturn:
int - The base implementation returns 0.
stream_seek
Move the stream cursor.
public function stream_seek(
int $offset,
int $whence = SEEK_SET
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$offset | int | Byte offset. |
$whence | int | Seek 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(): voidstream_flush
Flush buffered writes.
public function stream_flush(): boolReturn:
bool - The base implementation returns true.
stream_stat
Read metadata for an open stream.
public function stream_stat(): array|falseReturn:
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): mixedParameters:
| Parameter | Type | Description |
|---|---|---|
$as | int | PHP 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
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$option | int | Option identifier. |
$arg1 | int | First option-specific argument. |
$arg2 | int | Second option-specific argument. |
Return:
bool - The base implementation returns false.
stream_lock
Lock or unlock the stream.
public function stream_lock(int $operation): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$operation | int | PHP lock operation flags. |
Return:
bool - The base implementation returns false.
stream_truncate
Change the stream length.
public function stream_truncate(int $newSize): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$newSize | int | Target 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|falseParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
$flags | int | Metadata request flags. |
Return:
array<int|string,int>|false - Override to supply stat fields. The base implementation returns false.
unlink
Delete a file.
public function unlink(string $path): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
Return:
bool - The base implementation returns false.
rename
Rename or move a file or directory.
public function rename(string $from, string $to): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$from | string | Source URL. |
$to | string | Destination URL. |
Return:
bool - The base implementation returns false.
mkdir
Create a directory.
public function mkdir(
string $path,
int $mode = 0775,
int $options = 0
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
$mode | int | Directory mode (default: 0775). |
$options | int | Creation 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
$options | int | Removal 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$from | string | Source URL. |
$to | string | Destination 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
): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
$option | int | Metadata operation identifier. |
$value | mixed | Value 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): boolParameters:
| Parameter | Type | Description |
|---|---|---|
$path | string | Stream URL. |
$options | int | Directory opening flags (default: 0). |
Return:
bool - The base implementation returns false.
dir_readdir
Read the next directory entry.
public function dir_readdir(): string|falseReturn:
string|false - The base implementation returns false.
dir_rewinddir
Reset the directory cursor.
public function dir_rewinddir(): boolReturn:
bool - The base implementation returns false.
dir_closedir
Close the directory handle.
public function dir_closedir(): boolReturn:
bool - The base implementation returns true.