Skip to content

Commit 99f9632

Browse files
committed
Keep things private and expose Bucket::registerAsDefaultStreamWrapper()
1 parent 94160c1 commit 99f9632

File tree

5 files changed

+31
-36
lines changed

5 files changed

+31
-36
lines changed

src/GridFS/Bucket.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,12 @@ public function __debugInfo()
227227
];
228228
}
229229

230+
public function registerAsDefaultStreamWrapper(): void
231+
{
232+
// Use a closure to expose the private method into another class
233+
StreamWrapper::setDefaultContextResolver(fn (string $path, string $mode) => $this->resolveStreamContext($path, $mode));
234+
}
235+
230236
/**
231237
* Delete a file from the GridFS bucket.
232238
*
@@ -672,11 +678,6 @@ public function uploadFromStream(string $filename, $source, array $options = [])
672678
return $this->getFileIdForStream($destination);
673679
}
674680

675-
public function createPathForFilename(string $filename): string
676-
{
677-
return $this->createPathForFile((object) ['_id' => $filename]);
678-
}
679-
680681
/**
681682
* Create a stream context from
682683
*
@@ -688,7 +689,7 @@ public function createPathForFilename(string $filename): string
688689
*
689690
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}|null
690691
*/
691-
public function resolveStreamContext(string $path, string $mode): ?array
692+
private function resolveStreamContext(string $path, string $mode): ?array
692693
{
693694
// The file can be read only if it belongs to this bucket
694695
$basePath = $this->createPathForFile((object) ['_id' => '']);
@@ -817,10 +818,10 @@ private function openDownloadStreamByFile(object $file)
817818
*/
818819
private function registerStreamWrapper(): void
819820
{
820-
if (in_array(self::STREAM_WRAPPER_PROTOCOL, stream_get_wrappers())) {
821+
if (in_array($this->protocol, stream_get_wrappers())) {
821822
return;
822823
}
823824

824-
StreamWrapper::register(self::STREAM_WRAPPER_PROTOCOL);
825+
StreamWrapper::register($this->protocol);
825826
}
826827
}

src/GridFS/StreamWrapper.php

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,18 +64,12 @@ class StreamWrapper
6464
private static ?Closure $contextResolver = null;
6565

6666
/**
67-
* In order to use the stream wrapper with file names only,...
68-
*
6967
* @see Bucket::resolveStreamContext()
7068
*
71-
* @param Bucket|Closure(string, string):ContextOptions|null $resolver
69+
* @param Closure(string, string):ContextOptions|null $resolver
7270
*/
73-
public static function setDefaultContextResolver($resolver): void
71+
public static function setDefaultContextResolver(?Closure $resolver): void
7472
{
75-
if ($resolver instanceof Bucket) {
76-
$resolver = Closure::fromCallable([$resolver, 'resolveStreamContext']);
77-
}
78-
7973
self::$contextResolver = $resolver;
8074
}
8175

tests/GridFS/BucketFunctionalTest.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use MongoDB\Tests\Fixtures\Codec\TestDocumentCodec;
2121
use MongoDB\Tests\Fixtures\Codec\TestFileCodec;
2222
use MongoDB\Tests\Fixtures\Document\TestFile;
23+
use ReflectionMethod;
2324
use stdClass;
2425

2526
use function array_merge;
@@ -893,21 +894,17 @@ public function testDanglingOpenWritableStream(): void
893894
$this->assertSame(14, $fileDocument->length);
894895
}
895896

