Skip to content

Commit 37dc939

Browse files
committed
Fixed an issue with the recent PR and added a test to make sure the provider works when no config is provided
1 parent 5fbfa64 commit 37dc939

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

src/Aws/Laravel/AwsServiceProvider.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,10 @@
1818

1919
use Aws\Common\Aws;
2020
use Aws\Common\Client\UserAgentListener;
21-
use Aws\Common\Exception\RuntimeException;
2221
use Guzzle\Common\Event;
2322
use Guzzle\Service\Client;
24-
use Illuminate\Support\ServiceProvider;
2523
use Illuminate\Foundation\Application;
24+
use Illuminate\Support\ServiceProvider;
2625

2726
/**
2827
* AWS SDK for PHP service provider for Laravel applications
@@ -36,15 +35,19 @@ public function register()
3635
{
3736
$this->app['aws'] = $this->app->share(function ($app) {
3837
// Instantiate the AWS service builder
39-
$config = isset($app['config']['aws']) ? $app['config']['aws'] : array();
38+
$config = (isset($app['config']) && isset($app['config']['aws'])) ? $app['config']['aws'] : array();
4039
$aws = Aws::factory($config);
4140

4241
// Attach an event listener that will append the Laravel version number in the user agent string
4342
$aws->getEventDispatcher()->addListener('service_builder.create_client', function (Event $event) {
43+
// The version number is only available in BETA4+, so an extra check is needed
44+
$version = defined('Illuminate\Foundation\Application::VERSION') ? Application::VERSION : '4.0.0';
45+
46+
// Add the listener to modify the UA string
4447
$clientConfig = $event['client']->getConfig();
4548
$commandParams = $clientConfig->get(Client::COMMAND_PARAMS) ?: array();
4649
$clientConfig->set(Client::COMMAND_PARAMS, array_merge_recursive($commandParams, array(
47-
UserAgentListener::OPTION => 'Laravel' . ( defined('Application::VERSION') ? '/' . Application::VERSION : '' ),
50+
UserAgentListener::OPTION => "Laravel/{$version}",
4851
)));
4952
});
5053

tests/Aws/Laravel/Tests/AwsServiceProviderTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public function testRegisterAwsServiceProvider()
4040
$provider->boot();
4141

4242
// Get an instance of a client (S3) to use for testing
43+
/** @var $s3 \Aws\S3\S3Client */
4344
$s3 = $app['aws']->get('s3');
45+
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
4446

4547
// Verify that the app and clients created by the SDK receive the provided credentials
4648
$this->assertEquals('your-aws-access-key-id', $app['config']['aws']['key']);
@@ -54,4 +56,24 @@ public function testRegisterAwsServiceProvider()
5456
$s3->dispatch('command.before_send', array('command' => $command));
5557
$this->assertRegExp('/.+Laravel\/.+/', $request->getHeader('User-Agent', true));
5658
}
59+
60+
/**
61+
* @expectedException \Aws\Common\Exception\InstanceProfileCredentialsException
62+
*/
63+
public function testNoConfigProvided()
64+
{
65+
// Setup the Laravel app and AWS service provider
66+
$app = new Application();
67+
$provider = new AwsServiceProvider($app);
68+
$app->register($provider);
69+
$provider->boot();
70+
71+
// Make sure we can still get the S3Client
72+
/** @var $s3 \Aws\S3\S3Client */
73+
$s3 = $app['aws']->get('s3');
74+
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
75+
76+
// Trigger the expected exception
77+
$s3->getCredentials()->getAccessKeyId();
78+
}
5779
}

0 commit comments

Comments
 (0)