Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHPLIB-233: Add examples for each GridFS API method #527

Merged
merged 1 commit into from Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-delete.txt
Expand Up @@ -37,4 +37,19 @@ Behavior
If the files collection document is not found, this method will still attempt to
delete orphaned chunks.

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);

$id = $bucket->uploadFromStream('filename', $stream);

$bucket->delete($id);
27 changes: 25 additions & 2 deletions docs/reference/method/MongoDBGridFSBucket-downloadToStream.txt
Expand Up @@ -26,15 +26,38 @@ Definition

.. include:: /includes/apiargs/MongoDBGridFSBucket-method-downloadToStream-param.rst

.. todo: add examples

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-gridfs-filenotfoundexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);

$id = $bucket->uploadFromStream('filename', $stream);

$destination = fopen('php://temp', 'w+b');

$bucket->downloadToStream($id, $destination);

var_dump(stream_get_contents($destination, -1, 0));

The output would then resemble::

string(6) "foobar"

See Also
--------

Expand Down
Expand Up @@ -30,15 +30,38 @@ Definition

.. include:: /includes/apiargs/MongoDBGridFSBucket-method-downloadToStreamByName-option.rst

.. todo: add examples

Errors/Exceptions
-----------------

.. include:: /includes/extracts/error-gridfs-filenotfoundexception.rst
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);

$bucket->uploadFromStream('filename', $stream);

$destination = fopen('php://temp', 'w+b');

$bucket->downloadToStreamByName('filename', $destination);

var_dump(stream_get_contents($destination, -1, 0));

The output would then resemble::

string(6) "foobar"

See Also
--------

Expand Down
19 changes: 18 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-drop.txt
Expand Up @@ -26,4 +26,21 @@ Errors/Exceptions

.. include:: /includes/extracts/error-driver-runtimeexception.rst

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$database = (new MongoDB\Client)->test;

$bucket = $database->selectGridFSBucket();

$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);

$bucket->uploadFromStream('filename', $stream);

$bucket->drop();
44 changes: 43 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-find.txt
Expand Up @@ -46,7 +46,49 @@ Behavior

.. include:: /includes/extracts/note-bson-comparison.rst

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);

$bucket->uploadFromStream('b', $stream);

$cursor = $bucket->find(
['length' => ['$lte' => 6]],
[
'projection' => [
'filename' => 1,
'length' => 1,
'_id' => 0,
],
'sort' => ['length' => -1],
]
);

var_dump($cursor->toArray());

The output would then resemble::

array(1) {
[0]=>
object(MongoDB\Model\BSONDocument)#3015 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["filename"]=>
string(1) "b"
["length"]=>
int(6)
}
}
}

See Also
--------
Expand Down
41 changes: 40 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-findOne.txt
Expand Up @@ -49,7 +49,46 @@ Behavior

.. include:: /includes/extracts/note-bson-comparison.rst

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = fopen('php://temp', 'w+b');
fwrite($stream, "foobar");
rewind($stream);

$bucket->uploadFromStream('b', $stream);

$fileDocument = $bucket->findOne(
['length' => ['$lte' => 6]],
[
'projection' => [
'filename' => 1,
'length' => 1,
'_id' => 0,
],
'sort' => ['length' => -1],
]
);

var_dump($fileDocument);

The output would then resemble::

object(MongoDB\Model\BSONDocument)#3004 (1) {
["storage":"ArrayObject":private]=>
array(2) {
["filename"]=>
string(1) "b"
["length"]=>
int(6)
}
}

See Also
--------
Expand Down
15 changes: 14 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-getBucketName.txt
Expand Up @@ -26,4 +26,17 @@ Return Values

The name of this bucket as a string.

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

var_dump($bucket->getBucketName());

The output would then resemble::

string(2) "fs"
15 changes: 14 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-getChunkSizeBytes.txt
Expand Up @@ -28,4 +28,17 @@ Return Values

The chunk size of this bucket in bytes.

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

var_dump($bucket->getChunkSizeBytes());

The output would then resemble::

int(261120)
Expand Up @@ -28,4 +28,17 @@ Return Values

