From 8cf9113478258f2d71cb5ef92a46b7a05afe4c9c Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 16 Sep 2021 12:15:51 +0200 Subject: [PATCH] Add google cloud storage adapter (#238) --- composer.json | 2 + doc/adapter_google_cloud_storage.md | 20 ++++++++ .../Adapter/GoogleCloudStorageFactory.php | 49 +++++++++++++++++++ src/Resources/config/adapters.xml | 7 +++ src/Resources/config/factories.xml | 4 ++ tests/App/config/config.yml | 27 +++++++--- .../OneupFlysystemExtensionTest.php | 9 ++++ 7 files changed, 111 insertions(+), 7 deletions(-) create mode 100644 doc/adapter_google_cloud_storage.md create mode 100644 src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php diff --git a/composer.json b/composer.json index ae089f3..02eaf02 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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" }, diff --git a/doc/adapter_google_cloud_storage.md b/doc/adapter_google_cloud_storage.md new file mode 100644 index 0000000..8c4c813 --- /dev/null +++ b/doc/adapter_google_cloud_storage.md @@ -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) diff --git a/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php new file mode 100644 index 0000000..73db0c1 --- /dev/null +++ b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php @@ -0,0 +1,49 @@ +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() + ; + } +} diff --git a/src/Resources/config/adapters.xml b/src/Resources/config/adapters.xml index 7e9cd32..9c57ad7 100644 --- a/src/Resources/config/adapters.xml +++ b/src/Resources/config/adapters.xml @@ -41,6 +41,13 @@ + + + + + + + diff --git a/src/Resources/config/factories.xml b/src/Resources/config/factories.xml index d9c38e0..01923a4 100644 --- a/src/Resources/config/factories.xml +++ b/src/Resources/config/factories.xml @@ -32,6 +32,10 @@ class="Oneup\FlysystemBundle\DependencyInjection\Factory\Adapter\AsyncAwsS3Factory"> + + + diff --git a/tests/App/config/config.yml b/tests/App/config/config.yml index 0cde904..217706e 100644 --- a/tests/App/config/config.yml +++ b/tests/App/config/config.yml @@ -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 @@ -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 @@ -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 diff --git a/tests/DependencyInjection/OneupFlysystemExtensionTest.php b/tests/DependencyInjection/OneupFlysystemExtensionTest.php index c296240..65a4568 100644 --- a/tests/DependencyInjection/OneupFlysystemExtensionTest.php +++ b/tests/DependencyInjection/OneupFlysystemExtensionTest.php @@ -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); @@ -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();