Commit 1e3be59e authored by Jeremy Mikola's avatar Jeremy Mikola

Merge pull request #240

parents a0e53eb6 96284ef8
...@@ -21,12 +21,14 @@ use stdClass; ...@@ -21,12 +21,14 @@ use stdClass;
*/ */
class Bucket class Bucket
{ {
private static $defaultBucketName = 'fs';
private static $defaultChunkSizeBytes = 261120; private static $defaultChunkSizeBytes = 261120;
private static $streamWrapperProtocol = 'gridfs'; private static $streamWrapperProtocol = 'gridfs';
private $collectionWrapper; private $collectionWrapper;
private $databaseName; private $databaseName;
private $options; private $bucketName;
private $chunkSizeBytes;
/** /**
* Constructs a GridFS bucket. * Constructs a GridFS bucket.
...@@ -53,7 +55,7 @@ class Bucket ...@@ -53,7 +55,7 @@ class Bucket
public function __construct(Manager $manager, $databaseName, array $options = []) public function __construct(Manager $manager, $databaseName, array $options = [])
{ {
$options += [ $options += [
'bucketName' => 'fs', 'bucketName' => self::$defaultBucketName,
'chunkSizeBytes' => self::$defaultChunkSizeBytes, 'chunkSizeBytes' => self::$defaultChunkSizeBytes,
]; ];
...@@ -78,7 +80,8 @@ class Bucket ...@@ -78,7 +80,8 @@ class Bucket
} }
$this->databaseName = (string) $databaseName; $this->databaseName = (string) $databaseName;
$this->options = $options; $this->bucketName = $options['bucketName'];
$this->chunkSizeBytes = $options['chunkSizeBytes'];
$collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'writeConcern' => 1]); $collectionOptions = array_intersect_key($options, ['readConcern' => 1, 'readPreference' => 1, 'writeConcern' => 1]);
...@@ -86,6 +89,21 @@ class Bucket ...@@ -86,6 +89,21 @@ class Bucket
$this->registerStreamWrapper(); $this->registerStreamWrapper();
} }
/**
* Return internal properties for debugging purposes.
*
* @see http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
* @return array
*/
public function __debugInfo()
{
return [
'bucketName' => $this->bucketName,
'databaseName' => $this->databaseName,
'chunkSizeBytes' => $this->chunkSizeBytes,
];
}
/** /**
* Delete a file from the GridFS bucket. * Delete a file from the GridFS bucket.
* *
...@@ -179,11 +197,21 @@ class Bucket ...@@ -179,11 +197,21 @@ class Bucket
return $this->collectionWrapper->findFiles($filter, $options); return $this->collectionWrapper->findFiles($filter, $options);
} }
public function getCollectionWrapper() /**
* Return the bucket name.
*
* @return string
*/
public function getBucketName()
{ {
return $this->collectionWrapper; return $this->bucketName;
} }
/**
* Return the database name.
*
* @return string
*/
public function getDatabaseName() public function getDatabaseName()
{ {
return $this->databaseName; return $this->databaseName;
...@@ -280,7 +308,7 @@ class Bucket ...@@ -280,7 +308,7 @@ class Bucket
*/ */
public function openUploadStream($filename, array $options = []) public function openUploadStream($filename, array $options = [])
{ {
$options += ['chunkSizeBytes' => $this->options['chunkSizeBytes']]; $options += ['chunkSizeBytes' => $this->chunkSizeBytes];
$path = $this->createPathForUpload(); $path = $this->createPathForUpload();
$context = stream_context_create([ $context = stream_context_create([
...@@ -372,7 +400,7 @@ class Bucket ...@@ -372,7 +400,7 @@ class Bucket
'%s://%s/%s.files/%s', '%s://%s/%s.files/%s',
self::$streamWrapperProtocol, self::$streamWrapperProtocol,
urlencode($this->databaseName), urlencode($this->databaseName),
urlencode($this->options['bucketName']), urlencode($this->bucketName),
urlencode($id) urlencode($id)
); );
} }
...@@ -388,7 +416,7 @@ class Bucket ...@@ -388,7 +416,7 @@ class Bucket
'%s://%s/%s.files', '%s://%s/%s.files',
self::$streamWrapperProtocol, self::$streamWrapperProtocol,
urlencode($this->databaseName), urlencode($this->databaseName),
urlencode($this->options['bucketName']) urlencode($this->bucketName)
); );
} }
...@@ -399,7 +427,7 @@ class Bucket ...@@ -399,7 +427,7 @@ class Bucket
*/ */
private function getFilesNamespace() private function getFilesNamespace()
{ {
return sprintf('%s.%s.files', $this->databaseName, $this->options['bucketName']); return sprintf('%s.%s.files', $this->databaseName, $this->bucketName);
} }
/** /**
......
...@@ -306,6 +306,18 @@ class BucketFunctionalTest extends FunctionalTestCase ...@@ -306,6 +306,18 @@ class BucketFunctionalTest extends FunctionalTestCase
$this->assertSameDocuments($expected, $cursor); $this->assertSameDocuments($expected, $cursor);
} }
public function testGetBucketNameWithCustomValue()
{
$bucket = new Bucket($this->manager, $this->getDatabaseName(), ['bucketName' => 'custom_fs']);
$this->assertEquals('custom_fs', $bucket->getBucketName());
}
public function testGetBucketNameWithDefaultValue()
{
$this->assertEquals('fs', $this->bucket->getBucketName());
}
public function testGetDatabaseName() public function testGetDatabaseName()
{ {
$this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName()); $this->assertEquals($this->getDatabaseName(), $this->bucket->getDatabaseName());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment