2 namespace GuzzleHttp\Psr7;
4 use Psr\Http\Message\StreamInterface;
7 * Converts Guzzle streams into PHP stream resources.
14 /** @var StreamInterface */
17 /** @var string r, r+, or w */
21 * Returns a resource representing the stream.
23 * @param StreamInterface $stream The stream to get a resource for
26 * @throws \InvalidArgumentException if stream is not readable or writable
28 public static function getResource(StreamInterface $stream)
32 if ($stream->isReadable()) {
33 $mode = $stream->isWritable() ? 'r+' : 'r';
34 } elseif ($stream->isWritable()) {
37 throw new \InvalidArgumentException('The stream must be readable, '
38 . 'writable, or both.');
41 return fopen('guzzle://stream', $mode, null, stream_context_create([
42 'guzzle' => ['stream' => $stream]
47 * Registers the stream wrapper if needed
49 public static function register()
51 if (!in_array('guzzle', stream_get_wrappers())) {
52 stream_wrapper_register('guzzle', __CLASS__);
56 public function stream_open($path, $mode, $options, &$opened_path)
58 $options = stream_context_get_options($this->context);
60 if (!isset($options['guzzle']['stream'])) {
65 $this->stream = $options['guzzle']['stream'];
70 public function stream_read($count)
72 return $this->stream->read($count);
75 public function stream_write($data)
77 return (int) $this->stream->write($data);
80 public function stream_tell()
82 return $this->stream->tell();
85 public function stream_eof()
87 return $this->stream->eof();
90 public function stream_seek($offset, $whence)
92 $this->stream->seek($offset, $whence);
97 public function stream_stat()
108 'mode' => $modeMap[$this->mode],
113 'size' => $this->stream->getSize() ?: 0,