30
30
use function is_resource ;
31
31
use function is_string ;
32
32
use function sprintf ;
33
- use function str_contains ;
34
33
use function stream_context_get_options ;
35
34
use function stream_get_wrappers ;
36
35
use function stream_wrapper_register ;
37
36
use function stream_wrapper_unregister ;
38
37
use function trigger_error ;
39
38
40
- use const E_USER_WARNING ;
39
+ use const E_USER_ERROR ;
41
40
use const SEEK_CUR ;
42
41
use const SEEK_END ;
43
42
use const SEEK_SET ;
@@ -60,7 +59,7 @@ class StreamWrapper
60
59
/** @var ReadableStream|WritableStream|null */
61
60
private $ stream ;
62
61
63
- /** @var array<string, Closure(string, string): ContextOptions|null > */
62
+ /** @var array<string, Closure(string, string, array ): ContextOptions> */
64
63
private static array $ contextResolvers = [];
65
64
66
65
public function __destruct ()
@@ -103,7 +102,11 @@ public static function register(string $protocol = 'gridfs'): void
103
102
*/
104
103
public static function setContextResolver (string $ name , ?Closure $ resolver ): void
105
104
{
106
- self ::$ contextResolvers [$ name ] = $ resolver ;
105
+ if ($ resolver === null ) {
106
+ unset(self ::$ contextResolvers [$ name ]);
107
+ } else {
108
+ self ::$ contextResolvers [$ name ] = $ resolver ;
109
+ }
107
110
}
108
111
109
112
/**
@@ -150,6 +153,14 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
150
153
$ context = [];
151
154
if (is_resource ($ this ->context )) {
152
155
$ context = stream_context_get_options ($ this ->context )[$ protocol ] ?? [];
156
+
157
+ if (! is_array ($ context )) {
158
+ if ($ options & STREAM_REPORT_ERRORS ) {
159
+ trigger_error (sprintf ('Invalid context for "%s" protocol. ' , $ protocol ), E_USER_ERROR );
160
+ }
161
+
162
+ return false ;
163
+ }
153
164
}
154
165
155
166
// Opening stream from gridfs
@@ -158,32 +169,38 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
158
169
159
170
if (count ($ parts ) < 4 ) {
160
171
if ($ options & STREAM_REPORT_ERRORS ) {
161
- trigger_error (sprintf ('Invalid GridFS file name: "%s" ' , $ path ), E_USER_WARNING );
172
+ trigger_error (sprintf ('Invalid GridFS file name: "%s" ' , $ path ), E_USER_ERROR );
162
173
}
163
174
164
175
return false ;
165
176
}
166
177
167
- if (! isset (self ::$ contextResolvers [$ parts [2 ]])) {
178
+ $ resolver = self ::$ contextResolvers [$ parts [2 ]] ?? null ;
179
+ if (null === $ resolver ) {
168
180
if ($ options & STREAM_REPORT_ERRORS ) {
169
- trigger_error (sprintf ('Unknown GridFS Bucket "%1$s". Call $bucket->asStreamWrap( \'%1$s \') on the Bucket you want to access. ' , $ parts [2 ]), E_USER_WARNING );
181
+ trigger_error (sprintf ('Unknown GridFS Bucket "%1$s". Call $bucket->asStreamWrap( \'%1$s \') on the Bucket you want to access. ' , $ parts [2 ]), E_USER_ERROR );
170
182
}
171
183
172
184
return false ;
173
185
}
174
186
175
- $ context = self :: $ contextResolvers [ $ parts [ 2 ]] ($ path , $ mode , $ context );
187
+ $ context = $ resolver ($ path , $ mode , $ context );
176
188
if ($ context === null ) {
177
189
if ($ options & STREAM_REPORT_ERRORS ) {
178
- trigger_error (sprintf ('File not found "%s". ' , $ path ), E_USER_WARNING );
190
+ trigger_error (sprintf ('File not found "%s". ' , $ path ), E_USER_ERROR );
179
191
}
180
192
181
193
return false ;
182
194
}
183
195
}
184
196
185
- assert (is_array ($ context ));
186
- assert (isset ($ context ['collectionWrapper ' ]) && $ context ['collectionWrapper ' ] instanceof CollectionWrapper);
197
+ if (! $ context ['collectionWrapper ' ] instanceof CollectionWrapper) {
198
+ if ($ options & STREAM_REPORT_ERRORS ) {
199
+ trigger_error ('Invalid context: "gridfs[collectionWrapper]" must be a CollectionWrapper. ' , E_USER_ERROR );
200
+ }
201
+
202
+ return false ;
203
+ }
187
204
188
205
if ($ mode === 'r ' || $ mode === 'rb ' ) {
189
206
assert (isset ($ context ['file ' ]) && is_object ($ context ['file ' ]));
@@ -315,6 +332,7 @@ public function stream_write(string $data): int
315
332
return $ this ->stream ->writeBytes ($ data );
316
333
}
317
334
335
+ /** @return false|array */
318
336
public function url_stat (string $ path , int $ flags )
319
337
{
320
338
$ success = $ this ->stream_open ($ path , 'r ' , 0 , $ openedPath );
0 commit comments