From 8c70a9c8ecdb86b2f75f9ec1c2120e930854bdeb Mon Sep 17 00:00:00 2001 From: Toon Verwerft Date: Wed, 22 Jan 2025 14:31:23 +0100 Subject: [PATCH] Add common PHP I/O stream factories --- src/Factory/CliStream.php | 57 ++++++++++++++++++++++++++++ src/Factory/IOStream.php | 42 ++++++++++++++++++++ tests/Unit/Factory/CliStreamTest.php | 41 ++++++++++++++++++++ tests/Unit/Factory/IOStreamTest.php | 32 ++++++++++++++++ 4 files changed, 172 insertions(+) create mode 100644 src/Factory/CliStream.php create mode 100644 src/Factory/IOStream.php create mode 100644 tests/Unit/Factory/CliStreamTest.php create mode 100644 tests/Unit/Factory/IOStreamTest.php diff --git a/src/Factory/CliStream.php b/src/Factory/CliStream.php new file mode 100644 index 0000000..aa1119d --- /dev/null +++ b/src/Factory/CliStream.php @@ -0,0 +1,57 @@ + + */ + public static function stdout(): ResourceStream + { + $resource = SafeStreamAction::run( + static fn () => fopen('php://stdout', 'w'), + 'Unable to open file "php://stdout"' + ); + + return new ResourceStream($resource); + } + + /** + * @throws RuntimeException + * + * @return ResourceStream + */ + public static function stdin(): ResourceStream + { + $resource = SafeStreamAction::run( + static fn () => fopen('php://stdin', 'r'), + 'Unable to open file "php://stdin"' + ); + + return new ResourceStream($resource); + } + + /** + * @throws RuntimeException + * + * @return ResourceStream + */ + public static function stderr(): ResourceStream + { + $resource = SafeStreamAction::run( + static fn () => fopen('php://stderr', 'w'), + 'Unable to open file "php://stderr"' + ); + + return new ResourceStream($resource); + } +} diff --git a/src/Factory/IOStream.php b/src/Factory/IOStream.php new file mode 100644 index 0000000..22d4a76 --- /dev/null +++ b/src/Factory/IOStream.php @@ -0,0 +1,42 @@ + + */ + public static function input(): ResourceStream + { + $resource = SafeStreamAction::run( + static fn () => fopen('php://input', FileStream::READ_MODE), + 'Unable to open file "php://input"' + ); + + return new ResourceStream($resource); + } + + /** + * @throws RuntimeException + * + * @return ResourceStream + */ + public static function output(): ResourceStream + { + $resource = SafeStreamAction::run( + static fn () => fopen('php://output', FileStream::WRITE_MODE), + 'Unable to open file "php://output"' + ); + + return new ResourceStream($resource); + } +} diff --git a/tests/Unit/Factory/CliStreamTest.php b/tests/Unit/Factory/CliStreamTest.php new file mode 100644 index 0000000..4c53eac --- /dev/null +++ b/tests/Unit/Factory/CliStreamTest.php @@ -0,0 +1,41 @@ +isOpen()); + self::assertStringContainsString('php://stdin', $stream->uri()); + } + + #[Test] + public function it_can_open_output_cli_stream(): void + { + $stream = CliStream::stdout(); + + self::assertTrue($stream->isOpen()); + self::assertStringContainsString('php://stdout', $stream->uri()); + } + + #[Test] + public function it_can_open_error_cli_stream(): void + { + $stream = CliStream::stderr(); + + self::assertTrue($stream->isOpen()); + self::assertStringContainsString('php://stderr', $stream->uri()); + } +} diff --git a/tests/Unit/Factory/IOStreamTest.php b/tests/Unit/Factory/IOStreamTest.php new file mode 100644 index 0000000..bbe71e0 --- /dev/null +++ b/tests/Unit/Factory/IOStreamTest.php @@ -0,0 +1,32 @@ +isOpen()); + self::assertStringContainsString('php://input', $stream->uri()); + } + + #[Test] + public function it_can_open_output_io_stream(): void + { + $stream = IOStream::output(); + + self::assertTrue($stream->isOpen()); + self::assertStringContainsString('php://output', $stream->uri()); + } +}