Debian Codegraph @

vendor/guzzlehttp/psr7/src/StreamWrapper.php

raw
1 <?php
2
3 declare(strict_types=1);
4
5 namespace GuzzleHttp\Psr7;
6
7 use Psr\Http\Message\StreamInterface;
8
9 /**
10 * Converts Guzzle streams into PHP stream resources.
11 *
12 * @see https://www.php.net/streamwrapper
13 */
14 final class StreamWrapper
15 {
16 /** @var resource */
17 public $context;
18
19 /** @var StreamInterface */
20 private $stream;
21
22 /** @var string r, r+, or w */
23 private $mode;
24
25 /**
26 * Returns a resource representing the stream.
27 *
28 * @param StreamInterface $stream The stream to get a resource for
29 *
30 * @return resource
31 *
32 * @throws \InvalidArgumentException if stream is not readable or writable
33 */
34 public static function getResource(StreamInterface $stream)
35 {
36 self::register();
37
38 if ($stream->isReadable()) {
39 $mode = $stream->isWritable() ? 'r+' : 'r';
40 } elseif ($stream->isWritable()) {
41 $mode = 'w';
42 } else {
43 throw new \InvalidArgumentException('The stream must be readable, '
44 .'writable, or both.');
45 }
46
47 $resource = @fopen('guzzle://stream', $mode, false, self::createStreamContext($stream));
48
49 if ($resource === false) {
50 throw new \RuntimeException('Unable to create stream resource');
51 }
52
53 return $resource;
54 }
55
56 /**
57 * Creates a stream context that can be used to open a stream as a php stream resource.
58 *
59 * @return resource
60 */
61 public static function createStreamContext(StreamInterface $stream)
62 {
63 return stream_context_create([
64 'guzzle' => ['stream' => $stream],
65 ]);
66 }
67
68 /**
69 * Registers the stream wrapper if needed
70 */
71 public static function register(): void
72 {
73 if (!in_array('guzzle', stream_get_wrappers())) {
74 stream_wrapper_register('guzzle', __CLASS__);
75 }
76 }
77
78 public function stream_open(string $path, string $mode, int $options, ?string &$opened_path = null): bool
79 {
80 $options = stream_context_get_options($this->context);
81
82 if (!isset($options['guzzle']['stream'])) {
83 return false;
84 }
85
86 $this->mode = $mode;
87 $this->stream = $options['guzzle']['stream'];
88
89 return true;
90 }
91
92 public function stream_read(int $count): string
93 {
94 return $this->stream->read($count);
95 }
96
97 public function stream_write(string $data): int
98 {
99 return $this->stream->write($data);
100 }
101
102 public function stream_tell(): int
103 {
104 return $this->stream->tell();
105 }
106
107 public function stream_eof(): bool
108 {
109 return $this->stream->eof();
110 }
111
112 public function stream_seek(int $offset, int $whence): bool
113 {
114 $this->stream->seek($offset, $whence);
115
116 return true;
117 }
118
119 /**
120 * @return resource|false
121 */
122 public function stream_cast(int $cast_as)
123 {
124 $stream = clone $this->stream;
125 $resource = $stream->detach();
126
127 return $resource ?? false;
128 }
129
130 /**
131 * @return array{
132 * dev: int,
133 * ino: int,
134 * mode: int,
135 * nlink: int,
136 * uid: int,
137 * gid: int,
138 * rdev: int,
139 * size: int,
140 * atime: int,
141 * mtime: int,
142 * ctime: int,
143 * blksize: int,
144 * blocks: int
145 * }|false
146 */
147 public function stream_stat()
148 {
149 if ($this->stream->getSize() === null) {
150 return false;
151 }
152
153 static $modeMap = [
154 'r' => 33060,
155 'rb' => 33060,
156 'r+' => 33206,
157 'w' => 33188,
158 'wb' => 33188,
159 ];
160
161 return [
162 'dev' => 0,
163 'ino' => 0,
164 'mode' => $modeMap[$this->mode],
165 'nlink' => 0,
166 'uid' => 0,
167 'gid' => 0,
168 'rdev' => 0,
169 'size' => $this->stream->getSize() ?: 0,
170 'atime' => 0,
171 'mtime' => 0,
172 'ctime' => 0,
173 'blksize' => 0,
174 'blocks' => 0,
175 ];
176 }
177
178 /**
179 * @return array{
180 * dev: int,
181 * ino: int,
182 * mode: int,
183 * nlink: int,
184 * uid: int,
185 * gid: int,
186 * rdev: int,
187 * size: int,
188 * atime: int,
189 * mtime: int,
190 * ctime: int,
191 * blksize: int,
192 * blocks: int
193 * }
194 */
195 public function url_stat(string $path, int $flags): array
196 {
197 return [
198 'dev' => 0,
199 'ino' => 0,
200 'mode' => 0,
201 'nlink' => 0,
202 'uid' => 0,
203 'gid' => 0,
204 'rdev' => 0,
205 'size' => 0,
206 'atime' => 0,
207 'mtime' => 0,
208 'ctime' => 0,
209 'blksize' => 0,
210 'blocks' => 0,
211 ];
212 }
213 }