1<?php
2namespace GuzzleHttp\Stream;
3
4/**
5 * Converts Guzzle streams into PHP stream resources.
6 */
7class GuzzleStreamWrapper
8{
9    /** @var resource */
10    public $context;
11
12    /** @var StreamInterface */
13    private $stream;
14
15    /** @var string r, r+, or w */
16    private $mode;
17
18    /**
19     * Returns a resource representing the stream.
20     *
21     * @param StreamInterface $stream The stream to get a resource for
22     *
23     * @return resource
24     * @throws \InvalidArgumentException if stream is not readable or writable
25     */
26    public static function getResource(StreamInterface $stream)
27    {
28        self::register();
29
30        if ($stream->isReadable()) {
31            $mode = $stream->isWritable() ? 'r+' : 'r';
32        } elseif ($stream->isWritable()) {
33            $mode = 'w';
34        } else {
35            throw new \InvalidArgumentException('The stream must be readable, '
36                . 'writable, or both.');
37        }
38
39        return fopen('guzzle://stream', $mode, null, stream_context_create([
40            'guzzle' => ['stream' => $stream],
41        ]));
42    }
43
44    /**
45     * Registers the stream wrapper if needed
46     */
47    public static function register()
48    {
49        if (!in_array('guzzle', stream_get_wrappers())) {
50            stream_wrapper_register('guzzle', __CLASS__);
51        }
52    }
53
54    public function stream_open($path, $mode, $options, &$opened_path)
55    {
56        $options = stream_context_get_options($this->context);
57
58        if (!isset($options['guzzle']['stream'])) {
59            return false;
60        }
61
62        $this->mode = $mode;
63        $this->stream = $options['guzzle']['stream'];
64
65        return true;
66    }
67
68    public function stream_read($count)
69    {
70        return $this->stream->read($count);
71    }
72
73    public function stream_write($data)
74    {
75        return (int) $this->stream->write($data);
76    }
77
78    public function stream_tell()
79    {
80        return $this->stream->tell();
81    }
82
83    public function stream_eof()
84    {
85        return $this->stream->eof();
86    }
87
88    public function stream_seek($offset, $whence)
89    {
90        return $this->stream->seek($offset, $whence);
91    }
92
93    public function stream_stat()
94    {
95        static $modeMap = [
96            'r'  => 33060,
97            'r+' => 33206,
98            'w'  => 33188,
99        ];
100
101        return [
102            'dev'     => 0,
103            'ino'     => 0,
104            'mode'    => $modeMap[$this->mode],
105            'nlink'   => 0,
106            'uid'     => 0,
107            'gid'     => 0,
108            'rdev'    => 0,
109            'size'    => $this->stream->getSize() ?: 0,
110            'atime'   => 0,
111            'mtime'   => 0,
112            'ctime'   => 0,
113            'blksize' => 0,
114            'blocks'  => 0
115        ];
116    }
117}
118