21
21
*/
22
22
class Bucket
23
23
{
24
+ private static $ defaultBucketName = 'fs ' ;
24
25
private static $ defaultChunkSizeBytes = 261120 ;
25
26
private static $ streamWrapperProtocol = 'gridfs ' ;
26
27
27
28
private $ collectionWrapper ;
28
29
private $ databaseName ;
29
- private $ options ;
30
+ private $ bucketName ;
31
+ private $ chunkSizeBytes ;
30
32
31
33
/**
32
34
* Constructs a GridFS bucket.
@@ -53,7 +55,7 @@ class Bucket
53
55
public function __construct (Manager $ manager , $ databaseName , array $ options = [])
54
56
{
55
57
$ options += [
56
- 'bucketName ' => ' fs ' ,
58
+ 'bucketName ' => self :: $ defaultBucketName ,
57
59
'chunkSizeBytes ' => self ::$ defaultChunkSizeBytes ,
58
60
];
59
61
@@ -78,14 +80,30 @@ public function __construct(Manager $manager, $databaseName, array $options = []
78
80
}
79
81
80
82
$ this ->databaseName = (string ) $ databaseName ;
81
- $ this ->options = $ options ;
83
+ $ this ->bucketName = $ options ['bucketName ' ];
84
+ $ this ->chunkSizeBytes = $ options ['chunkSizeBytes ' ];
82
85
83
86
$ collectionOptions = array_intersect_key ($ options , ['readConcern ' => 1 , 'readPreference ' => 1 , 'writeConcern ' => 1 ]);
84
87
85
88
$ this ->collectionWrapper = new CollectionWrapper ($ manager , $ databaseName , $ options ['bucketName ' ], $ collectionOptions );
86
89
$ this ->registerStreamWrapper ();
87
90
}
88
91
92
+ /**
93
+ * Return internal properties for debugging purposes.
94
+ *
95
+ * @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
96
+ * @return array
97
+ */
98
+ public function __debugInfo ()
99
+ {
100
+ return [
101
+ 'bucketName ' => $ this ->bucketName ,
102
+ 'databaseName ' => $ this ->databaseName ,
103
+ 'chunkSizeBytes ' => $ this ->chunkSizeBytes ,
104
+ ];
105
+ }
106
+
89
107
/**
90
108
* Delete a file from the GridFS bucket.
91
109
*
@@ -179,11 +197,21 @@ public function find($filter, array $options = [])
179
197
return $ this ->collectionWrapper ->findFiles ($ filter , $ options );
180
198
}
181
199
182
- public function getCollectionWrapper ()
200
+ /**
201
+ * Return the bucket name.
202
+ *
203
+ * @return string
204
+ */
205
+ public function getBucketName ()
183
206
{
184
- return $ this ->collectionWrapper ;
207
+ return $ this ->bucketName ;
185
208
}
186
209
210
+ /**
211
+ * Return the database name.
212
+ *
213
+ * @return string
214
+ */
187
215
public function getDatabaseName ()
188
216
{
189
217
return $ this ->databaseName ;
@@ -280,7 +308,7 @@ public function openDownloadStreamByName($filename, array $options = [])
280
308
*/
281
309
public function openUploadStream ($ filename , array $ options = [])
282
310
{
283
- $ options += ['chunkSizeBytes ' => $ this ->options [ ' chunkSizeBytes ' ] ];
311
+ $ options += ['chunkSizeBytes ' => $ this ->chunkSizeBytes ];
284
312
285
313
$ path = $ this ->createPathForUpload ();
286
314
$ context = stream_context_create ([
@@ -372,7 +400,7 @@ private function createPathForFile(stdClass $file)
372
400
'%s://%s/%s.files/%s ' ,
373
401
self ::$ streamWrapperProtocol ,
374
402
urlencode ($ this ->databaseName ),
375
- urlencode ($ this ->options [ ' bucketName ' ] ),
403
+ urlencode ($ this ->bucketName ),
376
404
urlencode ($ id )
377
405
);
378
406
}
@@ -388,7 +416,7 @@ private function createPathForUpload()
388
416
'%s://%s/%s.files ' ,
389
417
self ::$ streamWrapperProtocol ,
390
418
urlencode ($ this ->databaseName ),
391
- urlencode ($ this ->options [ ' bucketName ' ] )
419
+ urlencode ($ this ->bucketName )
392
420
);
393
421
}
394
422
@@ -399,7 +427,7 @@ private function createPathForUpload()
399
427
*/
400
428
private function getFilesNamespace ()
401
429
{
402
- return sprintf ('%s.%s.files ' , $ this ->databaseName , $ this ->options [ ' bucketName ' ] );
430
+ return sprintf ('%s.%s.files ' , $ this ->databaseName , $ this ->bucketName );
403
431
}
404
432
405
433
/**
0 commit comments