From 5725a40a96a6008d123d2d957c97da5d37780c91 Mon Sep 17 00:00:00 2001 From: Tyler Bannister Date: Wed, 11 Jan 2023 13:45:22 -0500 Subject: [PATCH] Add destructor to restore original blocking status Restoring the original blocking status to prevent side effects. --- src/IO/ResourceInputStream.php | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/IO/ResourceInputStream.php b/src/IO/ResourceInputStream.php index 7582df1..4a23e8f 100644 --- a/src/IO/ResourceInputStream.php +++ b/src/IO/ResourceInputStream.php @@ -17,6 +17,11 @@ class ResourceInputStream implements InputStream */ private $stream; + /** + * @var bool Original blocking state. + */ + private $blocking; + public function __construct($stream = STDIN) { if (!is_resource($stream) || get_resource_type($stream) !== 'stream') { @@ -28,9 +33,18 @@ public function __construct($stream = STDIN) throw new \InvalidArgumentException('Expected a readable stream'); } + $meta = stream_get_meta_data($stream); + $this->blocking = $meta['blocked']; $this->stream = $stream; } + /** + * Restore the blocking state. + */ + public function __destruct() { + stream_set_blocking($this->stream, $this->blocking); + } + public function read(int $numBytes, callable $callback) : void { $buffer = fread($this->stream, $numBytes);