Skip to content

Commit

Permalink
Add google cloud storage adapter (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
bytehead authored Sep 16, 2021
1 parent f0c7531 commit 8cf9113
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 7 deletions.
2 changes: 2 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
"league/flysystem-async-aws-s3": "^2.0",
"league/flysystem-aws-s3-v3": "^2.0",
"league/flysystem-ftp": "^2.0",
"league/flysystem-google-cloud-storage": "^2.0",
"league/flysystem-memory": "^2.0",
"league/flysystem-sftp": "^2.0",
"phpstan/phpstan": "^0.12.10",
Expand All @@ -56,6 +57,7 @@
"ext-ftp": "Required for FTP and SFTP",
"league/flysystem-async-aws-s3": "Use flysystem S3 adapter from AsyncAws",
"league/flysystem-aws-s3-v3": "Use S3 storage with AWS SDK v3",
"league/flysystem-google-cloud-storage": "Use Google Cloud Storage Adapter for Flysystem",
"league/flysystem-sftp": "Allows SFTP server storage via phpseclib",
"royvoetman/flysystem-gitlab-storage": "Use Gitlab Storage filesystem for Flysystem"
},
Expand Down
20 changes: 20 additions & 0 deletions doc/adapter_google_cloud_storage.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use the Google Cloud Storage adapter

This adapter connects to the filesystem in the Google Cloud Storage.


```yml
oneup_flysystem:
adapters:
acme.flysystem_adapter:
googlecloudstorage:
client: 'google_cloud_storage_client' # Service ID of the Google\Cloud\Storage\StorageClient
bucket: 'my_gcs_bucket'
prefix: ''
```
For more details on the other parameters, take a look at the [Flysystem documentation](https://flysystem.thephpleague.com/v2/docs/adapter/gcs/).
## More to know
* [Create and use your filesystem](filesystem_create.md)
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter;

use League\Flysystem\Visibility;
use Oneup\FlysystemBundle\DependencyInjection\Factory\AdapterFactoryInterface;
use Symfony\Component\Config\Definition\Builder\NodeDefinition;
use Symfony\Component\DependencyInjection\ChildDefinition;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class GoogleCloudStorageFactory implements AdapterFactoryInterface
{
public function getKey(): string
{
return 'googlecloudstorage';
}

public function create(ContainerBuilder $container, string $id, array $config): void
{
$bucket = new Definition();
$bucket->setFactory([new Reference($config['client']), 'bucket']);
$bucket->setArgument(0, $config['bucket']);

$container
->setDefinition($id, new ChildDefinition('oneup_flysystem.adapter.googlecloudstorage'))
->replaceArgument(0, $bucket)
->replaceArgument(1, $config['prefix'])
->replaceArgument(2, $config['visibilityHandler'])
->replaceArgument(3, $config['defaultVisiblity'])
;
}

public function addConfiguration(NodeDefinition $node): void
{
$node
->children()
->scalarNode('client')->isRequired()->end()
->scalarNode('bucket')->isRequired()->end()
->scalarNode('prefix')->treatNullLike('')->defaultValue('')->end()
->scalarNode('visibilityHandler')->defaultNull()->end()
->scalarNode('defaultVisiblity')->defaultValue(Visibility::PRIVATE)->end()
->end()
;
}
}
7 changes: 7 additions & 0 deletions src/Resources/config/adapters.xml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@
<argument/><!-- Prefix -->
<argument/><!-- VisibilityConverter -->
</service>
<service id="oneup_flysystem.adapter.googlecloudstorage" class="League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter" abstract="true" public="false">
<argument/><!-- Client -->
<argument/><!-- Bucket -->
<argument/><!-- Prefix -->
<argument/><!-- VisibilityHandler -->
<argument/><!-- defaultVisibility -->
</service>
<service id="oneup_flysystem.adapter.gitlab" class="RoyVoetman\FlysystemGitlab\GitlabAdapter" abstract="true" public="false">
<argument/><!-- Client -->
<argument/><!-- Prefix -->
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/factories.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
class="Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter\AsyncAwsS3Factory">
<tag name="oneup_flysystem.adapter_factory"/>
</service>
<service id="oneup_flysystem.adapter_factory.google_cloud_storage"
class="Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter\GoogleCloudStorageFactory">
<tag name="oneup_flysystem.adapter_factory"/>
</service>
<service id="oneup_flysystem.adapter_factory.gitlab"
class="Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter\GitlabFactory">
<tag name="oneup_flysystem.adapter_factory"/>
Expand Down
27 changes: 20 additions & 7 deletions tests/App/config/config.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
services:
_defaults:
autowire: true
autoconfigure: true

Oneup\FlysystemBundle\Tests\DependencyInjection\TestService:
public: true

google_cloud_storage_client:
class: Google\Cloud\Storage\StorageClient


framework:
translator: { fallback: en }
secret: secret
Expand Down Expand Up @@ -45,6 +57,12 @@ oneup_flysystem:
username: foo
root: '/upload'

googlecloudstorage:
googlecloudstorage:
client: 'google_cloud_storage_client'
bucket: 'test'
prefix: 'prefix'

filesystems:
myfilesystem:
adapter: local
Expand All @@ -57,10 +75,5 @@ oneup_flysystem:
adapter: local
visibility: private

services:
_defaults:
autowire: true
autoconfigure: true

Oneup\FlysystemBundle\Tests\DependencyInjection\TestService:
public: true
myfilesystem4:
adapter: googlecloudstorage
9 changes: 9 additions & 0 deletions tests/DependencyInjection/OneupFlysystemExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function testAdapterAvailability(): void
$adapters = simplexml_load_string((string) file_get_contents(__DIR__ . '/../../src/Resources/config/adapters.xml'));

foreach ($adapters->children()->children() as $service) {
if (null === $service->attributes()) {
continue;
}

foreach ($service->attributes() as $key => $attribute) {
if ('class' === (string) $key) {
self::assertTrue(class_exists((string) $attribute), 'Could not load class: ' . $attribute);
Expand Down Expand Up @@ -143,6 +147,11 @@ public function testServiceAliasInjection(): void
self::assertInstanceOf(Filesystem::class, $testService->filesystem);
}

public function testGoogleCloudAdapter(): void
{
$this->assertInstanceOf(Filesystem::class, self::$container->get('oneup_flysystem.myfilesystem4_filesystem'));
}

private function loadExtension(array $config): ContainerBuilder
{
$extension = new OneupFlysystemExtension();
Expand Down

0 comments on commit 8cf9113

Please sign in to comment.