Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
8117675e
Commit
8117675e
authored
Dec 01, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #290
parents
79e12213
f0090c88
Hide whitespace changes
Inline
Side-by-side
Showing
14 changed files
with
250 additions
and
25 deletions
+250
-25
apiargs-MongoDBDatabase-method-selectGridFSBucket-option.yaml
...rgs-MongoDBDatabase-method-selectGridFSBucket-option.yaml
+6
-0
apiargs-MongoDBGridFSBucket-method-construct-option.yaml
.../apiargs-MongoDBGridFSBucket-method-construct-option.yaml
+4
-0
apiargs-MongoDBGridFSBucket-method-findOne-option.yaml
...es/apiargs-MongoDBGridFSBucket-method-findOne-option.yaml
+42
-0
MongoDBGridFSBucket.txt
docs/reference/class/MongoDBGridFSBucket.txt
+1
-0
MongoDBCollection-findOne.txt
docs/reference/method/MongoDBCollection-findOne.txt
+3
-3
MongoDBDatabase-selectGridFSBucket.txt
docs/reference/method/MongoDBDatabase-selectGridFSBucket.txt
+2
-2
MongoDBGridFSBucket-find.txt
docs/reference/method/MongoDBGridFSBucket-find.txt
+1
-0
MongoDBGridFSBucket-findOne.txt
docs/reference/method/MongoDBGridFSBucket-findOne.txt
+46
-0
MongoDBGridFSBucket-getFileDocumentForStream.txt
...e/method/MongoDBGridFSBucket-getFileDocumentForStream.txt
+3
-2
MongoDBGridFSBucket-getFileIdForStream.txt
...ference/method/MongoDBGridFSBucket-getFileIdForStream.txt
+2
-2
Database.php
src/Database.php
+1
-0
Bucket.php
src/GridFS/Bucket.php
+66
-14
CollectionWrapper.php
src/GridFS/CollectionWrapper.php
+15
-2
BucketFunctionalTest.php
tests/GridFS/BucketFunctionalTest.php
+58
-0
No files found.
docs/includes/apiargs-MongoDBDatabase-method-selectGridFSBucket-option.yaml
View file @
8117675e
...
...
@@ -31,6 +31,12 @@ replacement:
resource
:
"
bucket"
parent
:
"
database"
---
source
:
file
:
apiargs-common-option.yaml
ref
:
typeMap
replacement
:
parent
:
"
database"
---
source
:
file
:
apiargs-common-option.yaml
ref
:
writeConcern
...
...
docs/includes/apiargs-MongoDBGridFSBucket-method-construct-option.yaml
View file @
8117675e
...
...
@@ -31,6 +31,10 @@ replacement:
resource
:
"
bucket"
parent
:
"
database"
---
source
:
file
:
apiargs-MongoDBClient-method-construct-driverOptions.yaml
ref
:
typeMap
---
source
:
file
:
apiargs-common-option.yaml
ref
:
writeConcern
...
...
docs/includes/apiargs-MongoDBGridFSBucket-method-findOne-option.yaml
0 → 100644
View file @
8117675e
source
:
file
:
apiargs-MongoDBCollection-method-find-option.yaml
ref
:
projection
---
source
:
file
:
apiargs-MongoDBCollection-method-find-option.yaml
ref
:
sort
---
source
:
file
:
apiargs-MongoDBCollection-method-find-option.yaml
ref
:
skip
---
source
:
file
:
apiargs-MongoDBCollection-common-option.yaml
ref
:
collation
---
source
:
file
:
apiargs-MongoDBCollection-method-find-option.yaml
ref
:
comment
---
source
:
file
:
apiargs-common-option.yaml
ref
:
maxTimeMS
---
source
:
file
:
apiargs-MongoDBCollection-method-find-option.yaml
ref
:
readConcern
---
source
:
file
:
apiargs-MongoDBGridFSBucket-method-find-option.yaml
ref
:
readPreference
---
source
:
file
:
apiargs-MongoDBGridFSBucket-method-find-option.yaml
ref
:
typeMap
post
:
|
This will be used for the returned result document.
---
source
:
file
:
apiargs-MongoDBCollection-method-find-option.yaml
ref
:
modifiers
...
docs/reference/class/MongoDBGridFSBucket.txt
View file @
8117675e
...
...
@@ -40,6 +40,7 @@ Methods
/reference/method/MongoDBGridFSBucket-downloadToStreamByName
/reference/method/MongoDBGridFSBucket-drop
/reference/method/MongoDBGridFSBucket-find
/reference/method/MongoDBGridFSBucket-findOne
/reference/method/MongoDBGridFSBucket-getBucketName
/reference/method/MongoDBGridFSBucket-getDatabaseName
/reference/method/MongoDBGridFSBucket-getFileDocumentForStream
...
...
docs/reference/method/MongoDBCollection-findOne.txt
View file @
8117675e
...
...
@@ -32,9 +32,9 @@ Definition
Return Values
-------------
An array or object for the :term:`first document <natural order>`
document that
matched the query, or ``null`` if no document matched the query. The return type
will
depend on the ``typeMap`` option.
An array or object for the :term:`first document <natural order>`
that matched
the query, or ``null`` if no document matched the query. The return type will
depend on the ``typeMap`` option.
Errors/Exceptions
-----------------
...
...
docs/reference/method/MongoDBDatabase-selectGridFSBucket.txt
View file @
8117675e
...
...
@@ -42,8 +42,8 @@ Errors/Exceptions
Behavior
--------
The selected bucket inherits options such as read preference and
write concern
from the :phpclass:`Database <MongoDB\\Database>` object. Options may be
The selected bucket inherits options such as read preference and
type
mapping
from the :phpclass:`Database <MongoDB\\Database>` object. Options may be
overridden via the ``$options`` parameter.
Example
...
...
docs/reference/method/MongoDBGridFSBucket-find.txt
View file @
8117675e
...
...
@@ -40,3 +40,4 @@ See Also
--------
- :phpmethod:`MongoDB\\Collection::find()`
- :phpmethod:`MongoDB\\GridFS\\Bucket::findOne()`
docs/reference/method/MongoDBGridFSBucket-findOne.txt
0 → 100644
View file @
8117675e
==================================
MongoDB\\GridFS\\Bucket::findOne()
==================================
.. default-domain:: mongodb
.. contents:: On this page
:local:
:backlinks: none
:depth: 1
:class: singlecol
Definition
----------
.. phpmethod:: MongoDB\\GridFS\\Bucket::findOne()
Finds a single document from the GridFS bucket's files collection matching
the query.
.. code-block:: php
function findOne($filter = [], array $options = []): array|object|null
This method has the following parameters:
.. include:: /includes/apiargs/MongoDBCollection-method-findOne-param.rst
The ``$options`` parameter supports the following options:
.. include:: /includes/apiargs/MongoDBGridFSBucket-method-findOne-option.rst
Return Values
-------------
An array or object for the :term:`first document <natural order>` that matched
the query, or ``null`` if no document matched the query. The return type will
depend on the ``typeMap`` option.
.. todo: add examples
See Also
--------
- :phpmethod:`MongoDB\\Collection::findOne()`
- :phpmethod:`MongoDB\\GridFS\\Bucket::find()`
docs/reference/method/MongoDBGridFSBucket-getFileDocumentForStream.txt
View file @
8117675e
...
...
@@ -19,7 +19,7 @@ Definition
.. code-block:: php
function getFileDocumentForStream($stream): object
function getFileDocumentForStream($stream):
array|
object
This method has the following parameters:
...
...
@@ -28,7 +28,8 @@ Definition
Return Values
-------------
The metadata document associated with the GridFS stream.
The metadata document associated with the GridFS stream. The return type will
depend on the bucket's ``typeMap`` option.
.. todo: add examples
...
...
docs/reference/method/MongoDBGridFSBucket-getFileIdForStream.txt
View file @
8117675e
...
...
@@ -28,8 +28,8 @@ Definition
Return Values
-------------
The ``_id`` field of the metadata document associated with the GridFS
stream
.
The ``_id`` field of the metadata document associated with the GridFS
stream.
The return type will depend on the bucket's ``typeMap`` option
.
.. todo: add examples
...
...
src/Database.php
View file @
8117675e
...
...
@@ -317,6 +317,7 @@ class Database
$options
+=
[
'readConcern'
=>
$this
->
readConcern
,
'readPreference'
=>
$this
->
readPreference
,
'typeMap'
=>
$this
->
typeMap
,
'writeConcern'
=>
$this
->
writeConcern
,
];
...
...
src/GridFS/Bucket.php
View file @
8117675e
...
...
@@ -23,6 +23,11 @@ class Bucket
{
private
static
$defaultBucketName
=
'fs'
;
private
static
$defaultChunkSizeBytes
=
261120
;
private
static
$defaultTypeMap
=
[
'array'
=>
'MongoDB\Model\BSONArray'
,
'document'
=>
'MongoDB\Model\BSONDocument'
,
'root'
=>
'MongoDB\Model\BSONDocument'
,
];
private
static
$streamWrapperProtocol
=
'gridfs'
;
private
$collectionWrapper
;
...
...
@@ -32,6 +37,7 @@ class Bucket
private
$chunkSizeBytes
;
private
$readConcern
;
private
$readPreference
;
private
$typeMap
;
private
$writeConcern
;
/**
...
...
@@ -49,6 +55,8 @@ class Bucket
*
* * readPreference (MongoDB\Driver\ReadPreference): Read preference.
*
* * typeMap (array): Default type map for cursors and BSON documents.
*
* * writeConcern (MongoDB\Driver\WriteConcern): Write concern.
*
* @param Manager $manager Manager instance from the driver
...
...
@@ -79,6 +87,10 @@ class Bucket
throw
InvalidArgumentException
::
invalidType
(
'"readPreference" option'
,
$options
[
'readPreference'
],
'MongoDB\Driver\ReadPreference'
);
}
if
(
isset
(
$options
[
'typeMap'
])
&&
!
is_array
(
$options
[
'typeMap'
]))
{
throw
InvalidArgumentException
::
invalidType
(
'"typeMap" option'
,
$options
[
'typeMap'
],
'array'
);
}
if
(
isset
(
$options
[
'writeConcern'
])
&&
!
$options
[
'writeConcern'
]
instanceof
WriteConcern
)
{
throw
InvalidArgumentException
::
invalidType
(
'"writeConcern" option'
,
$options
[
'writeConcern'
],
'MongoDB\Driver\WriteConcern'
);
}
...
...
@@ -89,9 +101,10 @@ class Bucket
$this
->
chunkSizeBytes
=
$options
[
'chunkSizeBytes'
];
$this
->
readConcern
=
isset
(
$options
[
'readConcern'
])
?
$options
[
'readConcern'
]
:
$this
->
manager
->
getReadConcern
();
$this
->
readPreference
=
isset
(
$options
[
'readPreference'
])
?
$options
[
'readPreference'
]
:
$this
->
manager
->
getReadPreference
();
$this
->
typeMap
=
isset
(
$options
[
'typeMap'
])
?
$options
[
'typeMap'
]
:
self
::
$defaultTypeMap
;
$this
->
writeConcern
=
isset
(
$options
[
'writeConcern'
])
?
$options
[
'writeConcern'
]
:
$this
->
manager
->
getWriteConcern
();
$collectionOptions
=
array_intersect_key
(
$options
,
[
'readConcern'
=>
1
,
'readPreference'
=>
1
,
'writeConcern'
=>
1
]);
$collectionOptions
=
array_intersect_key
(
$options
,
[
'readConcern'
=>
1
,
'readPreference'
=>
1
,
'
typeMap'
=>
1
,
'
writeConcern'
=>
1
]);
$this
->
collectionWrapper
=
new
CollectionWrapper
(
$manager
,
$databaseName
,
$options
[
'bucketName'
],
$collectionOptions
);
$this
->
registerStreamWrapper
();
...
...
@@ -112,6 +125,7 @@ class Bucket
'chunkSizeBytes'
=>
$this
->
chunkSizeBytes
,
'readConcern'
=>
$this
->
readConcern
,
'readPreference'
=>
$this
->
readPreference
,
'typeMap'
=>
$this
->
typeMap
,
'writeConcern'
=>
$this
->
writeConcern
,
];
}
...
...
@@ -204,11 +218,25 @@ class Bucket
* @param array $options Additional options
* @return Cursor
*/
public
function
find
(
$filter
,
array
$options
=
[])
public
function
find
(
$filter
=
[]
,
array
$options
=
[])
{
return
$this
->
collectionWrapper
->
findFiles
(
$filter
,
$options
);
}
/**
* Finds a single document from the GridFS bucket's files collection
* matching the query.
*
* @see FindOne::__construct() for supported options
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
* @return array|object|null
*/
public
function
findOne
(
$filter
=
[],
array
$options
=
[])
{
return
$this
->
collectionWrapper
->
findOneFile
(
$filter
,
$options
);
}
/**
* Return the bucket name.
*
...
...
@@ -233,22 +261,15 @@ class Bucket
* Gets the file document of the GridFS file associated with a stream.
*
* @param resource $stream GridFS stream
* @return
stdClass
* @return
array|object
* @throws InvalidArgumentException
*/
public
function
getFileDocumentForStream
(
$stream
)
{
if
(
!
is_resource
(
$stream
)
||
get_resource_type
(
$stream
)
!=
"stream"
)
{
throw
InvalidArgumentException
::
invalidType
(
'$stream'
,
$stream
,
'resource'
);
}
$file
=
$this
->
getRawFileDocumentForStream
(
$stream
);
$metadata
=
stream_get_meta_data
(
$stream
);
if
(
!
$metadata
[
'wrapper_data'
]
instanceof
StreamWrapper
)
{
throw
InvalidArgumentException
::
invalidType
(
'$stream wrapper data'
,
$metadata
[
'wrapper_data'
],
'MongoDB\Driver\GridFS\StreamWrapper'
);
}
return
$metadata
[
'wrapper_data'
]
->
getFile
();
// Filter the raw document through the specified type map
return
\MongoDB\BSON\toPHP
(
\MongoDB\BSON\fromPHP
(
$file
),
$this
->
typeMap
);
}
/**
...
...
@@ -261,7 +282,13 @@ class Bucket
*/
public
function
getFileIdForStream
(
$stream
)
{
$file
=
$this
->
getFileDocumentForStream
(
$stream
);
$file
=
$this
->
getRawFileDocumentForStream
(
$stream
);
/* Filter the raw document through the specified type map, but override
* the root type so we can reliably access the ID.
*/
$typeMap
=
[
'root'
=>
'stdClass'
]
+
$this
->
typeMap
;
$file
=
\MongoDB\BSON\toPHP
(
\MongoDB\BSON\fromPHP
(
$file
),
$typeMap
);
if
(
!
isset
(
$file
->
_id
)
&&
!
property_exists
(
$file
,
'_id'
))
{
throw
new
CorruptFileException
(
'file._id does not exist'
);
...
...
@@ -466,6 +493,31 @@ class Bucket
return
sprintf
(
'%s.%s.files'
,
$this
->
databaseName
,
$this
->
bucketName
);
}
/**
* Gets the file document of the GridFS file associated with a stream.
*
* This returns the raw document from the StreamWrapper, which does not
* respect the Bucket's type map.
*
* @param resource $stream GridFS stream
* @return stdClass
* @throws InvalidArgumentException
*/
private
function
getRawFileDocumentForStream
(
$stream
)
{
if
(
!
is_resource
(
$stream
)
||
get_resource_type
(
$stream
)
!=
"stream"
)
{
throw
InvalidArgumentException
::
invalidType
(
'$stream'
,
$stream
,
'resource'
);
}
$metadata
=
stream_get_meta_data
(
$stream
);
if
(
!
$metadata
[
'wrapper_data'
]
instanceof
StreamWrapper
)
{
throw
InvalidArgumentException
::
invalidType
(
'$stream wrapper data'
,
$metadata
[
'wrapper_data'
],
'MongoDB\Driver\GridFS\StreamWrapper'
);
}
return
$metadata
[
'wrapper_data'
]
->
getFile
();
}
/**
* Opens a readable stream for the GridFS file.
*
...
...
src/GridFS/CollectionWrapper.php
View file @
8117675e
...
...
@@ -68,8 +68,8 @@ class CollectionWrapper
*/
public
function
dropCollections
()
{
$this
->
filesCollection
->
drop
();
$this
->
chunksCollection
->
drop
();
$this
->
filesCollection
->
drop
(
[
'typeMap'
=>
[]]
);
$this
->
chunksCollection
->
drop
(
[
'typeMap'
=>
[]]
);
}
/**
...
...
@@ -140,6 +140,18 @@ class CollectionWrapper
return
$this
->
filesCollection
->
find
(
$filter
,
$options
);
}
/**
* Finds a single document from the GridFS bucket's files collection.
*
* @param array|object $filter Query by which to filter documents
* @param array $options Additional options
* @return array|object|null
*/
public
function
findOneFile
(
$filter
,
array
$options
=
[])
{
return
$this
->
filesCollection
->
findOne
(
$filter
,
$options
);
}
/**
* Return the bucket name.
*
...
...
@@ -284,6 +296,7 @@ class CollectionWrapper
return
null
===
$this
->
filesCollection
->
findOne
([],
[
'readPreference'
=>
new
ReadPreference
(
ReadPreference
::
RP_PRIMARY
),
'projection'
=>
[
'_id'
=>
1
],
'typeMap'
=>
[],
]);
}
}
tests/GridFS/BucketFunctionalTest.php
View file @
8117675e
...
...
@@ -57,6 +57,10 @@ class BucketFunctionalTest extends FunctionalTestCase
$options
[][]
=
[
'readPreference'
=>
$value
];
}
foreach
(
$this
->
getInvalidArrayValues
()
as
$value
)
{
$options
[][]
=
[
'typeMap'
=>
$value
];
}
foreach
(
$this
->
getInvalidWriteConcernValues
()
as
$value
)
{
$options
[][]
=
[
'writeConcern'
=>
$value
];
}
...
...
@@ -314,6 +318,38 @@ class BucketFunctionalTest extends FunctionalTestCase
$this
->
assertSameDocuments
(
$expected
,
$cursor
);
}
public
function
testFindUsesTypeMap
()
{
$this
->
bucket
->
uploadFromStream
(
'a'
,
$this
->
createStream
(
'foo'
));
$cursor
=
$this
->
bucket
->
find
();
$fileDocument
=
current
(
$cursor
->
toArray
());
$this
->
assertInstanceOf
(
'MongoDB\Model\BSONDocument'
,
$fileDocument
);
}
public
function
testFindOne
()
{
$this
->
bucket
->
uploadFromStream
(
'a'
,
$this
->
createStream
(
'foo'
));
$this
->
bucket
->
uploadFromStream
(
'b'
,
$this
->
createStream
(
'foobar'
));
$this
->
bucket
->
uploadFromStream
(
'c'
,
$this
->
createStream
(
'foobarbaz'
));
$fileDocument
=
$this
->
bucket
->
findOne
(
[
'length'
=>
[
'$lte'
=>
6
]],
[
'projection'
=>
[
'filename'
=>
1
,
'length'
=>
1
,
'_id'
=>
0
,
],
'sort'
=>
[
'length'
=>
-
1
],
]
);
$this
->
assertInstanceOf
(
'MongoDB\Model\BSONDocument'
,
$fileDocument
);
$this
->
assertSameDocument
([
'filename'
=>
'b'
,
'length'
=>
6
],
$fileDocument
);
}
public
function
testGetBucketNameWithCustomValue
()
{
$bucket
=
new
Bucket
(
$this
->
manager
,
$this
->
getDatabaseName
(),
[
'bucketName'
=>
'custom_fs'
]);
...
...
@@ -331,6 +367,18 @@ class BucketFunctionalTest extends FunctionalTestCase
$this
->
assertEquals
(
$this
->
getDatabaseName
(),
$this
->
bucket
->
getDatabaseName
());
}
public
function
testGetFileDocumentForStreamUsesTypeMap
()
{
$metadata
=
[
'foo'
=>
'bar'
];
$stream
=
$this
->
bucket
->
openUploadStream
(
'filename'
,
[
'_id'
=>
1
,
'metadata'
=>
$metadata
]);
$fileDocument
=
$this
->
bucket
->
getFileDocumentForStream
(
$stream
);
$this
->
assertInstanceOf
(
'MongoDB\Model\BSONDocument'
,
$fileDocument
);
$this
->
assertInstanceOf
(
'MongoDB\Model\BSONDocument'
,
$fileDocument
[
'metadata'
]);
$this
->
assertSame
([
'foo'
=>
'bar'
],
$fileDocument
[
'metadata'
]
->
getArrayCopy
());
}
public
function
testGetFileDocumentForStreamWithReadableStream
()
{
$metadata
=
[
'foo'
=>
'bar'
];
...
...
@@ -366,6 +414,16 @@ class BucketFunctionalTest extends FunctionalTestCase
$this
->
bucket
->
getFileDocumentForStream
(
$stream
);
}
public
function
testGetFileIdForStreamUsesTypeMap
()
{
$stream
=
$this
->
bucket
->
openUploadStream
(
'filename'
,
[
'_id'
=>
[
'x'
=>
1
]]);
$id
=
$this
->
bucket
->
getFileIdForStream
(
$stream
);
$this
->
assertInstanceOf
(
'MongoDB\Model\BSONDocument'
,
$id
);
$this
->
assertSame
([
'x'
=>
1
],
$id
->
getArrayCopy
());
}
public
function
testGetFileIdForStreamWithReadableStream
()
{
$id
=
$this
->
bucket
->
uploadFromStream
(
'filename'
,
$this
->
createStream
(
'foobar'
));
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment