Skip to content

Commit 971336a

Browse files
authored
PHPLIB-1206 Add bucket alises for context resolver using GridFS StreamWrapper (#1138)
1 parent c51f6fc commit 971336a

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

source/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: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
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+
Registers an alias for the bucket, which enables files within the bucket to
21+
be accessed using a basic filename string (e.g.
22+
`gridfs://<bucket-alias>/<filename>`).
23+
24+
.. code-block:: php
25+
26+
function registerGlobalStreamWrapperAlias(string $alias): void
27+
28+
Parameters
29+
----------
30+
31+
``$alias`` : array
32+
A non-empty string used to identify the GridFS bucket when accessing files
33+
using the ``gridfs://`` stream wrapper.
34+
35+
Behavior
36+
--------
37+
38+
After registering an alias for the bucket, the most recent revision of a file
39+
can be accessed using a filename string in the form ``gridfs://<bucket-alias>/<filename>``.
40+
41+
Supported stream functions:
42+
43+
- :php:`copy() <copy>`
44+
- :php:`file_exists() <file_exists>`
45+
- :php:`file_get_contents() <file_get_contents>`
46+
- :php:`file_put_contents() <file_put_contents>`
47+
- :php:`filemtime() <filemtime>`
48+
- :php:`filesize() <filesize>`
49+
- :php:`file() <file>`
50+
- :php:`fopen() <fopen>` (with "r", "rb", "w", and "wb" modes)
51+
52+
In read mode, the stream context can contain the option ``gridfs['revision']``
53+
to specify the revision number of the file to read. If omitted, the most recent
54+
revision is read (revision ``-1``).
55+
56+
In write mode, the stream context can contain the option ``gridfs['chunkSizeBytes']``.
57+
If omitted, the defaults are inherited from the ``Bucket`` instance option.
58+
59+
Example
60+
-------
61+
62+
Read and write to a GridFS bucket using the ``gridfs://`` stream wrapper
63+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
64+
65+
The following example demonstrates how to register an alias for a GridFS bucket
66+
and use the functions ``file_exists()``, ``file_get_contents()``, and
67+
``file_put_contents()`` to read and write to the bucket.
68+
69+
Each call to these functions makes a request to the server.
70+
71+
.. code-block:: php
72+
73+
<?php
74+
75+
$database = (new MongoDB\Client)->selectDatabase('test');
76+
$bucket = $database->selectGridFSBucket();
77+
78+
$bucket->registerGlobalStreamWrapperAlias('mybucket');
79+
80+
var_dump(file_exists('gridfs://mybucket/hello.txt'));
81+
82+
file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!');
83+
84+
var_dump(file_exists('gridfs://mybucket/hello.txt'));
85+
86+
echo file_get_contents('gridfs://mybucket/hello.txt');
87+
88+
The output would then resemble:
89+
90+
.. code-block:: none
91+
92+
bool(false)
93+
bool(true)
94+
Hello, GridFS!
95+
96+
Read a specific revision of a file
97+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
98+
99+
Using a stream context, you can specify the revision number of the file to
100+
read. If omitted, the most recent revision is read.
101+
102+
.. code-block:: php
103+
104+
<?php
105+
106+
$database = (new MongoDB\Client)->selectDatabase('test');
107+
$bucket = $database->selectGridFSBucket();
108+
109+
$bucket->registerGlobalStreamWrapperAlias('mybucket');
110+
111+
// Creating revision 0
112+
$handle = fopen('gridfs://mybucket/hello.txt', 'w');
113+
fwrite($handle, 'Hello, GridFS! (v0)');
114+
fclose($handle);
115+
116+
// Creating revision 1
117+
$handle = fopen('gridfs://mybucket/hello.txt', 'w');
118+
fwrite($handle, 'Hello, GridFS! (v1)');
119+
fclose($handle);
120+
121+
// Read revision 0
122+
$context = stream_context_create([
123+
'gridfs' => ['revision' => 0],
124+
]);
125+
$handle = fopen('gridfs://mybucket/hello.txt', 'r', false, $context);
126+
echo fread($handle, 1024);
127+
128+
The output would then resemble:
129+
130+
.. code-block:: none
131+
132+
Hello, GridFS! (v0)

0 commit comments

Comments
 (0)