Skip to content

Commit c94fa5f

Browse files
committed
Implement Bucket::getBucketName() and debug handler
1 parent d3153e9 commit c94fa5f

File tree

2 files changed

+50
-6
lines changed

2 files changed

+50
-6
lines changed

src/GridFS/Bucket.php

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ class Bucket
2626

2727
private $collectionWrapper;
2828
private $databaseName;
29-
private $options;
29+
private $bucketName;
30+
private $chunkSizeBytes;
3031

3132
/**
3233
* Constructs a GridFS bucket.
@@ -78,14 +79,30 @@ public function __construct(Manager $manager, $databaseName, array $options = []
7879
}
7980

8081
$this->databaseName = (string) $databaseName;
81-
$this->options = $options;
82+
$this->bucketName = $options['bucketName'];
83+
$this->chunkSizeBytes = $options['chunkSizeBytes'];
8284

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

8587
$this->collectionWrapper = new CollectionWrapper($manager, $databaseName, $options['bucketName'], $collectionOptions);
8688
$this->registerStreamWrapper();
8789
}
8890

91+
/**
92+
* Return internal properties for debugging purposes.
93+
*
94+
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
95+
* @return array
96+
*/
97+
public function __debugInfo()
98+
{
99+
return [
100+
'bucketName' => $this->bucketName,
101+
'databaseName' => $this->databaseName,
102+
'chunkSizeBytes' => $this->chunkSizeBytes,
103+
];
104+
}
105+
89106
/**
90107
* Delete a file from the GridFS bucket.
91108
*
@@ -179,6 +196,21 @@ public function find($filter, array $options = [])
179196
return $this->collectionWrapper->findFiles($filter, $options);
180197
}
181198

199+
/**
200+
* Return the bucket name.
201+
*
202+
* @return string
203+
*/
204+
public function getBucketName()
205+
{
206+
return $this->bucketName;
207+
}
208+
209+
/**
210+
* Return the database name.
211+
*
212+
* @return string
213+
*/
182214
public function getDatabaseName()
183215
{
184216
return $this->databaseName;
@@ -275,7 +307,7 @@ public function openDownloadStreamByName($filename, array $options = [])
275307
*/
276308
public function openUploadStream($filename, array $options = [])
277309
{
278-
$options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']];
310+
$options += ['chunkSizeBytes' => $this->chunkSizeBytes];
279311

280312
$path = $this->createPathForUpload();
281313
$context = stream_context_create([
@@ -367,7 +399,7 @@ private function createPathForFile(stdClass $file)
367399
'%s://%s/%s.files/%s',
368400
self::$streamWrapperProtocol,
369401
urlencode($this->databaseName),
370-
urlencode($this->options['bucketName']),
402+
urlencode($this->bucketName),
371403
urlencode($id)
372404
);
373405
}
@@ -383,7 +415,7 @@ private function createPathForUpload()
383415
'%s://%s/%s.files',
384416
self::$streamWrapperProtocol,
385417
urlencode($this->databaseName),
386-
urlencode($this->options['bucketName'])
418+
urlencode($this->bucketName)
387419
);
388420
}
389421

@@ -394,7 +426,7 @@ private function createPathForUpload()
394426
*/
395427
private function getFilesNamespace()
396428
{
397-
return sprintf('%s.%s.files', $this->databaseName, $this->options['bucketName']);
429+
return sprintf('%s.%s.files', $this->databaseName, $this->bucketName);
398430
}
399431

400432
/**

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)