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