@@ -106,15 +106,15 @@ public function rename(string $fromPath, string $toPath): bool
106
106
}
107
107
108
108
try {
109
- $ this ->stream_open ($ fromPath , 'r ' , 0 , $ openedPath );
109
+ $ context = $ this ->getContext ($ fromPath , 'r ' );
110
110
} catch (FileNotFoundException $ e ) {
111
111
return false ;
112
112
}
113
113
114
- $ newName = explode ('/ ' , $ toPath , 4 )[3 ] ?? '' ;
115
- assert ( $ this -> stream instanceof ReadableStream );
114
+ $ newFilename = explode ('/ ' , $ toPath , 4 )[3 ] ?? '' ;
115
+ $ context [ ' collectionWrapper ' ]-> updateFilenameForFilename ( $ context [ ' file ' ]-> filename , $ newFilename );
116
116
117
- return $ this -> stream -> rename ( $ newName ) ;
117
+ return true ;
118
118
}
119
119
120
120
/**
@@ -170,41 +170,12 @@ public function stream_eof(): bool
170
170
*/
171
171
public function stream_open (string $ path , string $ mode , int $ options , ?string &$ openedPath ): bool
172
172
{
173
- $ context = [];
174
-
175
- /**
176
- * The Bucket methods { @see Bucket::openUploadStream() } and { @see Bucket::openDownloadStreamByFile() }
177
- * always set an internal context. But the context can also be set by the user.
178
- */
179
- if (is_resource ($ this ->context )) {
180
- $ context = stream_context_get_options ($ this ->context )['gridfs ' ] ?? [];
181
-
182
- if (! is_array ($ context )) {
183
- throw LogicException::invalidContext ($ context );
184
- }
185
- }
186
-
187
- // When the stream is opened using fopen(), the context is not required, it can contain only options.
188
- if (! isset ($ context ['collectionWrapper ' ])) {
189
- $ bucketAlias = explode ('/ ' , $ path , 4 )[2 ] ?? '' ;
190
-
191
- if (! isset (self ::$ contextResolvers [$ bucketAlias ])) {
192
- throw LogicException::bucketAliasNotRegistered ($ bucketAlias );
193
- }
194
-
195
- $ context = self ::$ contextResolvers [$ bucketAlias ]($ path , $ mode , $ context );
196
- }
197
-
198
- if (! $ context ['collectionWrapper ' ] instanceof CollectionWrapper) {
199
- throw LogicException::invalidContextCollectionWrapper ($ context ['collectionWrapper ' ]);
200
- }
201
-
202
173
if ($ mode === 'r ' || $ mode === 'rb ' ) {
203
- return $ this ->initReadableStream ($ context );
174
+ return $ this ->initReadableStream ($ this -> getContext ( $ path , $ mode ) );
204
175
}
205
176
206
177
if ($ mode === 'w ' || $ mode === 'wb ' ) {
207
- return $ this ->initWritableStream ($ context );
178
+ return $ this ->initWritableStream ($ this -> getContext ( $ path , $ mode ) );
208
179
}
209
180
210
181
throw LogicException::openModeNotSupported ($ mode );
@@ -326,15 +297,10 @@ public function stream_write(string $data): int
326
297
327
298
public function unlink (string $ path ): bool
328
299
{
329
- try {
330
- $ this ->stream_open ($ path , 'w ' , 0 , $ openedPath );
331
- } catch (FileNotFoundException $ e ) {
332
- return false ;
333
- }
300
+ $ context = $ this ->getContext ($ path , 'r ' );
301
+ $ count = $ context ['collectionWrapper ' ]->deleteFileAndChunksByFilename ($ context ['file ' ]->filename );
334
302
335
- assert ($ this ->stream instanceof WritableStream);
336
-
337
- return $ this ->stream ->delete () > 0 ;
303
+ return $ count !== 0 ;
338
304
}
339
305
340
306
/** @return false|array */
@@ -405,4 +371,39 @@ private function initWritableStream(array $contextOptions): bool
405
371
406
372
return true ;
407
373
}
374
+
375
+ /** @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array */
376
+ private function getContext (string $ path , string $ mode ): array
377
+ {
378
+ $ context = [];
379
+
380
+ /**
381
+ * The Bucket methods { @see Bucket::openUploadStream() } and { @see Bucket::openDownloadStreamByFile() }
382
+ * always set an internal context. But the context can also be set by the user.
383
+ */
384
+ if (is_resource ($ this ->context )) {
385
+ $ context = stream_context_get_options ($ this ->context )['gridfs ' ] ?? [];
386
+
387
+ if (! is_array ($ context )) {
388
+ throw LogicException::invalidContext ($ context );
389
+ }
390
+ }
391
+
392
+ // When the stream is opened using fopen(), the context is not required, it can contain only options.
393
+ if (! isset ($ context ['collectionWrapper ' ])) {
394
+ $ bucketAlias = explode ('/ ' , $ path , 4 )[2 ] ?? '' ;
395
+
396
+ if (! isset (self ::$ contextResolvers [$ bucketAlias ])) {
397
+ throw LogicException::bucketAliasNotRegistered ($ bucketAlias );
398
+ }
399
+
400
+ $ context = self ::$ contextResolvers [$ bucketAlias ]($ path , $ mode , $ context );
401
+ }
402
+
403
+ if (! $ context ['collectionWrapper ' ] instanceof CollectionWrapper) {
404
+ throw LogicException::invalidContextCollectionWrapper ($ context ['collectionWrapper ' ]);
405
+ }
406
+
407
+ return $ context ;
408
+ }
408
409
}
0 commit comments