Skip to content

Commit

Permalink
Support GridFS adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
GromNaN committed May 24, 2024
1 parent dd057ba commit e3444b8
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"league/flysystem-azure-blob-storage": "^3.1",
"league/flysystem-ftp": "^3.1",
"league/flysystem-google-cloud-storage": "^3.1",
"league/flysystem-gridfs": "^3.28",
"league/flysystem-memory": "^3.1",
"league/flysystem-read-only": "^3.15",
"league/flysystem-sftp-v3": "^3.1",
Expand Down
44 changes: 44 additions & 0 deletions docs/2-cloud-storage-providers.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,50 @@ flysystem:
bucket: '%env(SCALEWAY_SPACES_BUCKET)%'
```
## MongoDB GridFS
```
composer require league/flysystem-google-cloud-storage
```

The MongoDB GridFS storage requires a `MongoDB\GridFS\Bucket` instance.

```yaml
# config/packages/flysystem.yaml

services:
mongodb_client:
class: 'MongoDB\Client'
arguments:
- '%env(MONGODB_URI)%'

mongodb_database:
class: 'MongoDB\Database'
factory: ['mongodb_client', 'selectDatabase']
arguments: ['%env(MONGODB_DATABASE)%']

mongodb_gridfs_bucket:
class: 'MongoDB\GridFS\Bucket'
factory: ['@mongodb_database', 'selectGridFSBucket']

flysystem:
storages:
users.storage:
adapter: 'gridfs'
options:
bucket: 'mongodb_gridfs_bucket'
```
```dotenv
# .env

MONGODB_URI=mongodb://127.0.0.1:27017/
MONGODB_DATABASE=flysystem
```

Note: if the DoctrineMongoDBODMBundle is enabled on the projection,
`doctrine_mongodb.odm.default_connection` provides the `MongoDB\Client` service.

## Next

[Interacting with FTP and SFTP servers](https://github.com/thephpleague/flysystem-bundle/blob/master/docs/3-interacting-with-ftp-and-sftp-servers.md)
1 change: 1 addition & 0 deletions src/Adapter/AdapterDefinitionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ public function __construct()
new Builder\AzureAdapterDefinitionBuilder(),
new Builder\FtpAdapterDefinitionBuilder(),
new Builder\GcloudAdapterDefinitionBuilder(),
new Builder\GridFSAdapterDefinitionBuilder(),
new Builder\LocalAdapterDefinitionBuilder(),
new Builder\MemoryAdapterDefinitionBuilder(),
new Builder\SftpAdapterDefinitionBuilder(),
Expand Down
54 changes: 54 additions & 0 deletions src/Adapter/Builder/GridFSAdapterDefinitionBuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

/*
* This file is part of the flysystem-bundle project.
*
* (c) Titouan Galopin <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace League\FlysystemBundle\Adapter\Builder;

use League\Flysystem\GridFS\GridFSAdapter;
use MongoDB\GridFS\Bucket;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Jérôme Tamarelle <[email protected]>
*
* @internal
*/
class GridFSAdapterDefinitionBuilder extends AbstractAdapterDefinitionBuilder
{
public function getName(): string
{
return 'gridfs';
}

protected function getRequiredPackages(): array
{
return [
GridFSAdapter::class => 'league/flysystem-gridfs',
];
}

protected function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefault('bucket', Bucket::class);
$resolver->setAllowedTypes('bucket', 'string');

$resolver->setDefault('prefix', '');
$resolver->setAllowedTypes('prefix', 'string');
}

protected function configureDefinition(Definition $definition, array $options, ?string $defaultVisibilityForDirectories): void
{
$definition->setClass(GridFSAdapter::class);
$definition->setArgument(0, new Reference($options['bucket']));
$definition->setArgument(1, $options['prefix']);
}
}

0 comments on commit e3444b8

Please sign in to comment.