|
6 | 6 |
|
7 | 7 | use Illuminate\Cache\CacheManager;
|
8 | 8 | use Illuminate\Cache\Repository;
|
| 9 | +use Illuminate\Filesystem\FilesystemAdapter; |
| 10 | +use Illuminate\Filesystem\FilesystemManager; |
9 | 11 | use Illuminate\Foundation\Application;
|
10 | 12 | 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; |
11 | 19 | use MongoDB\Laravel\Cache\MongoStore;
|
12 | 20 | use MongoDB\Laravel\Eloquent\Model;
|
13 | 21 | use MongoDB\Laravel\Queue\MongoConnector;
|
| 22 | +use RuntimeException; |
14 | 23 |
|
| 24 | +use function array_keys; |
15 | 25 | use function assert;
|
| 26 | +use function class_exists; |
| 27 | +use function implode; |
| 28 | +use function is_string; |
| 29 | +use function sprintf; |
16 | 30 |
|
17 | 31 | class MongoDBServiceProvider extends ServiceProvider
|
18 | 32 | {
|
@@ -66,5 +80,74 @@ public function register()
|
66 | 80 | return new MongoConnector($this->app['db']);
|
67 | 81 | });
|
68 | 82 | });
|
| 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 | + }); |
69 | 152 | }
|
70 | 153 | }
|
0 commit comments