Skip to content
This repository has been archived by the owner on Apr 17, 2021. It is now read-only.

Commit

Permalink
Closes #2 // Added S3 Stream configuration option to automatically re…
Browse files Browse the repository at this point in the history
…gister S3 stream for a given S3 Service client via configuration
  • Loading branch information
jmcclell committed Feb 12, 2014
1 parent e64b2ec commit dfa66bf
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 1 deletion.
9 changes: 9 additions & 0 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->scalarNode('service_prefix')
->info('Default service prefix used for all AWS service names in the Symfony Container, eg: jlm_aws.ec2')
->example('jlm_aws')
->defaultValue('jlm_aws')
->end()
->scalarNode('aws_base_class')
->info('Default base class to use as the AWS factory. Note: You *must* extend the JLM version of Aws or implement a similar version as the default Aws base class is not compatible with this bundle!')
->example('JLM\AwsBundle\Aws\Common\Aws')
->defaultValue('JLM\AwsBundle\Aws\Common\Aws')
->cannotBeEmpty()
->end()
->scalarNode('s3_stream_wrapper')
->info('Enables the S3 stream wrapper by either passing true (which will use the default S3 service) or by passing an S3 service name directly. Note: the service (whether default or not) *must* be enabled in the services configuration or you will receive an error!')
->example('my_s3')
->defaultFalse()
->end()
->arrayNode('default_settings')
->info('Default settings applied to all services. These override the SDK\'s defaults. (@see http://docs.aws.amazon.com/aws-sdk-php/guide/latest/configuration.html)')
->append($this->getCredentialConfigNode())
Expand Down
23 changes: 22 additions & 1 deletion DependencyInjection/JLMAwsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ public function load(array $configs, ContainerBuilder $container)
$awsConfig = $awsConfigTranslator->translateConfigToAwsConfig($config);

$this->generateServices($awsConfig, $container);

$this->registerS3StreamWrapper($config, $container);
}

private function registerS3StreamWrapper(array $config, ContainerBuilder $container)
{
$s3StreamWrapper = $config['s3_stream_wrapper'];
if(!empty($s3StreamWrapper)) {
if ($s3StreamWrapper === true) {
$s3StreamWrapper = 's3';
} else {
$s3StreamWrapper = 's3.' . $s3StreamWrapper;
}

$s3 = $container->get($this->servicePrefix . $s3StreamWrapper, ContainerInterface::IGNORE_ON_INVALID_REFERENCE);

if ($s3 == null) {
throw new \Exception("Configuration directive 's3_stream_wrapper' is set to '$s3StreamWrapper', but no S3 service is configured by that name.'");
}

$s3->registerStreamWrapper();
}
}

private function generateServices(array $awsConfig, ContainerBuilder $container)
Expand Down Expand Up @@ -130,5 +152,4 @@ private function resolveServices($value)

return $value;
}

}
25 changes: 25 additions & 0 deletions Tests/DependencyInjection/JLMAwsExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,4 +397,29 @@ public function testSymfonyConfigInheritance($format)
$this->assertEquals(array('override_only' => 'override_value', 'foo' => 'override_bar', 'blah' => 'bleh'), $opts['cookies']);
$this->assertEquals(array('override_only' => 'override_value', 'foo' => 'override_bar', 'blah' => 'bleh'), $opts['query']);
}

/**
* @dataProvider formatDataProvider
*/
public function testS3StreamWrapperTrue($format)
{
$client = $this->getClient('s3_stream_wrapper_true_' . $format);
$container = $client->getContainer();

$wrappers = stream_get_wrappers();
$this->assertTrue(in_array('s3', $wrappers));
}

/**
* @dataProvider formatDataProvider
*/
public function testS3StreamWrapperNamed($format)
{
$client = $this->getClient('s3_stream_wrapper_named_' . $format);
$container = $client->getContainer();

$wrappers = stream_get_wrappers();

$this->assertTrue(in_array('s3', $wrappers));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jlm-aws="http://symfony.com/schema/dic/jlm-aws"
>
<jlm-aws:config
s3_stream_wrapper="my_s3"
>
<jlm-aws:default-settings>
<jlm-aws:credentials
key="MY_UNUSED_KEY"
secret="MY_UNUSED_SECRET"
/>
</jlm-aws:default-settings>

<jlm-aws:services>
<jlm-aws:s3 name="my_s3"/>
</jlm-aws:services>
</jlm-aws:config>
</container>


Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jlm-aws="http://symfony.com/schema/dic/jlm-aws"
>
<jlm-aws:config
s3_stream_wrapper="true"
>
<jlm-aws:default-settings>
<jlm-aws:credentials
key="MY_UNUSED_KEY"
secret="MY_UNUSED_SECRET"
/>
</jlm-aws:default-settings>

<jlm-aws:services>
<jlm-aws:s3 />
</jlm-aws:services>
</jlm-aws:config>
</container>


0 comments on commit dfa66bf

Please sign in to comment.