Skip to content

Commit 4245d6c

Browse files
committed
feature: add ability to inject a custom S3 client on aws s3 resolver
1 parent c00acc3 commit 4245d6c

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

DependencyInjection/Factory/Resolver/AwsS3ResolverFactory.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Liip\ImagineBundle\DependencyInjection\Factory\Resolver;
1313

1414
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
15+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
16+
use Symfony\Component\DependencyInjection\Alias;
1517
use Symfony\Component\DependencyInjection\ContainerBuilder;
1618
use Symfony\Component\DependencyInjection\Definition;
1719
use Symfony\Component\DependencyInjection\Reference;
@@ -21,10 +23,15 @@ class AwsS3ResolverFactory extends AbstractResolverFactory
2123
public function create(ContainerBuilder $container, $resolverName, array $config)
2224
{
2325
$awsS3ClientId = 'liip_imagine.cache.resolver.'.$resolverName.'.client';
24-
$awsS3ClientDefinition = new Definition('Aws\S3\S3Client');
25-
$awsS3ClientDefinition->setFactory(['Aws\S3\S3Client', 'factory']);
26-
$awsS3ClientDefinition->addArgument($config['client_config']);
27-
$container->setDefinition($awsS3ClientId, $awsS3ClientDefinition);
26+
27+
if ($config['client_id']) {
28+
$container->setAlias($awsS3ClientId, new Alias($config['client_id']));
29+
} else {
30+
$awsS3ClientDefinition = new Definition('Aws\S3\S3Client');
31+
$awsS3ClientDefinition->setFactory(['Aws\S3\S3Client', 'factory']);
32+
$awsS3ClientDefinition->addArgument($config['client_config']);
33+
$container->setDefinition($awsS3ClientId, $awsS3ClientDefinition);
34+
}
2835

2936
$resolverDefinition = $this->getChildResolverDefinition();
3037
$resolverDefinition->replaceArgument(0, new Reference($awsS3ClientId));
@@ -94,6 +101,9 @@ public function addConfiguration(ArrayNodeDefinition $builder)
94101
->scalarNode('cache_prefix')
95102
->defaultValue('')
96103
->end()
104+
->scalarNode('client_id')
105+
->defaultNull()
106+
->end()
97107
->arrayNode('client_config')
98108
->isRequired()
99109
->prototype('variable')
@@ -116,6 +126,22 @@ public function addConfiguration(ArrayNodeDefinition $builder)
116126
->prototype('scalar')
117127
->end()
118128
->end()
129+
->end()
130+
->beforeNormalization()
131+
->ifTrue(static function ($v) {
132+
return isset($v['client_id']) && isset($v['client_config']);
133+
})
134+
->thenInvalid('Children config "client_id" and "client_config" cannot be configured at the same time.')
135+
->end()
136+
->beforeNormalization()
137+
->ifTrue(static function ($v) {
138+
return isset($v['client_id']);
139+
})
140+
->then(function ($config) {
141+
$config['client_config'] = [];
142+
143+
return $config;
144+
})
119145
->end();
120146
}
121147
}

Tests/DependencyInjection/Factory/Resolver/AwsS3ResolverFactoryTest.php

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function testCreateResolverDefinitionOnCreate(): void
5454
$resolver = new AwsS3ResolverFactory();
5555

