MongoFileService
File upload service backed by GridFS.
use MongoHelperRequires the mongodb/mongodb composer package to be installed.
- Copyright
- 2015-2016 Appertly
- License
- Apache-2.0
Constructors / Destructors
| Returns | Signature and Description | 
|---|---|
| void | __construct()Creates a new MongoFileService | 
Instance Methods
| Returns | Signature and Description | 
|---|---|
| void | delete()Deletes a stored file. | 
| StreamInterface | messageStream()Gets the file as a PSR-7 Stream. | 
| ?stdClass | read()Gets a stored file. | 
| Traversable<stdClass> | readAll()Finds several files by some arbitrary criteria. | 
| resource | resource()Gets a readable stream resource for the given ID. | 
| ObjectID | store(,)Stores an uploaded file. | 
| void | stream(,)Efficiently writes the contents of a file to a Stream. | 
| Returns | Signature and Description | 
|---|---|
| Ta | Executes something in the context of the collection. | 
| Ta | Makes sure a document isn't null. | 
| UTCDateTime | now()Gets the current time. | 
| ?UTCDateTime | toDate()Tries to parse a date. | 
| ObjectID | toId()Transforms a literal into a MongoDB ObjectId. | 
| ConstVector<ObjectID> | toIds()Transforms literals into MongoDB ObjectIds. | 
Method Details
__construct
__construct(Bucket $bucketCreates a new MongoFileService
| Name | Type | Description | 
|---|---|---|
| $bucket | Bucket | The GridFS Bucket | 
delete
delete(mixed $idDeletes a stored file.
| Name | Type | Description | 
|---|---|---|
| $id | mixed | The document identifier, either a string or  | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Unretrievable | If the document doesn't exist | 
| Generic | If any other database problem occurs | 
doExecute
doExecute<Ta>((function(Bucket):Ta) $cbExecutes something in the context of the collection.
Exceptions are caught and translated.
| Name | Type | Description | 
|---|---|---|
| $cb | (function(Bucket):Ta) | The closure to execute, takes the Bucket | 
| Type | Description | 
|---|---|
| Ta | Whatever the function returns, this method also returns | 
| Type | Conditions | 
|---|---|
| Exception | If a database problem occurs | 
ensure
Inherited from MongoHelper
ensure<Ta>(mixed $id?Ta $documentMakes sure a document isn't null.
| Name | Type | Description | 
|---|---|---|
| $id | mixed | The document identifier, either a  | 
| $document | ?Ta | The document to check | 
| Type | Description | 
|---|---|
| Ta | Returns  | 
| Type | Conditions | 
|---|---|
| Unretrievable | if the document is null | 
messageStream
messageStream(mixed $idGets the file as a PSR-7 Stream.
| Name | Type | Description | 
|---|---|---|
| $id | mixed | The document identifier, either a string or  | 
| Type | Description | 
|---|---|
| StreamInterface | The readable stream | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Unretrievable | If the document doesn't exist | 
| Generic | If any other database problem occurs | 
now
Inherited from MongoHelper
now()Gets the current time.
| Type | Description | 
|---|---|
| UTCDateTime | The current time | 
read
read(mixed $idGets a stored file.
| Name | Type | Description | 
|---|---|---|
| $id | mixed | The document identifier, either a string or  | 
| Type | Description | 
|---|---|
| ?stdClass | The stored file | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Unretrievable | If the result cannot be retrieved | 
| Generic | If any other database problem occurs | 
readAll
readAll(ConstMap<string,mixed> $criteriaFinds several files by some arbitrary criteria.
| Name | Type | Description | 
|---|---|---|
| $criteria | ConstMap<string,mixed> | Field to value pairs | 
| Type | Description | 
|---|---|
| Traversable<stdClass> | The objects found | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Unretrievable | If the result cannot be retrieved | 
| Generic | If any other database problem occurs | 
resource
resource(mixed $idGets a readable stream resource for the given ID.
| Name | Type | Description | 
|---|---|---|
| $id | mixed | The document identifier, either a string or  | 
| Type | Description | 
|---|---|
| resource | The readable stream | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Unretrievable | If the document doesn't exist | 
| Generic | If any other database problem occurs | 
store
store(UploadedFileInterface $fileConstMap<string,mixed> $metadataStores an uploaded file.
You should specify contentType in the metadata Map.
| Name | Type | Description | 
|---|---|---|
| $file | UploadedFileInterface | The uploaded file | 
| $metadata | ConstMap<string,mixed> | Any additional fields to persist. At the very least, try to supply  | 
| Type | Description | 
|---|---|
| ObjectID | The document ID of the stored file | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Violating | If a constraint is violated | 
| Generic | If any other database problem occurs | 
stream
stream(stdClass $fileStreamInterface $streamEfficiently writes the contents of a file to a Stream.
| Name | Type | Description | 
|---|---|---|
| $file | stdClass | The file | 
| $stream | StreamInterface | The stream | 
| Type | Conditions | 
|---|---|
| Unreachable | If the connection fails | 
| Violating | If a constraint is violated | 
| Generic | If any other database problem occurs | 
toDate
Inherited from MongoHelper
toDate(mixed $dateTries to parse a date.
| Name | Type | Description | 
|---|---|---|
| $date | mixed | The possible string date value, a string, a         | 
| Type | Description | 
|---|---|
| ?UTCDateTime | The MongoDB datetime or null | 
toId
Inherited from MongoHelper
toId(mixed $idTransforms a literal into a MongoDB ObjectId.
| Name | Type | Description | 
|---|---|---|
| $id | mixed | If it's an  | 
| Type | Description | 
|---|---|
| ObjectID | The ObjectID | 
toIds
Inherited from MongoHelper
toIds(ConstVector<mixed> $idsTransforms literals into MongoDB ObjectIds.
| Name | Type | Description | 
|---|---|---|
| $ids | ConstVector<mixed> | Goes through each entry, converts to  | 
| Type | Description | 
|---|---|
| ConstVector<ObjectID> | The ObjectIDs |