-
Notifications
You must be signed in to change notification settings - Fork 266
PHPLIB-1206 Allow global registration of GridFS buckets with gridfs://
protocol
#1138
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
=========================================================== | ||
MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias() | ||
=========================================================== | ||
|
||
.. versionadded:: 1.18 | ||
|
||
.. default-domain:: mongodb | ||
|
||
.. contents:: On this page | ||
:local: | ||
:backlinks: none | ||
:depth: 1 | ||
:class: singlecol | ||
|
||
Definition | ||
---------- | ||
|
||
.. phpmethod:: MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias() | ||
|
||
Registers an alias for the bucket, which enables files within the bucket to | ||
be accessed using a basic filename string (e.g. | ||
`gridfs://<bucket-alias>/<filename>`). | ||
|
||
.. code-block:: php | ||
|
||
function registerGlobalStreamWrapperAlias(string $alias): void | ||
|
||
Parameters | ||
---------- | ||
|
||
``$alias`` : array | ||
A non-empty string used to identify the GridFS bucket when accessing files | ||
using the ``gridfs://`` stream wrapper. | ||
|
||
Behavior | ||
-------- | ||
|
||
After registering an alias for the bucket, the most recent revision of a file | ||
can be accessed using a filename string in the form ``gridfs://<bucket-alias>/<filename>``. | ||
|
||
Supported stream functions: | ||
|
||
- :php:`copy() <copy>` | ||
- :php:`file_exists() <file_exists>` | ||
- :php:`file_get_contents() <file_get_contents>` | ||
- :php:`file_put_contents() <file_put_contents>` | ||
- :php:`filemtime() <filemtime>` | ||
- :php:`filesize() <filesize>` | ||
- :php:`file() <file>` | ||
- :php:`fopen() <fopen>` (with "r", "rb", "w", and "wb" modes) | ||
|
||
In read mode, the stream context can contain the option ``gridfs['revision']`` | ||
to specify the revision number of the file to read. If omitted, the most recent | ||
revision is read (revision ``-1``). | ||
|
||
In write mode, the stream context can contain the option ``gridfs['chunkSizeBytes']``. | ||
If omitted, the defaults are inherited from the ``Bucket`` instance option. | ||
|
||
Example | ||
------- | ||
|
||
Read and write to a GridFS bucket using the ``gridfs://`` stream wrapper | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
The following example demonstrates how to register an alias for a GridFS bucket | ||
and use the functions ``file_exists()``, ``file_get_contents()``, and | ||
``file_put_contents()`` to read and write to the bucket. | ||
|
||
Each call to these functions makes a request to the server. | ||
|
||
.. code-block:: php | ||
|
||
<?php | ||
|
||
$database = (new MongoDB\Client)->selectDatabase('test'); | ||
$bucket = $database->selectGridFSBucket(); | ||
|
||
$bucket->registerGlobalStreamWrapperAlias('mybucket'); | ||
|
||
var_dump(file_exists('gridfs://mybucket/hello.txt')); | ||
|
||
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!'); | ||
|
||
var_dump(file_exists('gridfs://mybucket/hello.txt')); | ||
|
||
echo file_get_contents('gridfs://mybucket/hello.txt'); | ||
|
||
The output would then resemble: | ||
|
||
.. code-block:: none | ||
|
||
bool(false) | ||
bool(true) | ||
Hello, GridFS! | ||
|
||
Read a specific revision of a file | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
Using a stream context, you can specify the revision number of the file to | ||
read. If omitted, the most recent revision is read. | ||
|
||
.. code-block:: php | ||
|
||
<?php | ||
|
||
$database = (new MongoDB\Client)->selectDatabase('test'); | ||
$bucket = $database->selectGridFSBucket(); | ||
|
||
$bucket->registerGlobalStreamWrapperAlias('mybucket'); | ||
|
||
// Creating revision 0 | ||
$handle = fopen('gridfs://mybucket/hello.txt', 'w'); | ||
fwrite($handle, 'Hello, GridFS! (v0)'); | ||
fclose($handle); | ||
|
||
// Creating revision 1 | ||
$handle = fopen('gridfs://mybucket/hello.txt', 'w'); | ||
fwrite($handle, 'Hello, GridFS! (v1)'); | ||
fclose($handle); | ||
|
||
// Read revision 0 | ||
$context = stream_context_create([ | ||
'gridfs' => ['revision' => 0], | ||
]); | ||
$handle = fopen('gridfs://mybucket/hello.txt', 'r', false, $context); | ||
echo fread($handle, 1024); | ||
|
||
The output would then resemble: | ||
|
||
.. code-block:: none | ||
|
||
Hello, GridFS! (v0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
<?php | ||
|
||
/** | ||
* For applications that need to interact with GridFS using only a filename string, | ||
* a bucket can be registered with an alias. Files can then be accessed using the | ||
* following pattern: gridfs://<bucket-alias>/<filename> | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MongoDB\Examples; | ||
|
||
use MongoDB\Client; | ||
|
||
use function file_exists; | ||
use function file_get_contents; | ||
use function file_put_contents; | ||
use function getenv; | ||
use function stream_context_create; | ||
|
||
use const PHP_EOL; | ||
|
||
require __DIR__ . '/../vendor/autoload.php'; | ||
|
||
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); | ||
$bucket = $client->test->selectGridFSBucket(); | ||
$bucket->drop(); | ||
|
||
// Register the alias "mybucket" for default bucket of the "test" database | ||
$bucket->registerGlobalStreamWrapperAlias('mybucket'); | ||
|
||
echo 'File exists: '; | ||
echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no'; | ||
echo PHP_EOL; | ||
|
||
echo 'Writing file'; | ||
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!'); | ||
echo PHP_EOL; | ||
|
||
echo 'File exists: '; | ||
echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no'; | ||
echo PHP_EOL; | ||
|
||
echo 'Reading file: '; | ||
echo file_get_contents('gridfs://mybucket/hello.txt'); | ||
echo PHP_EOL; | ||
|
||
echo 'Writing new version of the file'; | ||
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS! (v2)'); | ||
echo PHP_EOL; | ||
|
||
echo 'Reading new version of the file: '; | ||
echo file_get_contents('gridfs://mybucket/hello.txt'); | ||
echo PHP_EOL; | ||
|
||
echo 'Reading previous version of the file: '; | ||
$context = stream_context_create(['gridfs' => ['revision' => -2]]); | ||
echo file_get_contents('gridfs://mybucket/hello.txt', false, $context); | ||
echo PHP_EOL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.