5656
$resolver->create($container, 'the_resolver_name', [
57+
'client_id' => null,
5758
'client_config' => [],
5859
'bucket' => 'theBucket',
5960
'acl' => 'theAcl',
@@ -85,6 +86,7 @@ public function testCreateS3ClientDefinitionOnCreate(): void
8586
$resolver = new AwsS3ResolverFactory();
8687

8788
$resolver->create($container, 'the_resolver_name', [
89+
'client_id' => null,
8890
'client_config' => ['theClientConfigKey' => 'theClientConfigVal'],
8991
'bucket' => 'aBucket',
9092
'acl' => 'aAcl',
@@ -108,6 +110,7 @@ public function testCreateS3ClientDefinitionWithFactoryOnCreate(): void
108110
$resolver = new AwsS3ResolverFactory();
109111

110112
$resolver->create($container, 'the_resolver_name', [
113+
'client_id' => null,
111114
'client_config' => ['theClientConfigKey' => 'theClientConfigVal'],
112115
'bucket' => 'aBucket',
113116
'acl' => 'aAcl',
@@ -121,13 +124,37 @@ public function testCreateS3ClientDefinitionWithFactoryOnCreate(): void
121124
$this->assertSame([S3Client::class, 'factory'], $clientDefinition->getFactory());
122125
}
123126

127+
public function testCreateS3ClientAliasOnCreate(): void
128+
{
129+
$container = new ContainerBuilder();
130+
131+
$resolver = new AwsS3ResolverFactory();
132+
133+
$resolver->create($container, 'the_resolver_name', [
134+
'client_id' => 's3.client.default',
135+
'client_config' => [],
136+
'bucket' => 'aBucket',
137+
'acl' => 'aAcl',
138+
'get_options' => [],
139+
'put_options' => [],
140+
'cache' => false,
141+
'proxies' => [],
142+
]);
143+
144+
$this->assertTrue($container->hasAlias('liip_imagine.cache.resolver.the_resolver_name.client'));
145+
146+
$clientAlias = $container->getAlias('liip_imagine.cache.resolver.the_resolver_name.client');
147+
$this->assertSame('s3.client.default', (string) $clientAlias);
148+
}
149+
124150
public function testWrapResolverWithProxyOnCreateWithoutCache(): void
125151
{
126152
$container = new ContainerBuilder();
127153

128154
$resolver = new AwsS3ResolverFactory();
129155

130156
$resolver->create($container, 'the_resolver_name', [
157+
'client_id' => null,
131158
'client_config' => [],
132159
'bucket' => 'aBucket',
133160
'acl' => 'aAcl',
@@ -162,6 +189,7 @@ public function testWrapResolverWithCacheOnCreateWithoutProxy(): void
162189
$resolver = new AwsS3ResolverFactory();
163190

164191
$resolver->create($container, 'the_resolver_name', [
192+
'client_id' => null,
165193
'client_config' => [],
166194
'bucket' => 'aBucket',
167195
'acl' => 'aAcl',
@@ -197,6 +225,7 @@ public function testWrapResolverWithProxyAndCacheOnCreate(): void
197225
$resolver = new AwsS3ResolverFactory();
198226

199227
$resolver->create($container, 'the_resolver_name', [
228+
'client_id' => null,
200229
'client_config' => [],
201230
'bucket' => 'aBucket',
202231
'acl' => 'aAcl',
@@ -240,6 +269,7 @@ public function testWrapResolverWithProxyMatchReplaceStrategyOnCreate(): void
240269
$resolver = new AwsS3ResolverFactory();
241270

242271
$resolver->create($container, 'the_resolver_name', [
272+
'client_id' => null,
243273
'client_config' => [],
244274
'bucket' => 'aBucket',
245275
'acl' => 'aAcl',
@@ -272,6 +302,7 @@ public function testSetCachePrefixIfDefined(): void
272302
$resolver = new AwsS3ResolverFactory();
273303

274304
$resolver->create($container, 'the_resolver_name', [
305+
'client_id' => null,
275306
'client_config' => [],
276307
'bucket' => 'aBucket',
277308
'acl' => 'aAcl',
@@ -311,7 +342,7 @@ public function testThrowBucketNotSetOnAddConfiguration(): void
311342
public function testThrowClientConfigNotSetOnAddConfiguration(): void
312343
{
313344
$this->expectException(\Symfony\Component\Config\Definition\Exception\InvalidConfigurationException::class);
314-
$this->expectExceptionMessageMatchesBC('/^The child (node|config) "client_config" (at path|under) "aws_s3" must be configured\.$/');
345+
$this->expectExceptionMessageMatchesBC('/^At least "client_id" or "client_config" child config under "aws_s3" must be configured.$/');
315346

316347
$treeBuilder = new TreeBuilder('aws_s3');
317348
$rootNode = method_exists(TreeBuilder::class, 'getRootNode')

0 commit comments

Comments
 (0)