Преглед изворни кода

fix: make ResponseStream compatible with psr/http-message 2.0

psr/http-message 2.0 added native return types to StreamInterface.
ResponseStream::tell() and write() returned false on failure, which is a
fatal signature mismatch against the v2 interface once any consumer pulls
psr/http-message 2.0. Throw RuntimeException instead, as required by
PSR-7, and guard fread/stream_get_contents against false returns.
Also fix getMetadata(null) to return the full metadata array.
Ne-Lexa пре 3 недеља
родитељ
комит
fbd2420a94
1 измењених фајлова са 35 додато и 10 уклоњено
  1. 35 10
      src/IO/Stream/ResponseStream.php

+ 35 - 10
src/IO/Stream/ResponseStream.php

@@ -92,17 +92,15 @@ class ResponseStream implements StreamInterface
 
     /**
      * {@inheritDoc}
-     *
-     * @noinspection PhpMissingReturnTypeInspection
      */
     public function getMetadata($key = null)
     {
         if ($this->stream === null) {
-            return $key ? null : [];
+            return $key === null ? [] : null;
         }
         $meta = stream_get_meta_data($this->stream);
 
-        return $meta[$key] ?? null;
+        return $key === null ? $meta : ($meta[$key] ?? null);
     }
 
     /**
@@ -173,9 +171,18 @@ class ResponseStream implements StreamInterface
         return null;
     }
 
-    public function tell()
+    public function tell(): int
     {
-        return $this->stream ? ftell($this->stream) : false;
+        if ($this->stream === null) {
+            throw new \RuntimeException('Stream is detached');
+        }
+        $position = ftell($this->stream);
+
+        if ($position === false) {
+            throw new \RuntimeException('Unable to determine stream position');
+        }
+
+        return $position;
     }
 
     /**
@@ -213,11 +220,19 @@ class ResponseStream implements StreamInterface
     /**
      * {@inheritDoc}
      */
-    public function write($string)
+    public function write($string): int
     {
+        if ($this->stream === null || !$this->writable) {
+            throw new \RuntimeException('Stream is not writable');
+        }
         $this->size = null;
+        $written = fwrite($this->stream, $string);
+
+        if ($written === false) {
+            throw new \RuntimeException('Unable to write to stream');
+        }
 
-        return $this->stream !== null && $this->writable ? fwrite($this->stream, $string) : false;
+        return $written;
     }
 
     /**
@@ -233,7 +248,12 @@ class ResponseStream implements StreamInterface
      */
     public function read($length): string
     {
-        return $this->stream !== null && $this->readable ? fread($this->stream, $length) : '';
+        if ($this->stream === null || !$this->readable) {
+            return '';
+        }
+        $buffer = fread($this->stream, $length);
+
+        return $buffer === false ? '' : $buffer;
     }
 
     /**
@@ -244,7 +264,12 @@ class ResponseStream implements StreamInterface
      */
     public function getContents(): string
     {
-        return $this->stream ? stream_get_contents($this->stream) : '';
+        if ($this->stream === null) {
+            return '';
+        }
+        $contents = stream_get_contents($this->stream);
+
+        return $contents === false ? '' : $contents;
     }
 
     /**