From cfa3b9c21a8f33b636d4424dca6998aa410226e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Tue, 21 Nov 2023 15:23:46 +0100 Subject: [PATCH 1/5] Add examples on gridfs --- examples/gridfs-stream.php | 47 ++++++++++++++++++++++++++++++++++++++ examples/gridfs-upload.php | 47 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 94 insertions(+) create mode 100644 examples/gridfs-stream.php create mode 100644 examples/gridfs-upload.php diff --git a/examples/gridfs-stream.php b/examples/gridfs-stream.php new file mode 100644 index 000000000..372f8a811 --- /dev/null +++ b/examples/gridfs-stream.php @@ -0,0 +1,47 @@ +test->selectGridFSBucket(['disableMD5' => true]); + +// Open a stream for writing, similar to fopen with mode 'w' +$stream = $bucket->openUploadStream('hello.txt'); + +for ($i = 0; $i < 1_000_000; $i++) { + fwrite($stream, 'Hello line ' . $i . PHP_EOL); +} + +// Last data are flushed to the server when the stream is closed +fclose($stream); + +// Open a stream for reading, similar to fopen with mode 'r' +$stream = $bucket->openDownloadStreamByName('hello.txt'); + +$size = 0; +while (! feof($stream)) { + $data = fread($stream, 2 ** 10); + $size += strlen($data); +} + +echo 'Read a total of ' . $size . ' bytes' . PHP_EOL; diff --git a/examples/gridfs-upload.php b/examples/gridfs-upload.php new file mode 100644 index 000000000..ef40b5653 --- /dev/null +++ b/examples/gridfs-upload.php @@ -0,0 +1,47 @@ +selectDatabase('test')->selectGridFSBucket(); + +// Create an in-memory stream, this can be any stream source like STDIN or php://input for web requests +$stream = fopen('php://temp', 'w+'); +fwrite($stream, 'Hello world!'); +rewind($stream); + +// Upload to GridFS from the stream +$id = $gridfs->uploadFromStream('hello.txt', $stream); +assert($id instanceof ObjectId); +echo 'Inserted file with ID: ' . $id . PHP_EOL; +fclose($stream); + +// Download the file and print the contents directly to STDOUT, chunk by chunk +echo 'File contents: '; +$gridfs->downloadToStreamByName('hello.txt', STDOUT); +echo PHP_EOL; + +// Delete the file +$gridfs->delete($id); From 3f2e3aada138a553ac632b00689b3351f726567b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Fri, 5 Jan 2024 10:48:06 +0100 Subject: [PATCH 2/5] Add tests on gridfs examples --- .../{atlas-search.php => atlas_search.php} | 0 .../{gridfs-stream.php => gridfs_stream.php} | 9 ++++++ ...-wrapper.php => gridfs_stream_wrapper.php} | 0 .../{gridfs-upload.php => gridfs_upload.php} | 13 +++++---- psalm-baseline.xml | 2 +- tests/ExamplesTest.php | 28 +++++++++++++++++-- 6 files changed, 43 insertions(+), 9 deletions(-) rename examples/{atlas-search.php => atlas_search.php} (100%) rename examples/{gridfs-stream.php => gridfs_stream.php} (81%) rename examples/{gridfs-stream-wrapper.php => gridfs_stream_wrapper.php} (100%) rename examples/{gridfs-upload.php => gridfs_upload.php} (73%) diff --git a/examples/atlas-search.php b/examples/atlas_search.php similarity index 100% rename from examples/atlas-search.php rename to examples/atlas_search.php diff --git a/examples/gridfs-stream.php b/examples/gridfs_stream.php similarity index 81% rename from examples/gridfs-stream.php rename to examples/gridfs_stream.php index 372f8a811..8dd99a9eb 100644 --- a/examples/gridfs-stream.php +++ b/examples/gridfs_stream.php @@ -8,8 +8,10 @@ namespace MongoDB\Examples; +use MongoDB\BSON\ObjectId; use MongoDB\Client; +use function assert; use function fclose; use function feof; use function fread; @@ -45,3 +47,10 @@ } echo 'Read a total of ' . $size . ' bytes' . PHP_EOL; + +// Retrieve the file ID to delete it +$id = $bucket->getFileIdForStream($stream); +assert($id instanceof ObjectId); +$bucket->delete($id); + +echo 'Deleted file with ID: ' . $id . PHP_EOL; diff --git a/examples/gridfs-stream-wrapper.php b/examples/gridfs_stream_wrapper.php similarity index 100% rename from examples/gridfs-stream-wrapper.php rename to examples/gridfs_stream_wrapper.php diff --git a/examples/gridfs-upload.php b/examples/gridfs_upload.php similarity index 73% rename from examples/gridfs-upload.php rename to examples/gridfs_upload.php index ef40b5653..c290be093 100644 --- a/examples/gridfs-upload.php +++ b/examples/gridfs_upload.php @@ -17,9 +17,9 @@ use function fwrite; use function getenv; use function rewind; +use function stream_get_contents; use const PHP_EOL; -use const STDOUT; require __DIR__ . '/../vendor/autoload.php'; @@ -38,10 +38,13 @@ echo 'Inserted file with ID: ' . $id . PHP_EOL; fclose($stream); -// Download the file and print the contents directly to STDOUT, chunk by chunk -echo 'File contents: '; -$gridfs->downloadToStreamByName('hello.txt', STDOUT); -echo PHP_EOL; +// Download the file and print the contents directly to an in-memory stream, chunk by chunk +$stream = fopen('php://temp', 'w+'); +$gridfs->downloadToStreamByName('hello.txt', $stream); +rewind($stream); +echo 'File contents: ' . stream_get_contents($stream) . PHP_EOL; // Delete the file $gridfs->delete($id); + +echo 'Deleted file with ID: ' . $id . PHP_EOL; diff --git a/psalm-baseline.xml b/psalm-baseline.xml index b5470ce18..972023cc9 100644 --- a/psalm-baseline.xml +++ b/psalm-baseline.xml @@ -5,7 +5,7 @@ decrypt($document->encryptedField)]]> - + name]]> diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index 9079e04a6..050c943e6 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -99,6 +99,28 @@ public static function provideExamples(): Generator 'expectedOutput' => $expectedOutput, ]; + $expectedOutput = <<<'OUTPUT' +Read a total of 17888890 bytes +Deleted file with ID: %s +OUTPUT; + + yield 'gridfs_stream' => [ + 'file' => __DIR__ . '/../examples/gridfs_stream.php', + 'expectedOutput' => $expectedOutput, + ]; + + $expectedOutput = <<<'OUTPUT' +Inserted file with ID: %s +File contents: Hello world! +Deleted file with ID: %s + +OUTPUT; + + yield 'gridfs_upload' => [ + 'file' => __DIR__ . '/../examples/gridfs_upload.php', + 'expectedOutput' => $expectedOutput, + ]; + $expectedOutput = <<<'OUTPUT' File exists: no Writing file @@ -109,8 +131,8 @@ public static function provideExamples(): Generator Reading previous version of the file: Hello, GridFS! OUTPUT; - yield 'gridfs-stream-wrapper' => [ - 'file' => __DIR__ . '/../examples/gridfs-stream-wrapper.php', + yield 'gridfs_stream_wrapper' => [ + 'file' => __DIR__ . '/../examples/gridfs_stream_wrapper.php', 'expectedOutput' => $expectedOutput, ]; @@ -243,7 +265,7 @@ public function testAtlasSearch(): void OUTPUT; - $this->assertExampleOutput(__DIR__ . '/../examples/atlas-search.php', $expectedOutput); + $this->assertExampleOutput(__DIR__ . '/../examples/atlas_search.php', $expectedOutput); } public function testChangeStream(): void From 413488c17f8b1dc2cc6f37a0e801e4406a819f60 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 8 Jan 2024 09:22:56 +0100 Subject: [PATCH 3/5] Remove usages of PHP_EOL --- examples/gridfs_stream.php | 8 +++----- examples/gridfs_stream_wrapper.php | 16 +++++++--------- examples/gridfs_upload.php | 8 +++----- 3 files changed, 13 insertions(+), 19 deletions(-) diff --git a/examples/gridfs_stream.php b/examples/gridfs_stream.php index 8dd99a9eb..12db71d8a 100644 --- a/examples/gridfs_stream.php +++ b/examples/gridfs_stream.php @@ -19,8 +19,6 @@ use function getenv; use function strlen; -use const PHP_EOL; - require __DIR__ . '/../vendor/autoload.php'; $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); @@ -31,7 +29,7 @@ $stream = $bucket->openUploadStream('hello.txt'); for ($i = 0; $i < 1_000_000; $i++) { - fwrite($stream, 'Hello line ' . $i . PHP_EOL); + fwrite($stream, 'Hello line ' . $i . "\n"); } // Last data are flushed to the server when the stream is closed @@ -46,11 +44,11 @@ $size += strlen($data); } -echo 'Read a total of ' . $size . ' bytes' . PHP_EOL; +echo 'Read a total of ' . $size . ' bytes' . "\n"; // Retrieve the file ID to delete it $id = $bucket->getFileIdForStream($stream); assert($id instanceof ObjectId); $bucket->delete($id); -echo 'Deleted file with ID: ' . $id . PHP_EOL; +echo 'Deleted file with ID: ' . $id . "\n"; diff --git a/examples/gridfs_stream_wrapper.php b/examples/gridfs_stream_wrapper.php index e07285fa0..3371022c7 100644 --- a/examples/gridfs_stream_wrapper.php +++ b/examples/gridfs_stream_wrapper.php @@ -18,8 +18,6 @@ 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/'); @@ -31,29 +29,29 @@ echo 'File exists: '; echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no'; -echo PHP_EOL; +echo "\n"; echo 'Writing file'; file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS!'); -echo PHP_EOL; +echo "\n"; echo 'File exists: '; echo file_exists('gridfs://mybucket/hello.txt') ? 'yes' : 'no'; -echo PHP_EOL; +echo "\n"; echo 'Reading file: '; echo file_get_contents('gridfs://mybucket/hello.txt'); -echo PHP_EOL; +echo "\n"; echo 'Writing new version of the file'; file_put_contents('gridfs://mybucket/hello.txt', 'Hello, GridFS! (v2)'); -echo PHP_EOL; +echo "\n"; echo 'Reading new version of the file: '; echo file_get_contents('gridfs://mybucket/hello.txt'); -echo PHP_EOL; +echo "\n"; 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; +echo "\n"; diff --git a/examples/gridfs_upload.php b/examples/gridfs_upload.php index c290be093..64bd3ea1c 100644 --- a/examples/gridfs_upload.php +++ b/examples/gridfs_upload.php @@ -19,8 +19,6 @@ use function rewind; use function stream_get_contents; -use const PHP_EOL; - require __DIR__ . '/../vendor/autoload.php'; $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); @@ -35,16 +33,16 @@ // Upload to GridFS from the stream $id = $gridfs->uploadFromStream('hello.txt', $stream); assert($id instanceof ObjectId); -echo 'Inserted file with ID: ' . $id . PHP_EOL; +echo 'Inserted file with ID: ', $id, "\n"; fclose($stream); // Download the file and print the contents directly to an in-memory stream, chunk by chunk $stream = fopen('php://temp', 'w+'); $gridfs->downloadToStreamByName('hello.txt', $stream); rewind($stream); -echo 'File contents: ' . stream_get_contents($stream) . PHP_EOL; +echo 'File contents: ', stream_get_contents($stream), "\n"; // Delete the file $gridfs->delete($id); -echo 'Deleted file with ID: ' . $id . PHP_EOL; +echo 'Deleted file with ID: ', $id, "\n"; From eac3eee7a2475afaa2e42c9bbefda53464f8a98f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 8 Jan 2024 09:53:23 +0100 Subject: [PATCH 4/5] Disable MD5 for GridFS examples, this feature is deprecated --- examples/gridfs_stream_wrapper.php | 2 +- examples/gridfs_upload.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/gridfs_stream_wrapper.php b/examples/gridfs_stream_wrapper.php index 3371022c7..f7ca70137 100644 --- a/examples/gridfs_stream_wrapper.php +++ b/examples/gridfs_stream_wrapper.php @@ -21,7 +21,7 @@ require __DIR__ . '/../vendor/autoload.php'; $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); -$bucket = $client->test->selectGridFSBucket(); +$bucket = $client->test->selectGridFSBucket(['disableMD5' => true]); $bucket->drop(); // Register the alias "mybucket" for default bucket of the "test" database diff --git a/examples/gridfs_upload.php b/examples/gridfs_upload.php index 64bd3ea1c..c53fa7c1a 100644 --- a/examples/gridfs_upload.php +++ b/examples/gridfs_upload.php @@ -23,7 +23,7 @@ $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); -$gridfs = $client->selectDatabase('test')->selectGridFSBucket(); +$gridfs = $client->selectDatabase('test')->selectGridFSBucket(['disableMD5' => true]); // Create an in-memory stream, this can be any stream source like STDIN or php://input for web requests $stream = fopen('php://temp', 'w+'); From 73a372bb8e934aae8d7c130f35e8e4ba0d36abe4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Tamarelle?= Date: Mon, 8 Jan 2024 09:54:09 +0100 Subject: [PATCH 5/5] Disable MD5 on GridFS examples --- examples/gridfs_stream.php | 2 +- examples/gridfs_stream_wrapper.php | 1 + examples/gridfs_upload.php | 3 ++- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/examples/gridfs_stream.php b/examples/gridfs_stream.php index 12db71d8a..60f102707 100644 --- a/examples/gridfs_stream.php +++ b/examples/gridfs_stream.php @@ -22,7 +22,7 @@ require __DIR__ . '/../vendor/autoload.php'; $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); - +// Disable MD5 computation for faster uploads, this feature is deprecated $bucket = $client->test->selectGridFSBucket(['disableMD5' => true]); // Open a stream for writing, similar to fopen with mode 'w' diff --git a/examples/gridfs_stream_wrapper.php b/examples/gridfs_stream_wrapper.php index f7ca70137..d8e73ea4d 100644 --- a/examples/gridfs_stream_wrapper.php +++ b/examples/gridfs_stream_wrapper.php @@ -21,6 +21,7 @@ require __DIR__ . '/../vendor/autoload.php'; $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); +// Disable MD5 computation for faster uploads, this feature is deprecated $bucket = $client->test->selectGridFSBucket(['disableMD5' => true]); $bucket->drop(); diff --git a/examples/gridfs_upload.php b/examples/gridfs_upload.php index c53fa7c1a..64a030352 100644 --- a/examples/gridfs_upload.php +++ b/examples/gridfs_upload.php @@ -23,7 +23,8 @@ $client = new Client(getenv('MONGODB_URI') ?: 'mongodb://127.0.0.1/'); -$gridfs = $client->selectDatabase('test')->selectGridFSBucket(['disableMD5' => true]); +// Disable MD5 computation for faster uploads, this feature is deprecated +$gridfs = $client->test->selectGridFSBucket(['disableMD5' => true]); // Create an in-memory stream, this can be any stream source like STDIN or php://input for web requests $stream = fopen('php://temp', 'w+');