@@ -219,12 +219,6 @@ public function __debugInfo()
219
219
];
220
220
}
221
221
222
- public function registerAsDefaultStreamWrapper (): void
223
- {
224
- // Use a closure to expose the private method into another class
225
- StreamWrapper::setDefaultContextResolver (fn (string $ path , string $ mode ) => $ this ->resolveStreamContext ($ path , $ mode ));
226
- }
227
-
228
222
/**
229
223
* Delete a file from the GridFS bucket.
230
224
*
@@ -598,6 +592,18 @@ public function openUploadStream(string $filename, array $options = [])
598
592
return fopen ($ path , 'w ' , false , $ context );
599
593
}
600
594
595
+ /**
596
+ * For legacy applications that needs to open stream from a string, the bucket can be registered in the stream
597
+ * wrapper to be used by default when no GridFS context is provided.
598
+ * Supported file name must follow this pattern:
599
+ * gridfs://<database_name>/<bucket_name>.fs/<file_name>
600
+ */
601
+ public function registerAsDefaultStreamWrapper (): void
602
+ {
603
+ // Use a closure to expose the private method into another class
604
+ StreamWrapper::setDefaultContextResolver (fn (string $ path , string $ mode ) => $ this ->resolveStreamContext ($ path , $ mode ));
605
+ }
606
+
601
607
/**
602
608
* Renames the GridFS file with the specified ID.
603
609
*
@@ -670,55 +676,6 @@ public function uploadFromStream(string $filename, $source, array $options = [])
670
676
return $ this ->getFileIdForStream ($ destination );
671
677
}
672
678
673
- /**
674
- * Create a stream context from
675
- *
676
- * @see StreamWrapper::setDefaultContextResolver()
677
- * @see stream_context_create()
678
- *
679
- * @param string $path The full url provided to fopen(). It contains the filename.
680
- * gridfs://database_name/collection_name.files/file_name
681
- *
682
- * @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}|null
683
- */
684
- private function resolveStreamContext (string $ path , string $ mode ): ?array
685
- {
686
- // The file can be read only if it belongs to this bucket
687
- $ basePath = $ this ->createPathForFile ((object ) ['_id ' => '' ]);
688
- if (! str_starts_with ($ path , $ basePath )) {
689
- return null ;
690
- }
691
-
692
- $ filename = urldecode (substr ($ path , strlen ($ basePath )));
693
-
694
- if (str_contains ($ mode , 'r ' )) {
695
- $ file = $ this ->collectionWrapper ->findFileByFilenameAndRevision ($ filename , -1 );
696
-
697
- // File not found
698
- if ($ file === null ) {
699
- return null ;
700
- }
701
-
702
- return [
703
- 'collectionWrapper ' => $ this ->collectionWrapper ,
704
- 'file ' => $ file ,
705
- ];
706
- }
707
-
708
- if (str_contains ($ mode , 'w ' )) {
709
- return [
710
- 'collectionWrapper ' => $ this ->collectionWrapper ,
711
- 'filename ' => $ filename ,
712
- 'options ' => [
713
- 'chunkSizeBytes ' => $ this ->chunkSizeBytes ,
714
- 'disableMD5 ' => $ this ->disableMD5 ,
715
- ],
716
- ];
717
- }
718
-
719
- return null ;
720
- }
721
-
722
679
/**
723
680
* Creates a path for an existing GridFS file.
724
681
*
@@ -816,4 +773,53 @@ private function registerStreamWrapper(): void
816
773
817
774
StreamWrapper::register (self ::STREAM_WRAPPER_PROTOCOL );
818
775
}
776
+
777
+ /**
778
+ * Create a stream context from
779
+ *
780
+ * @see StreamWrapper::setDefaultContextResolver()
781
+ * @see stream_context_create()
782
+ *
783
+ * @param string $path The full url provided to fopen(). It contains the filename.
784
+ * gridfs://database_name/collection_name.files/file_name
785
+ *
786
+ * @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}|null
787
+ */
788
+ private function resolveStreamContext (string $ path , string $ mode ): ?array
789
+ {
790
+ // The file can be read only if it belongs to this bucket
791
+ $ basePath = $ this ->createPathForFile ((object ) ['_id ' => '' ]);
792
+ if (! str_starts_with ($ path , $ basePath )) {
793
+ return null ;
794
+ }
795
+
796
+ $ filename = urldecode (substr ($ path , strlen ($ basePath )));
797
+
798
+ if (str_contains ($ mode , 'r ' )) {
799
+ $ file = $ this ->collectionWrapper ->findFileByFilenameAndRevision ($ filename , -1 );
800
+
801
+ // File not found
802
+ if ($ file === null ) {
803
+ return null ;
804
+ }
805
+
806
+ return [
807
+ 'collectionWrapper ' => $ this ->collectionWrapper ,
808
+ 'file ' => $ file ,
809
+ ];
810
+ }
811
+
812
+ if (str_contains ($ mode , 'w ' )) {
813
+ return [
814
+ 'collectionWrapper ' => $ this ->collectionWrapper ,
815
+ 'filename ' => $ filename ,
816
+ 'options ' => [
817
+ 'chunkSizeBytes ' => $ this ->chunkSizeBytes ,
818
+ 'disableMD5 ' => $ this ->disableMD5 ,
819
+ ],
820
+ ];
821
+ }
822
+
823
+ return null ;
824
+ }
819
825
}
0 commit comments