Skip to content

Commit 6919fb1

Browse files
committed
GridFS adapter for Filesystem
1 parent 55ca36e commit 6919fb1

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

composer.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
},
3535
"require-dev": {
3636
"mongodb/builder": "^0.2",
37+
"league/flysystem-gridfs": "^3.28",
38+
"league/flysystem-read-only": "^3.0",
3739
"phpunit/phpunit": "^10.3",
3840
"orchestra/testbench": "^8.0|^9.0",
3941
"mockery/mockery": "^1.4.4",

src/MongoDBServiceProvider.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,27 @@
66

77
use Illuminate\Cache\CacheManager;
88
use Illuminate\Cache\Repository;
9+
use Illuminate\Filesystem\FilesystemAdapter;
10+
use Illuminate\Filesystem\FilesystemManager;
911
use Illuminate\Foundation\Application;
1012
use Illuminate\Support\ServiceProvider;
13+
use InvalidArgumentException;
14+
use League\Flysystem\Filesystem;
15+
use League\Flysystem\GridFS\GridFSAdapter;
16+
use League\Flysystem\ReadOnly\ReadOnlyFilesystemAdapter;
17+
use MongoDB\Client;
18+
use MongoDB\GridFS\Bucket;
1119
use MongoDB\Laravel\Cache\MongoStore;
1220
use MongoDB\Laravel\Eloquent\Model;
1321
use MongoDB\Laravel\Queue\MongoConnector;
22+
use RuntimeException;
1423

24+
use function array_keys;
1525
use function assert;
26+
use function class_exists;
27+
use function implode;
28+
use function is_string;
29+
use function sprintf;
1630

1731
class MongoDBServiceProvider extends ServiceProvider
1832
{
@@ -66,5 +80,74 @@ public function register()
6680
return new MongoConnector($this->app['db']);
6781
});
6882
});
83+
84+
$this->registerFlysystemAdapter();
85+
}
86+
87+
private function registerFlysystemAdapter(): void
88+
{
89+
// GridFS adapter for filesystem
90+
$this->app->resolving('filesystem', static function (FilesystemManager $filesystemManager) {
91+
$filesystemManager->extend('gridfs', static function (Application $app, array $config) {
92+
if (! class_exists(GridFSAdapter::class)) {
93+
throw new RuntimeException('GridFS adapter for Flysystem is missing. Try running "composer require league/flysystem-gridfs"');
94+
}
95+
96+
// Reuse an existing database connection
97+
if (isset($config['connection'])) {
98+
if (isset($config['mongodb_uri'])) {
99+
throw new InvalidArgumentException('In GridFS configuration, "connection" and "mongodb_uri" options cannot be set together.');
100+
}
101+
102+
$connection = $app['db']->connection($config['connection']);
103+
if (! $connection instanceof Connection) {
104+
throw new InvalidArgumentException(sprintf('The database connection "%s" does not use the "mongodb" driver.', $connection['connection']));
105+
}
106+
107+
$bucket = $connection->getMongoClient()
108+
->selectDatabase($config['database'] ?? $connection->getDatabaseName())
109+
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs']);
110+
} elseif (isset($config['mongodb_uri'])) {
111+
// Create a new MongoDB client from the config
112+
if (! isset($config['database'])) {
113+
throw new InvalidArgumentException('MongoDB "database" name is required for filesystem GridFS configuration');
114+
}
115+
116+
$bucket = (new Client($config['mongodb_uri'], $config['mongodb_uri_options'] ?? [], $config['mongodb_driver_options'] ?? []))
117+
->selectDatabase($config['database'])
118+
->selectGridFSBucket(['bucketName' => $config['bucket'] ?? 'fs']);
119+
} elseif (isset($config['bucket'])) {
120+
// Allows setting the bucket instance directly
121+
$bucket = $config['bucket'];
122+
if (is_string($bucket)) {
123+
// Resolves the "bucket" service
124+
$bucket = $app->get($bucket);
125+
} elseif ($bucket instanceof \Closure) {
126+
$bucket = $bucket($app, $config);
127+
}
128+
129+
if (! $bucket instanceof Bucket) {
130+
throw new InvalidArgumentException(sprintf('Provided GridFS bucket is not a instance of "%s"', Bucket::class));
131+
}
132+
}
133+
134+
if (! isset($bucket)) {
135+
throw new InvalidArgumentException(sprintf('The "gridfs" configuration requires the "connection", "mongodb_uri", or "bucket" option. Got "%s"', implode('", "', array_keys($config))));
136+
}
137+
138+
$adapter = new GridFSAdapter($bucket, $config['prefix'] ?? '');
139+
140+
/** @see FilesystemManager::createFlysystem() */
141+
if ($config['read-only'] ?? false) {
142+
if (! class_exists(ReadOnlyFilesystemAdapter::class)) {
143+
throw new RuntimeException('Read-only Adapter for Flysystem is missing. Try running "composer require league/flysystem-read-only"');
144+
}
145+
146+
$adapter = new ReadOnlyFilesystemAdapter($adapter);
147+
}
148+
149+
return new FilesystemAdapter(new Filesystem($adapter, $config), $adapter, $config);
150+
});
151+
});
69152
}
70153
}

0 commit comments

Comments
 (0)