Skip to content

Commit 1e3be59

Browse files
committed
Merge pull request #240
2 parents a0e53eb + 96284ef commit 1e3be59

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

src/GridFS/Bucket.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@
2121
*/
2222
class Bucket
2323
{
24+
private static $defaultBucketName = 'fs';
2425
private static $defaultChunkSizeBytes = 261120;
2526
private static $streamWrapperProtocol = 'gridfs';
2627

2728
private $collectionWrapper;
2829
private $databaseName;
29-
private $options;
30+
private $bucketName;
31+
private $chunkSizeBytes;
3032

3133
/**
3234
* Constructs a GridFS bucket.
@@ -53,7 +55,7 @@ class Bucket
5355
public function __construct(Manager $manager, $databaseName, array $options = [])
5456
{
5557
$options += [
56-
'bucketName' => 'fs',
58+
'bucketName' => self::$defaultBucketName,
5759
'chunkSizeBytes' => self::$defaultChunkSizeBytes,
5860
];
5961

@@ -78,14 +80,30 @@ public function __construct(Manager $manager, $databaseName, array $options = []
7880
}
7981

8082
$this->databaseName = (string) $databaseName;
81-
$this->options = $options;
83+
$this->bucketName = $options['bucketName'];
84+
$this->chunkSizeBytes = $options['chunkSizeBytes'];
8285

8386
$collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'writeConcern' => 1]);
8487

8588
$this->collectionWrapper = new CollectionWrapper($manager, $databaseName, $options['bucketName'], $collectionOptions);
8689
$this->registerStreamWrapper();
8790
}
8891

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+
89107
/**
90108
* Delete a file from the GridFS bucket.
91109
*
@@ -179,11 +197,21 @@ public function find($filter, array $options = [])
179197
return $this->collectionWrapper->findFiles($filter, $options);
180198
}
181199

182-
public function getCollectionWrapper()
200+
/**
201+
* Return the bucket name.
202+
*
203+
* @return string
204+
*/
205+
public function getBucketName()
183206
{
184-
return $this->collectionWrapper;
207+
return $this->bucketName;
185208
}
186209

210+
/**
211+
* Return the database name.
212+
*
213+
* @return string
214+
*/
187215
public function getDatabaseName()
188216
{
189217
return $this->databaseName;
@@ -280,7 +308,7 @@ public function openDownloadStreamByName($filename, array $options = [])
280308
*/
281309
public function openUploadStream($filename, array $options = [])
282310
{
283-
$options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']];
311+
$options += ['chunkSizeBytes' => $this->chunkSizeBytes];
284312

285313
$path = $this->createPathForUpload();
286314
$context = stream_context_create([
@@ -372,7 +400,7 @@ private function createPathForFile(stdClass $file)
372400
'%s://%s/%s.files/%s',
373401
self::$streamWrapperProtocol,
374402
urlencode($this->databaseName),
375-
urlencode($this->options['bucketName']),
403+
urlencode($this->bucketName),
376404
urlencode($id)
377405
);
378406
}
@@ -388,7 +416,7 @@ private function createPathForUpload()
388416
'%s://%s/%s.files',
389417
self::$streamWrapperProtocol,
390418
urlencode($this->databaseName),
391-
urlencode($this->options['bucketName'])
419+
urlencode($this->bucketName)
392420
);
393421
}
394422

@@ -399,7 +427,7 @@ private function createPathForUpload()
399427
*/
400428
private function getFilesNamespace()
401429
{
402-
return sprintf('%s.%s.files', $this->databaseName, $this->options['bucketName']);
430+
return sprintf('%s.%s.files', $this->databaseName, $this->bucketName);
403431
}
404432

405433
/**

tests/GridFS/BucketFunctionalTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,18 @@ public function testFind()
306306
$this->assertSameDocuments($expected, $cursor);
307307
}
308308

309+
public function testGetBucketNameWithCustomValue()
310+
{
311+
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['bucketName' => 'custom_fs']);
312+
313+
$this->assertEquals('custom_fs', $bucket->getBucketName());
314+
}
315+
316+
public function testGetBucketNameWithDefaultValue()
317+
{
318+
$this->assertEquals('fs', $this->bucket->getBucketName());
319+
}
320+
309321
public function testGetDatabaseName()
310322
{
311323
$this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName());

0 commit comments

Comments
 (0)