Skip to content

Commit d16f1f0

Browse files
committed
Add docs for registerGlobalStreamWrapperAlias
1 parent db87f76 commit d16f1f0

File tree

5 files changed

+163
-3
lines changed

5 files changed

+163
-3
lines changed

docs/reference/class/MongoDBGridFSBucket.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ Methods
5555
/reference/method/MongoDBGridFSBucket-openDownloadStream
5656
/reference/method/MongoDBGridFSBucket-openDownloadStreamByName
5757
/reference/method/MongoDBGridFSBucket-openUploadStream
58+
/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias
5859
/reference/method/MongoDBGridFSBucket-rename
5960
/reference/method/MongoDBGridFSBucket-uploadFromStream
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
============================================
2+
MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias()
3+
============================================
4+
5+
.. versionadded:: 1.18
6+
7+
.. default-domain:: mongodb
8+
9+
.. contents:: On this page
10+
:local:
11+
:backlinks: none
12+
:depth: 1
13+
:class: singlecol
14+
15+
Definition
16+
----------
17+
18+
.. phpmethod:: MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias()
19+
20+
Register an alias to enable basic filename access for a GridFS bucket
21+
22+
.. code-block:: php
23+
24+
function registerGlobalStreamWrapperAlias(
25+
string $alias
26+
): void
27+
28+
29+
Parameters
30+
----------
31+
32+
``$alias`` : array
33+
A non-empty string used to identify the GridFS bucket when accessing files
34+
using the ``gridfs://`` stream wrapper.
35+
36+
37+
38+
Behavior
39+
--------
40+
41+
Files can then be accessed using the following pattern: ``gridfs://<bucket-alias>/<filename>``
42+
43+
Supported stream functions:
44+
45+
- ``file_exists()``
46+
- ``file_get_contents()``
47+
- ``file_put_contents()``
48+
- ``fopen()`` (with "r", "rb", "w", and "wb" modes)
49+
- ``filesize()``
50+
- ``filemtime()``
51+
52+
Example
53+
-------
54+
55+
.. code-block:: php
56+
57+
<?php
58+
59+
$database = (new MongoDB\Client)->selectDatabase('test');
60+
$bucket = $database->selectGridFSBucket();
61+
62+
$bucket->registerGlobalStreamWrapperAlias('mybucket');
63+
64+
var_dump(file_exists('gridfs://mybucket/hello.txt'));
65+
66+
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!');
67+
68+
var_dump(file_exists('gridfs://mybucket/hello.txt'));
69+
70+
echo file_get_contents('gridfs://mybucket/hello.txt');
71+
72+
The output would then resemble:
73+
74+
.. code-block:: none
75+
76+
bool(false)
77+
bool(true)
78+
Hello, GridFS!
79+
80+

examples/gridfs-stream-wrapper.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/**
4+
* For applications that need to interact with GridFS using only a filename string,
5+
* a bucket can be registered with an alias. Files can then be accessed using the
6+
* following pattern:
7+
* gridfs://<bucket-alias>/<filename>
8+
*/
9+
10+
declare(strict_types=1);
11+
12+
namespace MongoDB\Examples;
13+
14+
use MongoDB\Client;
15+
16+
use function file_exists;
17+
use function file_get_contents;
18+
use function file_put_contents;
19+
use function getenv;
20+
use function stream_context_create;
21+
22+
use const PHP_EOL;
23+
24+
require __DIR__ . '/../vendor/autoload.php';
25+
26+
$client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/');
27+
$bucket = $client->test->selectGridFSBucket();
28+
$bucket->drop();
29+
30+
// Register the alias "mybucket" for default bucket of the "test" database
31+
$bucket->registerGlobalStreamWrapperAlias('mybucket');
32+
33+
echo 'File exists: ';
34+
echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no';
35+
echo PHP_EOL;
36+
37+
echo 'Writing file';
38+
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!');
39+
echo PHP_EOL;
40+
41+
echo 'File exists: ';
42+
echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no';
43+
echo PHP_EOL;
44+
45+
echo 'Reading file: ';
46+
echo file_get_contents('gridfs://mybucket/hello.txt');
47+
echo PHP_EOL;
48+
49+
echo 'Writing new version of the file';
50+
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS! (v2)');
51+
echo PHP_EOL;
52+
53+
echo 'Reading new version of the file: ';
54+
echo file_get_contents('gridfs://mybucket/hello.txt');
55+
echo PHP_EOL;
56+
57+
echo 'Reading previous version of the file: ';
58+
$context = stream_context_create(['gridfs' => ['revision' => -2]]);
59+
echo file_get_contents('gridfs://mybucket/hello.txt', false, $context);
60+
echo PHP_EOL;

src/GridFS/Bucket.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -591,11 +591,15 @@ public function openUploadStream(string $filename, array $options = [])
591591
}
592592

593593
/**
594-
* For legacy applications that needs to open stream from a string, the bucket can be registered in the stream
595-
* wrapper to be used by default when no GridFS context is provided.
596-
* Supported file name must follow this pattern:
594+
* Register an alias to enable basic filename access for this bucket
595+
*
596+
* For applications that need to interact with GridFS using only a filename
597+
* string, a bucket can be registered with an alias. Files can then be
598+
* accessed using the following pattern:
597599
* gridfs://<bucket-alias>/<filename>
598600
*
601+
* Read operations will always target the most recent revision of a file.
602+
*
599603
* @param non-empty-string string $alias The alias to use for the bucket
600604
*/
601605
public function registerGlobalStreamWrapperAlias(string $alias): void

tests/ExamplesTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,21 @@ public static function provideExamples(): Generator
127127
)
128128
OUTPUT;
129129

130+
$expectedOutput = <<<'OUTPUT'
131+
File exists: no
132+
Writing file
133+
File exists: yes
134+
Reading file: Hello, GridFS!
135+
Writing new version of the file
136+
Reading new version of the file: Hello, GridFS! (v2)
137+
Reading previous version of the file: Hello, GridFS!
138+
OUTPUT;
139+
140+
yield 'gridfs-stream-wrapper' => [
141+
'file' => __DIR__ . '/../examples/gridfs-stream-wrapper.php',
142+
'expectedOutput' => $expectedOutput,
143+
];
144+
130145
yield 'persistable' => [
131146
'file' => __DIR__ . '/../examples/persistable.php',
132147
'expectedOutput' => $expectedOutput,

0 commit comments

Comments
 (0)