Skip to content

Commit 082080f

Browse files
committed
Fix CS
1 parent 8fe693c commit 082080f

File tree

2 files changed

+74
-69
lines changed

2 files changed

+74
-69
lines changed

src/GridFS/Bucket.php

Lines changed: 61 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,6 @@ public function __debugInfo()
219219
];
220220
}
221221

222-
public function registerAsDefaultStreamWrapper(): void
223-
{
224-
// Use a closure to expose the private method into another class
225-
StreamWrapper::setDefaultContextResolver(fn (string $path, string $mode) => $this->resolveStreamContext($path, $mode));
226-
}
227-
228222
/**
229223
* Delete a file from the GridFS bucket.
230224
*
@@ -598,6 +592,18 @@ public function openUploadStream(string $filename, array $options = [])
598592
return fopen($path, 'w', false, $context);
599593
}
600594

595+
/**
596+
* For legacy applications that needs to open stream from a string, the bucket can be registered in the stream
597+
* wrapper to be used by default when no GridFS context is provided.
598+
* Supported file name must follow this pattern:
599+
* gridfs://<database_name>/<bucket_name>.fs/<file_name>
600+
*/
601+
public function registerAsDefaultStreamWrapper(): void
602+
{
603+
// Use a closure to expose the private method into another class
604+
StreamWrapper::setDefaultContextResolver(fn (string $path, string $mode) => $this->resolveStreamContext($path, $mode));
605+
}
606+
601607
/**
602608
* Renames the GridFS file with the specified ID.
603609
*
@@ -670,55 +676,6 @@ public function uploadFromStream(string $filename, $source, array $options = [])
670676
return $this->getFileIdForStream($destination);
671677
}
672678

673-
/**
674-
* Create a stream context from
675-
*
676-
* @see StreamWrapper::setDefaultContextResolver()
677-
* @see stream_context_create()
678-
*
679-
* @param string $path The full url provided to fopen(). It contains the filename.
680-
* gridfs://database_name/collection_name.files/file_name
681-
*
682-
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}|null
683-
*/
684-
private function resolveStreamContext(string $path, string $mode): ?array
685-
{
686-
// The file can be read only if it belongs to this bucket
687-
$basePath = $this->createPathForFile((object) ['_id' => '']);
688-
if (! str_starts_with($path, $basePath)) {
689-
return null;
690-
}
691-
692-
$filename = urldecode(substr($path, strlen($basePath)));
693-
694-
if (str_contains($mode, 'r')) {
695-
$file = $this->collectionWrapper->findFileByFilenameAndRevision($filename, -1);
696-
697-
// File not found
698-
if ($file === null) {
699-
return null;
700-
}
701-
702-
return [
703-
'collectionWrapper' => $this->collectionWrapper,
704-
'file' => $file,
705-
];
706-
}
707-
708-
if (str_contains($mode, 'w')) {
709-
return [
710-
'collectionWrapper' => $this->collectionWrapper,
711-
'filename' => $filename,
712-
'options' => [
713-
'chunkSizeBytes' => $this->chunkSizeBytes,
714-
'disableMD5' => $this->disableMD5,
715-
],
716-
];
717-
}
718-
719-
return null;
720-
}
721-
722679
/**
723680
* Creates a path for an existing GridFS file.
724681
*
@@ -816,4 +773,53 @@ private function registerStreamWrapper(): void
816773

817774
StreamWrapper::register(self::STREAM_WRAPPER_PROTOCOL);
818775
}
776+
777+
/**
778+
* Create a stream context from
779+
*
780+
* @see StreamWrapper::setDefaultContextResolver()
781+
* @see stream_context_create()
782+
*
783+
* @param string $path The full url provided to fopen(). It contains the filename.
784+
* gridfs://database_name/collection_name.files/file_name
785+
*
786+
* @return array{collectionWrapper: CollectionWrapper, file: object}|array{collectionWrapper: CollectionWrapper, filename: string, options: array}|null
787+
*/
788+
private function resolveStreamContext(string $path, string $mode): ?array
789+
{
790+
// The file can be read only if it belongs to this bucket
791+
$basePath = $this->createPathForFile((object) ['_id' => '']);
792+
if (! str_starts_with($path, $basePath)) {
793+
return null;
794+
}
795+
796+
$filename = urldecode(substr($path, strlen($basePath)));
797+
798+
if (str_contains($mode, 'r')) {
799+
$file = $this->collectionWrapper->findFileByFilenameAndRevision($filename, -1);
800+
801+
// File not found
802+
if ($file === null) {
803+
return null;
804+
}
805+
806+
return [
807+
'collectionWrapper' => $this->collectionWrapper,
808+
'file' => $file,
809+
];
810+
}
811+
812+
if (str_contains($mode, 'w')) {
813+
return [
814+
'collectionWrapper' => $this->collectionWrapper,
815+
'filename' => $filename,
816+
'options' => [
817+
'chunkSizeBytes' => $this->chunkSizeBytes,
818+
'disableMD5' => $this->disableMD5,
819+
],
820+
];
821+
}
822+
823+
return null;
824+
}
819825
}

