Skip to content
This repository has been archived by the owner on Jun 1, 2023. It is now read-only.

Commit

Permalink
#30 Add functionality to set up multiple AWS SDK instances
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Gorenkin committed Oct 9, 2018
1 parent c9e2d2e commit 9022e0c
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 19 deletions.
30 changes: 30 additions & 0 deletions src/AwsBundle/Client/AwsSdks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace Ruwork\AwsBundle\Client;

use Aws\Sdk;
use Symfony\Component\DependencyInjection\ServiceLocator;

final class AwsSdks
{
private $locator;
private $defaultName;

public function __construct(ServiceLocator $locator, string $defaultName)
{
$this->locator = $locator;
$this->defaultName = $defaultName;
}

public function has(string $name): bool
{
return $this->locator->has($name);
}

public function get(?string $name = null): Sdk
{
return $this->locator->get($name ?? $this->defaultName);
}
}
43 changes: 39 additions & 4 deletions src/AwsBundle/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,48 @@ public function getConfigTreeBuilder()
// @formatter:off
return (new TreeBuilder())
->root('ruwork_aws')
->beforeNormalization()
->ifTrue(function ($value): bool {
return \is_array($value) && [] !== $value && !\array_key_exists('sdks', $value);
})
->then(function (array $value): array {
return [
'sdks' => [
'default' => $value,
],
'default_sdk' => 'default',
];
})
->end()
->children()
->scalarNode('http_handler')
->defaultValue(\interface_exists(HttpClient::class) ? HttplugHandler::class : null)
->arrayNode('sdks')
->isRequired()
->arrayPrototype()
->children()
->scalarNode('http_handler')
->defaultValue(\interface_exists(HttpClient::class) ? HttplugHandler::class : null)
->end()
->end()
->ignoreExtraKeys(false)
->end()
->end()
->scalarNode('default_sdk')
->isRequired()
->cannotBeEmpty()
->end()
->end()
->ignoreExtraKeys(false)
->end();
->validate()
->ifTrue(function ($value): bool {
return \is_array($value)
&& isset($value['default_sdk'])
&& isset($value['sdks'])
&& !isset($value['sdks'][$value['default_sdk']]);
})
->then(function (array $value): void {
throw new \InvalidArgumentException(\sprintf('SDK "%s" is not defined and cannot be used as default.', $value['default_sdk']));
})
->end()
->end();
// @formatter:on
}
}
41 changes: 35 additions & 6 deletions src/AwsBundle/DependencyInjection/RuworkAwsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

use Aws\Sdk;
use Http\Client\HttpClient;
use Ruwork\AwsBundle\Client\AwsSdks;
use Ruwork\AwsBundle\HttpHandler\HttplugHandler;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\Compiler\ServiceLocatorTagPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Loader\PhpFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\ConfigurableExtension;
Expand All @@ -24,16 +28,41 @@ public function loadInternal(array $config, ContainerBuilder $container): void
$loader = new PhpFileLoader($container, $locator);
$loader->load('services.php');

if (isset($config['http_handler'])) {
$config['http_handler'] = new Reference($config['http_handler']);
}
if (\class_exists(Sdk::class)) {
$references = [];
foreach ($config['sdks'] as $name => $value) {
if (isset($value['http_handler'])) {
$value['http_handler'] = new Reference($value['http_handler']);
}
$references[$name] = $this->registerSdk($container, $name, $value);
}

$container
->findDefinition(Sdk::class)
->setArgument(0, $config);
$sdkAlias = new Alias('ruwork_aws.sdk.'.$config['default_sdk'], false);
$container->setAlias(Sdk::class, $sdkAlias);
$container->setAlias('ruwork_aws.sdk._default', $sdkAlias);

$container
->findDefinition(AwsSdks::class)
->setArguments([
'$locator' => ServiceLocatorTagPass::register($container, $references),
'$defaultName' => $config['default_sdk'],
]);
}

if (!\interface_exists(HttpClient::class)) {
$container->removeDefinition(HttplugHandler::class);
}
}

public function registerSdk(ContainerBuilder $container, string $name, array $config): Reference
{
$id = 'ruwork_aws.sdk.'.$name;

$container
->setDefinition($id, new Definition(Sdk::class))
->setPublic(false)
->setArgument(0, $config);

return new Reference($id);
}
}
5 changes: 5 additions & 0 deletions src/AwsBundle/Resources/config/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

use Aws\Sdk;
use Aws\Ses\SesClient;
use Ruwork\AwsBundle\Client\AwsSdks;
use Ruwork\AwsBundle\HttpHandler\HttplugHandler;

return function (ContainerConfigurator $container): void {
Expand All @@ -23,6 +24,10 @@
->set(SesClient::class)
->factory([ref(Sdk::class), 'createSes']);

// Sdks

$services->set(AwsSdks::class);

// HttpHandler

$services
Expand Down
35 changes: 26 additions & 9 deletions tests/AwsBundle/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,41 @@ class ConfigurationTest extends TestCase
{
use ConfigurationTestCaseTrait;

public function testDefault(): void
public function testDefaults(): void
{
$this->assertProcessedConfigurationEquals([], [
'http_handler' => HttplugHandler::class,
]);
$this->assertProcessedConfigurationEquals(
[
'sdks' => [
'extra_value' => 'value',
],
],
[
'sdks' => [
'default' => [
'http_handler' => HttplugHandler::class,
'extra_value' => 'value',
],
],
'default_sdk' => 'default',
]
);
}

public function testExtraValues(): void
public function testSingleSdkExpanding(): void
{
$this->assertProcessedConfigurationEquals(
[
[
'extra' => 'value',
'sdks' => [
'http_handler' => HttplugHandler::class,
],
],
[
'extra' => 'value',
'http_handler' => HttplugHandler::class,
'sdks' => [
'default' => [
'http_handler' => HttplugHandler::class,
],
],
'default_sdk' => 'default',
]
);
}
Expand Down

0 comments on commit 9022e0c

Please sign in to comment.