896-
public function testCreatePathForFilename(): void
897-
{
898-
$filename = 'filename';
899-
$expected = sprintf('gridfs://%s/%s.files/%s', $this->bucket->getDatabaseName(), $this->bucket->getBucketName(), $filename);
900-
901-
$this->assertSame($expected, $this->bucket->createPathForFilename($filename));
902-
}
903-
904897
public function testResolveStreamContextForRead(): void
905898
{
906899
$stream = $this->bucket->openUploadStream('filename');
907900
fwrite($stream, 'foobar');
908901
fclose($stream);
909902

910-
$context = $this->bucket->resolveStreamContext($this->bucket->createPathForFilename('filename'), 'rb');
903+
$method = new ReflectionMethod($this->bucket, 'resolveStreamContext');
904+
$method->setAccessible(true);
905+
$method->setAccessible(true);
906+
907+
$context = $method->invokeArgs($this->bucket, [$this->getFileUrl('filename'), 'rb']);
911908

912909
$this->assertIsArray($context);
913910
$this->assertArrayHasKey('collectionWrapper', $context);
@@ -920,7 +917,11 @@ public function testResolveStreamContextForRead(): void
920917

921918
public function testResolveStreamContextForWrite(): void
922919
{
923-
$context = $this->bucket->resolveStreamContext($this->bucket->createPathForFilename('filename'), 'wb');
920+
$method = new ReflectionMethod($this->bucket, 'resolveStreamContext');
921+
$method->setAccessible(true);
922+
$method->setAccessible(true);
923+
924+
$context = $method->invokeArgs($this->bucket, [$this->getFileUrl('filename'), 'wb']);
924925

925926
$this->assertIsArray($context);
926927
$this->assertArrayHasKey('collectionWrapper', $context);

tests/GridFS/FunctionalTestCase.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use function fwrite;
1212
use function get_resource_type;
1313
use function rewind;
14+
use function sprintf;
1415
use function stream_get_contents;
1516

1617
/**
@@ -69,4 +70,9 @@ protected function createStream(string $data = '')
6970

7071
return $stream;
7172
}
73+
74+
protected function getFileUrl(string $filename): string
75+
{
76+
return sprintf('gridfs://%s/%s.files/%s', $this->bucket->getDatabaseName(), $this->bucket->getBucketName(), $filename);
77+
}
7278
}

tests/GridFS/StreamWrapperFunctionalTest.php

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

55
use MongoDB\BSON\Binary;
66
use MongoDB\BSON\UTCDateTime;
7-
use MongoDB\GridFS\StreamWrapper;
87

98
use function fclose;
109
use function feof;
@@ -13,7 +12,6 @@
1312
use function fseek;
1413
use function fstat;
1514
use function fwrite;
16-
use function sprintf;
1715

1816
use const SEEK_CUR;
1917
use const SEEK_END;
@@ -210,7 +208,7 @@ public function testWritableStreamWrite(): void
210208

211209
public function testStreamWithDefaultResolver(): void
212210
{
213-
StreamWrapper::setDefaultContextResolver($this->bucket);
211+
$this->bucket->registerAsDefaultStreamWrapper();
214212

215213
$stream = fopen($this->getFileUrl('filename'), 'wb');
216214

@@ -225,7 +223,7 @@ public function testStreamWithDefaultResolver(): void
225223

226224
public function testFileNoFoundWithDefaultResolver(): void
227225
{
228-
StreamWrapper::setDefaultContextResolver($this->bucket);
226+
$this->bucket->registerAsDefaultStreamWrapper();
229227

230228
$this->expectWarning();
231229
$stream = fopen($this->getFileUrl('filename'), 'r');
@@ -236,13 +234,8 @@ public function testFileNoFoundWithDefaultResolver(): void
236234
public function testFileNoFoundWithoutDefaultResolver(): void
237235
{
238236
$this->expectWarning();
239-
$stream = fopen($this->bucket->getFileUrl('filename'), 'r');
237+
$stream = fopen($this->getFileUrl('filename'), 'r');
240238

241239
$this->assertFalse($stream);
242240
}
243-
244-
private function getFileUrl(string $filename): string
245-
{
246-
return sprintf('gridfs://%s/%s.files/%s', $this->bucket->getDatabaseName(), $this->bucket->getBucketName(), $filename);
247-
}
248241
}

0 commit comments

Comments
 (0)