src/GridFS/StreamWrapper.php

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
use MongoDB\BSON\UTCDateTime;
2222

2323
use function assert;
24-
use function call_user_func;
2524
use function explode;
2625
use function in_array;
2726
use function is_array;
@@ -63,16 +62,6 @@ class StreamWrapper
6362
/** @var Closure(string, string): ContextOptions|null */
6463
private static ?Closure $contextResolver = null;
6564

66-
/**
67-
* @see Bucket::resolveStreamContext()
68-
*
69-
* @param Closure(string, string):ContextOptions|null $resolver
70-
*/
71-
public static function setDefaultContextResolver(?Closure $resolver): void
72-
{
73-
self::$contextResolver = $resolver;
74-
}
75-
7665
public function __destruct()
7766
{
7867
/* Ensure the stream is closed so the last chunk is written. This is
@@ -106,6 +95,16 @@ public static function register(string $protocol = 'gridfs'): void
10695
stream_wrapper_register($protocol, static::class, STREAM_IS_URL);
10796
}
10897

98+
/**
99+
* @see Bucket::resolveStreamContext()
100+
*
101+
* @param Closure(string, string):ContextOptions|null $resolver
102+
*/
103+
public static function setDefaultContextResolver(?Closure $resolver): void
104+
{
105+
self::$contextResolver = $resolver;
106+
}
107+
109108
/**
110109
* Closes the stream.
111110
*
@@ -145,7 +144,7 @@ public function stream_eof(): bool
145144
*/
146145
public function stream_open(string $path, string $mode, int $options, ?string &$openedPath): bool
147146
{
148-
$protocol = $this->parseProtocol($path);
147+
$protocol = $this->initProtocol($path);
149148

150149
assert(is_resource($this->context));
151150
$contextOptions = stream_context_get_options($this->context)[$protocol] ?? null;
@@ -159,7 +158,7 @@ public function stream_open(string $path, string $mode, int $options, ?string &$
159158
return false;
160159
}
161160

162-
$contextOptions = call_user_func(self::$contextResolver, $path, $mode);
161+
$contextOptions = (self::$contextResolver)($path, $mode);
163162
if ($contextOptions === null) {
164163
if ($options & STREAM_REPORT_ERRORS) {
165164
trigger_error(sprintf('File not found "%s" with the default GridFS resolver.', $path), E_USER_WARNING);
@@ -331,7 +330,7 @@ private function getStatTemplate(): array
331330
*
332331
* @see StreamWrapper::stream_open()
333332
*/
334-
private function parseProtocol(string $path): string
333+
private function initProtocol(string $path): string
335334
{
336335
$parts = explode('://', $path, 2);
337336

0 commit comments

Comments
 (0)