@@ -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 */
@@ -351,6 +317,41 @@ public function url_stat(string $path, int $flags)
351
317
return $ this ->stream_stat ();
352
318
}
353
319
320
+ /** @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array */
321
+ private function getContext (string $ path , string $ mode ): array
322
+ {
323
+ $ context = [];
324
+
325
+ /**
326
+ * The Bucket methods { @see Bucket::openUploadStream() } and { @see Bucket::openDownloadStreamByFile() }
327
+ * always set an internal context. But the context can also be set by the user.
328
+ */
329
+ if (is_resource ($ this ->context )) {
330
+ $ context = stream_context_get_options ($ this ->context )['gridfs ' ] ?? [];
331
+
332
+ if (! is_array ($ context )) {
333
+ throw LogicException::invalidContext ($ context );
334
+ }
335
+ }
336
+
337
+ // When the stream is opened using fopen(), the context is not required, it can contain only options.
338
+ if (! isset ($ context ['collectionWrapper ' ])) {
339
+ $ bucketAlias = explode ('/ ' , $ path , 4 )[2 ] ?? '' ;
340
+
341
+ if (! isset (self ::$ contextResolvers [$ bucketAlias ])) {
342
+ throw LogicException::bucketAliasNotRegistered ($ bucketAlias );
343
+ }
344
+
345
+ $ context = self ::$ contextResolvers [$ bucketAlias ]($ path , $ mode , $ context );
346
+ }
347
+
348
+ if (! $ context ['collectionWrapper ' ] instanceof CollectionWrapper) {
349
+ throw LogicException::invalidContextCollectionWrapper ($ context ['collectionWrapper ' ]);
350
+ }
351
+
352
+ return $ context ;
353
+ }
354
+
354
355
/**
355
356
* Returns a stat template with default values.
356
357
*/
0 commit comments