@@ -106,15 +106,15 @@ public function rename(string $fromPath, string $toPath): bool
106106 }
107107
108108 try {
109- $ this ->stream_open ($ fromPath , 'r ' , 0 , $ openedPath );
109+ $ context = $ this ->getContext ($ fromPath , 'r ' );
110110 } catch (FileNotFoundException $ e ) {
111111 return false ;
112112 }
113113
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 );
116116
117- return $ this -> stream -> rename ( $ newName ) ;
117+ return true ;
118118 }
119119
120120 /**
@@ -170,41 +170,12 @@ public function stream_eof(): bool
170170 */
171171 public function stream_open (string $ path , string $ mode , int $ options , ?string &$ openedPath ): bool
172172 {
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- 202173 if ($ mode === 'r ' || $ mode === 'rb ' ) {
203- return $ this ->initReadableStream ($ context );
174+ return $ this ->initReadableStream ($ this -> getContext ( $ path , $ mode ) );
204175 }
205176
206177 if ($ mode === 'w ' || $ mode === 'wb ' ) {
207- return $ this ->initWritableStream ($ context );
178+ return $ this ->initWritableStream ($ this -> getContext ( $ path , $ mode ) );
208179 }
209180
210181 throw LogicException::openModeNotSupported ($ mode );
@@ -326,15 +297,10 @@ public function stream_write(string $data): int
326297
327298 public function unlink (string $ path ): bool
328299 {
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 );
334302
335- assert ($ this ->stream instanceof WritableStream);
336- 337- return $ this ->stream ->delete () > 0 ;
303+ return $ count > 0 ;
338304 }
339305
340306 /** @return false|array */
@@ -351,6 +317,41 @@ public function url_stat(string $path, int $flags)
351317 return $ this ->stream_stat ();
352318 }
353319
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+ 354355 /**
355356 * Returns a stat template with default values.
356357 */
0 commit comments