krz/vox-populi
A Tumblr web client.
clone: git clone https://gitbay.org/krz/vox-populi.git
main: vendor/guzzlehttp/psr7/src/StreamWrapper.php · raw
1<?php
2namespace GuzzleHttp\Psr7;
3
4use Psr\Http\Message\StreamInterface;
5
6/**
7 * Converts Guzzle streams into PHP stream resources.
8 */
9class StreamWrapper
10{
11 /** @var resource */
12 public $context;
13
14 /** @var StreamInterface */
15 private $stream;
16
17 /** @var string r, r+, or w */
18 private $mode;
19
20 /**
21 * Returns a resource representing the stream.
22 *
23 * @param StreamInterface $stream The stream to get a resource for
24 *
25 * @return resource
26 * @throws \InvalidArgumentException if stream is not readable or writable
27 */
28 public static function getResource(StreamInterface $stream)
29 {
30 self::register();
31
32 if ($stream->isReadable()) {
33 $mode = $stream->isWritable() ? 'r+' : 'r';
34 } elseif ($stream->isWritable()) {
35 $mode = 'w';
36 } else {
37 throw new \InvalidArgumentException('The stream must be readable, '
38 . 'writable, or both.');
39 }
40
41 return fopen('guzzle://stream', $mode, null, self::createStreamContext($stream));
42 }
43
44 /**
45 * Creates a stream context that can be used to open a stream as a php stream resource.
46 *
47 * @param StreamInterface $stream
48 *
49 * @return resource
50 */
51 public static function createStreamContext(StreamInterface $stream)
52 {
53 return stream_context_create([
54 'guzzle' => ['stream' => $stream]
55 ]);
56 }
57
58 /**
59 * Registers the stream wrapper if needed
60 */
61 public static function register()
62 {
63 if (!in_array('guzzle', stream_get_wrappers())) {
64 stream_wrapper_register('guzzle', __CLASS__);
65 }
66 }
67
68 public function stream_open($path, $mode, $options, &$opened_path)
69 {
70 $options = stream_context_get_options($this->context);
71
72 if (!isset($options['guzzle']['stream'])) {
73 return false;
74 }
75
76 $this->mode = $mode;
77 $this->stream = $options['guzzle']['stream'];
78
79 return true;
80 }
81
82 public function stream_read($count)
83 {
84 return $this->stream->read($count);
85 }
86
87 public function stream_write($data)
88 {
89 return (int) $this->stream->write($data);
90 }
91
92 public function stream_tell()
93 {
94 return $this->stream->tell();
95 }
96
97 public function stream_eof()
98 {
99 return $this->stream->eof();
100 }
101
102 public function stream_seek($offset, $whence)
103 {
104 $this->stream->seek($offset, $whence);
105
106 return true;
107 }
108
109 public function stream_cast($cast_as)
110 {
111 $stream = clone($this->stream);
112
113 return $stream->detach();
114 }
115
116 public function stream_stat()
117 {
118 static $modeMap = [
119 'r' => 33060,
120 'rb' => 33060,
121 'r+' => 33206,
122 'w' => 33188,
123 'wb' => 33188
124 ];
125
126 return [
127 'dev' => 0,
128 'ino' => 0,
129 'mode' => $modeMap[$this->mode],
130 'nlink' => 0,
131 'uid' => 0,
132 'gid' => 0,
133 'rdev' => 0,
134 'size' => $this->stream->getSize() ?: 0,
135 'atime' => 0,
136 'mtime' => 0,
137 'ctime' => 0,
138 'blksize' => 0,
139 'blocks' => 0
140 ];
141 }
142
143 public function url_stat($path, $flags)
144 {
145 return [
146 'dev' => 0,
147 'ino' => 0,
148 'mode' => 0,
149 'nlink' => 0,
150 'uid' => 0,
151 'gid' => 0,
152 'rdev' => 0,
153 'size' => 0,
154 'atime' => 0,
155 'mtime' => 0,
156 'ctime' => 0,
157 'blksize' => 0,
158 'blocks' => 0
159 ];
160 }
161}