diff --git a/CHANGELOG.md b/CHANGELOG.md
index 1c83911..d8e4194 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,7 +5,12 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
-# [[v0.5-0](https://github.com/The-FireHub-Project/Runtime/compare/v0.4.3...v0.5.0)] – 2026-11-04
+# [[v0.5.1](https://github.com/The-FireHub-Project/Runtime/compare/v0.5.0...v0.5.1)] – 2026-11-04
+
+### Added
+* Add comprehensive stream utilities in `Runtime` by @dieselxxx
+
+# [[v0.5.0](https://github.com/The-FireHub-Project/Runtime/compare/v0.4.3...v0.5.0)] – 2026-11-04
### Added
* Add comprehensive stream utilities in `Runtime` by @dieselxxx
diff --git a/README.md b/README.md
index 1e21618..f3ff069 100644
--- a/README.md
+++ b/README.md
@@ -7,12 +7,12 @@
PHP Runtime Foundation
@@ -22,25 +22,46 @@
/>
-[](https://github.com/The-FireHub-Project/Runtime/actions/workflows/PHPStan.yml)
-[](https://github.com/The-FireHub-Project/Runtime/actions/workflows/PHPUnit.yml)
-[](https://app.codecov.io/gh/The-FireHub-Project/Runtime/tree/develop)
-
-
+
+
+
-
+
-
+
+
+
+
+
+
+
+
+
+ * File path or supported stream URI. + *
+ * @param \FireHub\Runtime\Stream\Enum\OpenMode $mode [optional]+ * Stream opening mode. + *
+ * @param bool $use_include_path [optional]+ * Whether to search the configured include path. + *
+ * @param null|resource $context [optional]+ * Stream context resource. + *
+ * + * @return resource|false Opened stream resource, or false on failure. + */ + public static function open (string $path, OpenMode $mode, bool $use_include_path = false, mixed $context = null):mixed { + + return fopen($path, $mode->value, $use_include_path, $context); + + } + + /** + * ### Closes an open stream + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to close. + *
+ * + * @return bool True on success, false on failure. + */ + public static function close (mixed $stream):bool { + + return fclose($stream); + + } + +} \ No newline at end of file diff --git a/src/Stream/Lock.php b/src/Stream/Lock.php new file mode 100644 index 0000000..ca7ea0e --- /dev/null +++ b/src/Stream/Lock.php @@ -0,0 +1,63 @@ + + * @copyright 2026-present The FireHub Project - All rights reserved + * @license https://opensource.org/license/Apache-2-0 Apache License, Version 2.0 + * + * @php-version >=8.2 + * @package Runtime + */ + +namespace FireHub\Runtime\Stream; + +use FireHub\Core\Boundary\Runtime\NativeRuntime; +use FireHub\Core\Type\IO\LockMode; + +use function flock; + +/** + * ### PHP Runtime Stream Lock Utilities + * + * Provides low-level wrappers for acquiring and releasing advisory locks on native PHP streams. + * + * Locking behavior depends on the underlying stream wrapper and operating system. + * @since 1.0.0 + */ +final readonly class Lock extends NativeRuntime { + + /** + * ### Performs a stream locking operation + * + * Acquires or releases an advisory lock on a stream. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource. + *
+ * @param \FireHub\Core\Type\IO\LockMode $mode+ * Locking operation. + *
+ * @param bool $non_blocking [optional]+ * Whether to apply the non-blocking modifier. + *
+ * @param null|bool $would_block [optional]+ * Receives whether the lock attempt would block. + *
+ * @param-out int $would_block + * + * @return bool True on success, false on failure. + */ + public static function operation (mixed $stream, LockMode $mode, bool $non_blocking = false, ?bool &$would_block = null):bool { + + return flock( + $stream, + $mode->value | ($non_blocking ? LOCK_NB : 0), + $would_block + ); + + } + +} \ No newline at end of file diff --git a/src/Stream/Metadata.php b/src/Stream/Metadata.php index 0cdde71..c55b275 100644 --- a/src/Stream/Metadata.php +++ b/src/Stream/Metadata.php @@ -15,6 +15,7 @@ use FireHub\Core\Boundary\Runtime\NativeRuntime; +use function fstat; use function stream_get_meta_data; use function stream_get_transports; @@ -67,4 +68,24 @@ public static function transports ():array { } + /** + * ### Gets stream resource statistics + * + * Retrieves information about the resource underlying the stream. + * + * The returned array may contain a device, inode, mode, size, timestamps, and other filesystem-related information. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource. + *
+ * + * @return array+ * Stream resource. + *
+ * @param int $offset+ * Position offset in bytes. + *
+ * @param \FireHub\Core\Type\IO\SeekOrigin $origin [optional]+ * Reference position. + *
+ * + * @return int Zero on success, negative one on failure. + */ + public static function seek (mixed $stream, int $offset, SeekOrigin $origin = SeekOrigin::START):int { + + return fseek($stream, $offset, $origin->value); + + } + + /** + * ### Gets the current stream position + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource. + *
+ * + * @return int|false Current position, or false on failure. + */ + public static function tell (mixed $stream):int|false { + + return ftell($stream); + + } + + /** + * ### Rewinds the stream + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource. + *
+ * + * @return bool True on success, false on failure. + */ + public static function rewind (mixed $stream):bool { + + return rewind($stream); + + } + + /** + * ### Determines whether a stream has reached EOF + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource. + *
+ * + * @return bool True if EOF has been reached. + */ + public static function eof (mixed $stream):bool { + + return feof($stream); + + } + +} \ No newline at end of file diff --git a/src/Stream/Read.php b/src/Stream/Read.php index 0ed5903..5cdbf38 100644 --- a/src/Stream/Read.php +++ b/src/Stream/Read.php @@ -15,6 +15,11 @@ use FireHub\Core\Boundary\Runtime\NativeRuntime; +use function fread; +use function fgets; +use function fgetc; +use function fpassthru; +use function fscanf; use function stream_get_contents; use function stream_get_line; @@ -86,4 +91,105 @@ public static function line (mixed $stream, int $length, string $delimiter = '') } + /** + * ### Reads bytes from a stream + * + * Reads up to the specified number of bytes from the current stream position. + * + * The returned data may contain fewer bytes than requested. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to read from. + *
+ * @param positive-int $length+ * Maximum number of bytes to read. + *
+ * + * @return string|false Read data, or false on failure. + */ + public static function bytes (mixed $stream, int $length):string|false { + + return fread($stream, $length); + + } + + /** + * ### Reads a line from a stream + * + * Reads data until a newline, the specified length, or the end of the stream is reached. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to read from. + *
+ * @param null|positive-int $length [optional]+ * Maximum read length, including space for the terminating byte. + *
+ * + * @return string|false Read line, or false on failure. + */ + public static function lineNative (mixed $stream, ?int $length = null):string|false { + + return fgets($stream, $length); + + } + + /** + * ### Reads a character from a stream + * + * Reads a single byte from the current stream position. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to read from. + *
+ * + * @return string|false Single-byte string, or false on failure. + */ + public static function character (mixed $stream):string|false { + + return fgetc($stream); + + } + + /** + * ### Outputs all remaining data from a stream + * + * Reads from the current stream position until EOF and writes the data directly to the output. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to read from. + *
+ * + * @return int Number of bytes written to the output. + */ + public static function passthrough (mixed $stream):int { + + return fpassthru($stream); + + } + + /** + * ### Parses input from a stream according to a format + * + * Reads a line from the stream and parses its values according to the specified format. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to read from. + *
+ * @param string $format+ * Format string containing conversion specifications. + *
+ * + * @return null|int|list+ * Destination stream resource. + *
+ * @param string $data+ * Data to write. + *
+ * @param null|int<0, max> $length [optional]+ * Maximum number of bytes to write. + *
+ * + * @return non-negative-int|false Number of bytes written, or false on failure. + */ + public static function bytes (mixed $stream, string $data, ?int $length = null):int|false { + + return fwrite($stream, $data, $length); + + } + + /** + * ### Flushes buffered output to a stream + * + * Flushes PHP's buffered output for the specified stream. + * + * This operation does not guarantee that data has been physically synchronized to persistent storage. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to flush. + *
+ * + * @return bool True on success, false on failure. + */ + public static function flush (mixed $stream):bool { + + return fflush($stream); + + } + + /** + * ### Truncates a stream + * + * Changes the stream size to the specified number of bytes. + * + * The current stream position is not changed. + * @since 1.0.0 + * + * @param resource $stream+ * Stream resource to truncate. + *
+ * @param non-negative-int $size+ * New stream size in bytes. + *
+ * + * @return bool True on success, false on failure. + */ + public static function truncate (mixed $stream, int $size):bool { + + return ftruncate($stream, $size); + + } + +} \ No newline at end of file