A :phpclass:`MongoDB\\Collection` object for the chunks collection.

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

var_dump((string) $bucket->getChunksCollection());

The output would then resemble::

string(14) "test.fs.chunks"
16 changes: 15 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-getDatabaseName.txt
Expand Up @@ -26,4 +26,18 @@ Return Values

The name of the database containing this bucket as a string.

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

var_dump($bucket->getDatabaseName());

The output would then resemble::

string(4) "test"

Expand Up @@ -37,7 +37,39 @@ Errors/Exceptions
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = $bucket->openUploadStream('filename');

$fileDocument = $bucket->getFileDocumentForStream($stream);

var_dump($fileDocument);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives me two errors:

Warning: hash_final(): supplied resource is not a valid Hash Context resource in /home/jmikola/workspace/mongodb/phplib/src/GridFS/WritableStream.php on line 222

Fatal error: Uncaught Error: Class 'MongoDB\Operation\FindOne' not found in /home/jmikola/workspace/mongodb/phplib/src/Collection.php:594
Stack trace:
#0 /home/jmikola/workspace/mongodb/phplib/src/GridFS/CollectionWrapper.php(333): MongoDB\Collection->findOne(Array, Array)
#1 /home/jmikola/workspace/mongodb/phplib/src/GridFS/CollectionWrapper.php(317): MongoDB\GridFS\CollectionWrapper->isFilesCollectionEmpty()
#2 /home/jmikola/workspace/mongodb/phplib/src/GridFS/CollectionWrapper.php(254): MongoDB\GridFS\CollectionWrapper->ensureIndexes()
#3 /home/jmikola/workspace/mongodb/phplib/src/GridFS/WritableStream.php(229): MongoDB\GridFS\CollectionWrapper->insertFile(Array)
#4 /home/jmikola/workspace/mongodb/phplib/src/GridFS/WritableStream.php(137): MongoDB\GridFS\WritableStream->fileCollectionInsert()
#5 /home/jmikola/workspace/mongodb/phplib/src/GridFS/StreamWrapper.php(73): MongoDB\GridFS\WritableStream->close()
#6 [internal function]: MongoDB\GridFS\StreamWrapper->stream_close()
#7 {main}
  thrown in /home/jmikola/workspace/mongodb/phplib/src/Collection.php on line 594

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Opened PHPLIB-345 to track this.

For now, let's just fclose($stream) after the var_dump().


fclose($stream);

The output would then resemble::

object(MongoDB\Model\BSONDocument)#4956 (1) {
["storage":"ArrayObject":private]=>
array(3) {
["_id"]=>
object(MongoDB\BSON\ObjectId)#4955 (1) {
["oid"]=>
string(24) "5acfb05b7e21e83b5a29037c"
}
["chunkSize"]=>
int(261120)
["filename"]=>
string(8) "filename"
}
}

See Also
--------
Expand Down
24 changes: 23 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-getFileIdForStream.txt
Expand Up @@ -38,7 +38,29 @@ Errors/Exceptions
.. include:: /includes/extracts/error-invalidargumentexception.rst
.. include:: /includes/extracts/error-driver-runtimeexception.rst

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$stream = $bucket->openUploadStream('filename');

$id = $bucket->getFileIdForStream($stream);

var_dump($id);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add fclose($stream) after the var_dump() for now.


fclose($stream);

The output would then resemble::

object(MongoDB\BSON\ObjectId)#3005 (1) {
["oid"]=>
string(24) "5acfb37d7e21e83cdb3e1583"
}

See Also
--------
Expand Down
18 changes: 17 additions & 1 deletion docs/reference/method/MongoDBGridFSBucket-getFilesCollection.txt
Expand Up @@ -28,4 +28,20 @@ Return Values

A :phpclass:`MongoDB\\Collection` object for the files collection.

.. todo: add examples
Examples
--------

.. code-block:: php

<?php

$bucket = (new MongoDB\Client)->test->selectGridFSBucket();

$filesCollection = $bucket->getFilesCollection();

var_dump($filesCollection->getCollectionName());

The output would then resemble::

string(8) "fs.files"