From e3191480d05df2d7465ad3de508edb18f90a33ca Mon Sep 17 00:00:00 2001 From: David Greminger Date: Tue, 23 Mar 2021 21:05:02 +0100 Subject: [PATCH 1/7] Add google cloud storage adapter --- composer.json | 2 + doc/adapter_google_cloud_storage.md | 19 ++++++++ .../Adapter/GoogleCloudStorageFactory.php | 43 +++++++++++++++++++ src/Resources/config/adapters.xml | 6 +++ src/Resources/config/factories.xml | 4 ++ tests/App/config/config.yml | 4 ++ 6 files changed, 78 insertions(+) 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..8f64a16 --- /dev/null +++ b/doc/adapter_google_cloud_storage.md @@ -0,0 +1,19 @@ +# 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: + 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..6872fe6 --- /dev/null +++ b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php @@ -0,0 +1,43 @@ +setDefinition($id, new ChildDefinition('oneup_flysystem.adapter.googlecloudstorage')) + ->replaceArgument(0, $config['bucket']) + ->replaceArgument(1, $config['prefix']) + ->replaceArgument(2, $config['visibilityHandler']) + ->replaceArgument(3, $config['defaultVisiblity']) + ; + } + + public function addConfiguration(NodeDefinition $node): void + { + $node + ->children() + ->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 4cbbb0d..f18ee13 100644 --- a/src/Resources/config/adapters.xml +++ b/src/Resources/config/adapters.xml @@ -41,6 +41,12 @@ + + + + + + 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 b167863..9bbc1fb 100644 --- a/tests/App/config/config.yml +++ b/tests/App/config/config.yml @@ -39,6 +39,10 @@ oneup_flysystem: connectionProvider: 'test' root: 'test' + googlecloudstorage: + googlecloudstorage: + bucket: 'test' + filesystems: myfilesystem: adapter: local From bcb4d0fd5301508b6216bf2a29a7b0d1140089d4 Mon Sep 17 00:00:00 2001 From: David Greminger Date: Tue, 23 Mar 2021 21:08:00 +0100 Subject: [PATCH 2/7] Fix CS --- .../Factory/Adapter/GoogleCloudStorageFactory.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php index 6872fe6..8c86d36 100644 --- a/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php +++ b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php @@ -9,7 +9,6 @@ use Symfony\Component\Config\Definition\Builder\NodeDefinition; use Symfony\Component\DependencyInjection\ChildDefinition; use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Reference; class GoogleCloudStorageFactory implements AdapterFactoryInterface { From ef94d9b994a82514b4ff1d2ed44d3e8bb4c96fbc Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 16 Sep 2021 12:06:03 +0200 Subject: [PATCH 3/7] Fix GoogleCloudStorage factory and add some test --- .../Adapter/GoogleCloudStorageFactory.php | 9 ++++++- src/Resources/config/adapters.xml | 1 + tests/App/config/config.yml | 24 +++++++++++++------ .../OneupFlysystemExtensionTest.php | 5 ++++ 4 files changed, 31 insertions(+), 8 deletions(-) diff --git a/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php index 8c86d36..73db0c1 100644 --- a/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php +++ b/src/DependencyInjection/Factory/Adapter/GoogleCloudStorageFactory.php @@ -9,6 +9,8 @@ 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 { @@ -19,9 +21,13 @@ public function getKey(): string 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, $config['bucket']) + ->replaceArgument(0, $bucket) ->replaceArgument(1, $config['prefix']) ->replaceArgument(2, $config['visibilityHandler']) ->replaceArgument(3, $config['defaultVisiblity']) @@ -32,6 +38,7 @@ 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() diff --git a/src/Resources/config/adapters.xml b/src/Resources/config/adapters.xml index f18ee13..a38e828 100644 --- a/src/Resources/config/adapters.xml +++ b/src/Resources/config/adapters.xml @@ -42,6 +42,7 @@ + diff --git a/tests/App/config/config.yml b/tests/App/config/config.yml index 9bbc1fb..ebf6c4c 100644 --- a/tests/App/config/config.yml +++ b/tests/App/config/config.yml @@ -1,3 +1,16 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + Oneup\FlysystemBundle\Tests\DependencyInjection\TestService: + public: true + + google_cloud_storage_client: + class: Google\Cloud\Storage\StorageClient + public: true + + framework: translator: { fallback: en } secret: secret @@ -41,7 +54,9 @@ oneup_flysystem: googlecloudstorage: googlecloudstorage: + client: 'google_cloud_storage_client' bucket: 'test' + prefix: 'prefix' filesystems: myfilesystem: @@ -55,10 +70,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..d31c2e6 100644 --- a/tests/DependencyInjection/OneupFlysystemExtensionTest.php +++ b/tests/DependencyInjection/OneupFlysystemExtensionTest.php @@ -143,6 +143,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(); From 817d9fd4f2e0e89ddd5c5d029b0243e5da53abec Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 16 Sep 2021 12:08:00 +0200 Subject: [PATCH 4/7] Update doc --- doc/adapter_google_cloud_storage.md | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/adapter_google_cloud_storage.md b/doc/adapter_google_cloud_storage.md index 8f64a16..8c4c813 100644 --- a/doc/adapter_google_cloud_storage.md +++ b/doc/adapter_google_cloud_storage.md @@ -8,6 +8,7 @@ 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: '' ``` From 875a6c4cde2c0226b960f20680b78ea470067909 Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 16 Sep 2021 12:09:56 +0200 Subject: [PATCH 5/7] Fix PHPStan --- tests/DependencyInjection/OneupFlysystemExtensionTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/DependencyInjection/OneupFlysystemExtensionTest.php b/tests/DependencyInjection/OneupFlysystemExtensionTest.php index d31c2e6..1ff0b7a 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) { + continue; + } + foreach ($service->attributes() as $key => $attribute) { if ('class' === (string) $key) { self::assertTrue(class_exists((string) $attribute), 'Could not load class: ' . $attribute); From ee89fd8488f2d8467ef6024494a160885ca74f84 Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 16 Sep 2021 12:11:55 +0200 Subject: [PATCH 6/7] Fix --- tests/DependencyInjection/OneupFlysystemExtensionTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/DependencyInjection/OneupFlysystemExtensionTest.php b/tests/DependencyInjection/OneupFlysystemExtensionTest.php index 1ff0b7a..65a4568 100644 --- a/tests/DependencyInjection/OneupFlysystemExtensionTest.php +++ b/tests/DependencyInjection/OneupFlysystemExtensionTest.php @@ -60,7 +60,7 @@ 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) { + if (null === $service->attributes()) { continue; } From e8a47b4f16cd3873671274d5c0d1dc84753552dc Mon Sep 17 00:00:00 2001 From: David Greminger Date: Thu, 16 Sep 2021 12:14:22 +0200 Subject: [PATCH 7/7] Make test service private --- tests/App/config/config.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/App/config/config.yml b/tests/App/config/config.yml index ebf6c4c..2045807 100644 --- a/tests/App/config/config.yml +++ b/tests/App/config/config.yml @@ -8,7 +8,6 @@ services: google_cloud_storage_client: class: Google\Cloud\Storage\StorageClient - public: true framework: