Skip to content

Commit 2c372cd

Browse files
committed
Improve docs
1 parent 41c915b commit 2c372cd

File tree

3 files changed

+78
-20
lines changed

3 files changed

+78
-20
lines changed

docs/reference/method/MongoDBGridFSBucket-registerGlobalStreamWrapperAlias.txt

Lines changed: 63 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
============================================
1+
===========================================================
22
MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias()
3-
============================================
3+
===========================================================
44

55
.. versionadded:: 1.18
66

@@ -17,14 +17,13 @@ Definition
1717

1818
.. phpmethod:: MongoDB\\GridFS\\Bucket::registerGlobalStreamWrapperAlias()
1919

20-
Register an alias to enable basic filename access for a GridFS bucket
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>`).
2123

2224
.. code-block:: php
2325

24-
function registerGlobalStreamWrapperAlias(
25-
string $alias
26-
): void
27-
26+
function registerGlobalStreamWrapperAlias(string $alias): void
2827

2928
Parameters
3029
----------
@@ -33,25 +32,43 @@ Parameters
3332
A non-empty string used to identify the GridFS bucket when accessing files
3433
using the ``gridfs://`` stream wrapper.
3534

36-
37-
3835
Behavior
3936
--------
4037

41-
Files can then be accessed using the following pattern: ``gridfs://<bucket-alias>/<filename>``
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>``.
4240

4341
Supported stream functions:
4442

43+
- ``copy()``
4544
- ``file_exists()``
4645
- ``file_get_contents()``
4746
- ``file_put_contents()``
4847
- ``filemtime()``
4948
- ``filesize()``
49+
- ``file()``
5050
- ``fopen()`` (with "r", "rb", "w", and "wb" modes)
5151

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 options ``gridfs['chunkSizeBytes']``
57+
and ``gridfs['disableMD5']`` options. If omitted, the defaults are inherited from the
58+
``Bucket`` instance options.
59+
5260
Example
5361
-------
5462

63+
Read and write to a GridFS bucket using the ``gridfs://`` stream wrapper
64+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65+
66+
The following example demonstrates how to register an alias for a GridFS bucket
67+
and use the functions ``file_exists()``, ``file_get_contents()``, and
68+
``file_put_contents()`` to read and write to the bucket.
69+
70+
Each call to these functions makes a request to the server.
71+
5572
.. code-block:: php
5673

5774
<?php
@@ -77,4 +94,40 @@ The output would then resemble:
7794
bool(true)
7895
Hello, GridFS!
7996

97+
Read a specific revision of a file
98+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99+
100+
Using a stream context, you can specify the revision number of the file to
101+
read. If omitted, the most recent revision is read.
102+
103+
.. code-block:: php
104+
105+
<?php
106+
107+
$database = (new MongoDB\Client)->selectDatabase('test');
108+
$bucket = $database->selectGridFSBucket();
109+
110+
$bucket->registerGlobalStreamWrapperAlias('mybucket');
111+
112+
// Creating revision 0
113+
$handle = fopen('gridfs://mybucket/hello.txt', 'w');
114+
fwrite($handle, 'Hello, GridFS! (v0)');
115+
fclose($handle);
116+
117+
// Creating revision 1
118+
$handle = fopen('gridfs://mybucket/hello.txt', 'w');
119+
fwrite($handle, 'Hello, GridFS! (v1)');
120+
fclose($handle);
121+
122+
// Read revision 0
123+
$context = stream_context_create([
124+
'gridfs' => ['revision' => 0],
125+
]);
126+
$handle = fopen('gridfs://mybucket/hello.txt', 'r', false, $context);
127+
echo fread($handle, 1024);
128+
129+
The output would then resemble:
130+
131+
.. code-block:: none
80132

133+
Hello, GridFS! (v0)

tests/GridFS/FunctionalTestCase.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use MongoDB\Collection;
66
use MongoDB\GridFS\Bucket;
7-
use MongoDB\GridFS\StreamWrapper;
87
use MongoDB\Tests\FunctionalTestCase as BaseFunctionalTestCase;
98

109
use function fopen;
@@ -35,13 +34,6 @@ public function setUp(): void
3534
$this->filesCollection = $this->createCollection($this->getDatabaseName(), 'fs.files');
3635
}
3736

38-
public function tearDown(): void
39-
{
40-
StreamWrapper::setContextResolver('default', null);
41-
42-
parent::tearDown();
43-
}
44-
4537
/**
4638
* Asserts that a variable is a stream containing the expected data.
4739
*

tests/GridFS/StreamWrapperFunctionalTest.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
use MongoDB\BSON\UTCDateTime;
77
use MongoDB\GridFS\Exception\FileNotFoundException;
88
use MongoDB\GridFS\Exception\LogicException;
9+
use MongoDB\GridFS\StreamWrapper;
910

11+
use function copy;
1012
use function fclose;
1113
use function feof;
1214
use function file_exists;
@@ -51,6 +53,13 @@ public function setUp(): void
5153
]);
5254
}
5355

56+
public function tearDown(): void
57+
{
58+
StreamWrapper::setContextResolver('bucket', null);
59+
60+
parent::tearDown();
61+
}
62+
5463
public function testReadableStreamClose(): void
5564
{
5665
$stream = $this->bucket->openDownloadStream('length-10');
@@ -313,9 +322,9 @@ public function testFileNoFoundWithContextResolver(): void
313322
public function testFileNoFoundWithoutDefaultResolver(): void
314323
{
315324
$this->expectException(LogicException::class);
316-
$this->expectExceptionMessage('GridFS stream wrapper has no bucket alias: "not-registered-alias"');
325+
$this->expectExceptionMessage('GridFS stream wrapper has no bucket alias: "bucket"');
317326

318-
fopen('gridfs://not-registered-alias/filename', 'w');
327+
fopen('gridfs://bucket/filename', 'w');
319328
}
320329

321330
public function testFileStats(): void
@@ -324,6 +333,7 @@ public function testFileStats(): void
324333
$path = 'gridfs://bucket/filename';
325334

326335
$this->assertFalse(file_exists($path));
336+
$this->assertFalse(is_file($path));
327337

328338
$time = time();
329339
$this->assertSame(6, file_put_contents($path, 'foobar'));
@@ -336,5 +346,8 @@ public function testFileStats(): void
336346
$this->assertSame(6, filesize($path));
337347
$this->assertGreaterThanOrEqual($time, filemtime($path));
338348
$this->assertLessThanOrEqual(time(), filemtime($path));
349+
350+
copy($path, $path . '.copy');
351+
$this->assertTrue(file_exists($path . '.copy'));
339352
}
340353
}

0 commit comments

Comments
 (0)