Skip to content

Commit 524ce4b

Browse files
committed
Add Google Cloud adapter back
1 parent 0a92470 commit 524ce4b

8 files changed

Lines changed: 150 additions & 19 deletions

File tree

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
"async-aws/flysystem-s3": "^0.3",
3232
"league/flysystem-aws-s3-v3": "^2.0",
3333
"league/flysystem-ftp": "^2.0",
34+
"league/flysystem-google-cloud-storage": "^2.0",
3435
"league/flysystem-memory": "^2.0",
3536
"league/flysystem-sftp": "^2.0",
3637
"lustmored/flysystem-v2-simple-cache-adapter": "^0.1.0",

src/Adapter/AdapterDefinitionFactory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public function __construct()
3333
new Builder\AwsAdapterDefinitionBuilder(),
3434
new Builder\CacheAdapterDefinitionBuilder(),
3535
new Builder\FtpAdapterDefinitionBuilder(),
36+
new Builder\GcloudAdapterDefinitionBuilder(),
3637
new Builder\LocalAdapterDefinitionBuilder(),
3738
new Builder\MemoryAdapterDefinitionBuilder(),
3839
new Builder\SftpAdapterDefinitionBuilder(),
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <galopintitouan@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace League\FlysystemBundle\Adapter\Builder;
13+
14+
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter;
15+
use Symfony\Component\DependencyInjection\Definition;
16+
use Symfony\Component\DependencyInjection\Reference;
17+
use Symfony\Component\OptionsResolver\OptionsResolver;
18+
19+
/**
20+
* @author Titouan Galopin <galopintitouan@gmail.com>
21+
*
22+
* @internal
23+
*/
24+
class GcloudAdapterDefinitionBuilder extends AbstractAdapterDefinitionBuilder
25+
{
26+
public function getName(): string
27+
{
28+
return 'gcloud';
29+
}
30+
31+
protected function getRequiredPackages(): array
32+
{
33+
return [
34+
GoogleCloudStorageAdapter::class => 'league/flysystem-google-cloud-storage',
35+
];
36+
}
37+
38+
protected function configureOptions(OptionsResolver $resolver)
39+
{
40+
$resolver->setRequired('client');
41+
$resolver->setAllowedTypes('client', 'string');
42+
43+
$resolver->setRequired('bucket');
44+
$resolver->setAllowedTypes('bucket', 'string');
45+
46+
$resolver->setDefault('prefix', '');
47+
$resolver->setAllowedTypes('prefix', 'string');
48+
}
49+
50+
protected function configureDefinition(Definition $definition, array $options)
51+
{
52+
$bucketDefinition = new Definition();
53+
$bucketDefinition->setFactory([new Reference($options['client']), 'bucket']);
54+
$bucketDefinition->setArgument(0, $options['bucket']);
55+
56+
$definition->setClass(GoogleCloudStorageAdapter::class);
57+
$definition->setArgument(0, $bucketDefinition);
58+
$definition->setArgument(1, $options['prefix']);
59+
}
60+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the flysystem-bundle project.
5+
*
6+
* (c) Titouan Galopin <galopintitouan@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Tests\League\FlysystemBundle\Adapter\Builder;
13+
14+
use League\Flysystem\GoogleCloudStorage\GoogleCloudStorageAdapter;
15+
use League\FlysystemBundle\Adapter\Builder\GcloudAdapterDefinitionBuilder;
16+
use PHPUnit\Framework\TestCase;
17+
use Symfony\Component\DependencyInjection\Definition;
18+
use Symfony\Component\DependencyInjection\Reference;
19+
20+
class GcloudAdapterDefinitionBuilderTest extends TestCase
21+
{
22+
public function createBuilder()
23+
{
24+
return new GcloudAdapterDefinitionBuilder();
25+
}
26+
27+
public function provideValidOptions()
28+
{
29+
yield 'minimal' => [[
30+
'client' => 'my_client',
31+
'bucket' => 'bucket',
32+
]];
33+
34+
yield 'full' => [[
35+
'client' => 'my_client',
36+
'bucket' => 'bucket',
37+
'prefix' => 'prefix/path',
38+
]];
39+
}
40+
41+
/**
42+
* @dataProvider provideValidOptions
43+
*/
44+
public function testCreateDefinition($options)
45+
{
46+
$this->assertSame(GoogleCloudStorageAdapter::class, $this->createBuilder()->createDefinition($options)->getClass());
47+
}
48+
49+
public function testOptionsBehavior()
50+
{
51+
$definition = $this->createBuilder()->createDefinition([
52+
'client' => 'my_client',
53+
'bucket' => 'bucket_name',
54+
'prefix' => 'prefix/path',
55+
]);
56+
57+
$this->assertSame(GoogleCloudStorageAdapter::class, $definition->getClass());
58+
59+
/** @var Definition $bucketDefinition */
60+
$bucketDefinition = $definition->getArgument(0);
61+
$this->assertInstanceOf(Definition::class, $bucketDefinition);
62+
$this->assertSame('bucket_name', $bucketDefinition->getArgument(0));
63+
$this->assertInstanceOf(Reference::class, $bucketDefinition->getFactory()[0]);
64+
$this->assertSame('my_client', (string) $bucketDefinition->getFactory()[0]);
65+
$this->assertSame('bucket', $bucketDefinition->getFactory()[1]);
66+
67+
$this->assertSame('prefix/path', $definition->getArgument(1));
68+
}
69+
}

tests/Adapter/options.yaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,12 @@ fs_ftp:
4444
timeout: 30
4545
ignore_passive_address: true
4646

47-
#fs_gcloud:
48-
# adapter: 'gcloud'
49-
# options:
50-
# client: 'gcloud_client_service'
51-
# bucket: 'bucket_name'
52-
# prefix: 'optional/path/prefix'
53-
# api_url: 'https://storage.googleapis.com'
47+
fs_gcloud:
48+
adapter: 'gcloud'
49+
options:
50+
client: 'gcloud_client_service'
51+
bucket: 'bucket_name'
52+
prefix: 'optional/path/prefix'
5453

5554
fs_local:
5655
adapter: 'local'

tests/DependencyInjection/FlysystemExtensionTest.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313

1414
use AsyncAws\S3\S3Client as AsyncS3Client;
1515
use Aws\S3\S3Client;
16+
use Google\Cloud\Storage\Bucket;
17+
use Google\Cloud\Storage\StorageClient;
1618
use League\Flysystem\FilesystemOperator;
1719
use PHPUnit\Framework\TestCase;
1820
use Symfony\Component\Dotenv\Dotenv;
@@ -30,7 +32,7 @@ public function provideFilesystems()
3032
// 'fs_custom',
3133
// 'fs_dropbox',
3234
'fs_ftp',
33-
// 'fs_gcloud',
35+
'fs_gcloud',
3436
// 'fs_lazy',
3537
'fs_local',
3638
// 'fs_rackspace',
@@ -99,8 +101,8 @@ private function createFysystemKernel()
99101

100102
private function getClientMocks()
101103
{
102-
// $gcloud = $this->createMock(StorageClient::class);
103-
// $gcloud->method('bucket')->willReturn($this->createMock(Bucket::class));
104+
$gcloud = $this->createMock(StorageClient::class);
105+
$gcloud->method('bucket')->willReturn($this->createMock(Bucket::class));
104106

105107
$asyncAws = null;
106108
if (Kernel::VERSION_ID > 50200 && class_exists(AsyncS3Client::class)) {
@@ -112,7 +114,7 @@ private function getClientMocks()
112114
'asyncaws_client_service' => $asyncAws,
113115
// 'azure_client_service' => $this->createMock(BlobRestProxy::class),
114116
// 'dropbox_client_service' => $this->createMock(DropboxClient::class),
115-
// 'gcloud_client_service' => $gcloud,
117+
'gcloud_client_service' => $gcloud,
116118
// 'rackspace_container_service' => $this->createMock(Container::class),
117119
// 'webdav_client_service' => $this->createMock(WebDAVClient::class),
118120
];

tests/Kernel/config/flysystem.yaml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,12 @@ flysystem:
4242
timeout: 30
4343
utf8: false
4444

45-
# fs_gcloud:
46-
# adapter: 'gcloud'
47-
# options:
48-
# client: 'gcloud_client_service'
49-
# bucket: 'bucket_name'
50-
# prefix: 'optional/path/prefix'
51-
# api_url: 'https://storage.googleapis.com'
45+
fs_gcloud:
46+
adapter: 'gcloud'
47+
options:
48+
client: 'gcloud_client_service'
49+
bucket: 'bucket_name'
50+
prefix: 'optional/path/prefix'
5251

5352
fs_lazy:
5453
adapter: 'lazy'

tests/Kernel/config/services.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ services:
1212
# flysystem.test.fs_custom: { alias: 'fs_custom' }
1313
# flysystem.test.fs_dropbox: { alias: 'fs_dropbox' }
1414
flysystem.test.fs_ftp: { alias: 'fs_ftp' }
15-
# flysystem.test.fs_gcloud: { alias: 'fs_gcloud' }
15+
flysystem.test.fs_gcloud: { alias: 'fs_gcloud' }
1616
# flysystem.test.fs_lazy: { alias: 'fs_lazy' }
1717
flysystem.test.fs_local: { alias: 'fs_local' }
1818
flysystem.test.fs_memory: { alias: 'fs_memory' }

0 commit comments

Comments
 (0)