Skip to content

Commit a1429fd

Browse files
committed
Merge pull request #18 from aws/refactor-config
Refactoring config to support package-level configuration. Fixes #17.
2 parents ccb255d + fa3811b commit a1429fd

13 files changed

+310
-166
lines changed

.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
11
composer.lock
22
composer.phar
3-
phpunit.xml
43
vendor

README.md

+24-55
Original file line numberDiff line numberDiff line change
@@ -18,24 +18,28 @@ The AWS Service Provider can be installed via [Composer](http://getcomposer.org)
1818
}
1919
```
2020

21-
## Usage
21+
## Configuration
22+
23+
To use the AWS Service Provider, you must register the provider when bootstrapping your Laravel application.
2224

23-
To use the AWS Service Provider, you must register the provider when bootstrapping your Laravel application. There are
24-
essentially two ways to do this.
25+
Publish the package configuration using Artisan.
2526

26-
### 1. Use Laravel Configuration
27+
```sh
28+
php artisan config:publish aws/aws-sdk-php-laravel
29+
```
2730

28-
Create a new `app/config/aws.php` configuration file with the following options:
31+
Update your settings in the generated `app/config/packages/aws/aws-sdk-php-laravel` configuration file.
2932

3033
```php
3134
return array(
32-
'key' => '<your-aws-access-key-id>',
33-
'secret' => '<your-aws-secret-access-key>',
34-
'region' => Aws\Common\Enum\Region::US_WEST_2,
35+
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
36+
'secret' => 'YOUR_AWS_SECRET_KEY',
37+
'region' => 'us-east-1',
38+
'config_file' => null,
3539
);
3640
```
3741

38-
Find the `providers` key in `app/config/app.php` and register the AWS Service Provider.
42+
Find the `providers` key in your `app/config/app.php` and register the AWS Service Provider.
3943

4044
```php
4145
'providers' => array(
@@ -44,7 +48,7 @@ Find the `providers` key in `app/config/app.php` and register the AWS Service Pr
4448
)
4549
```
4650

47-
Find the `aliases` key in `app/config/app.php` and add the AWS facade alias.
51+
Find the `aliases` key in your `app/config/app.php` and add the AWS facade alias.
4852

4953
```php
5054
'aliases' => array(
@@ -53,64 +57,29 @@ Find the `aliases` key in `app/config/app.php` and add the AWS facade alias.
5357
)
5458
```
5559

56-
### 2. Manual Instantiation
57-
58-
You can also register the provider and configuration options at runtime. This could be done in your global bootstrapping
59-
process in `app/start/global.php`.
60-
61-
```php
62-
use Aws\Common\Enum\Region;
63-
use Aws\Laravel\AwsServiceProvider;
64-
use Illuminate\Foundation\Application;
65-
66-
// Instantiate a new application. This is normally done by the Laravel framework and the instance is available in
67-
// `app/start/global.php` for you to use.
68-
$app = new Application;
69-
70-
// Register the AWS service provider and provide your configuration
71-
$app->register(new AwsServiceProvider($app), array(
72-
'config' => array(
73-
'aws' => array(
74-
'key' => '<your-aws-access-key-id>',
75-
'secret' => '<your-aws-secret-access-key>',
76-
'region' => Region::US_WEST_2,
77-
),
78-
),
79-
));
80-
```
81-
82-
You can alternatively specify the path to an AWS config file (see [AWS SDK for PHP](http://github.com/aws/aws-sdk-php)
83-
for details about how to format this type of file).
84-
85-
```php
86-
$app->register(new AwsServiceProvider($app), array('config' => array('aws' => '/path/to/aws/config/file.php')));
87-
```
88-
89-
Either way, the value of `$app['config']['aws']` is passed directly into `Aws\Common\Aws::factory()`.
90-
91-
### Retrieving and Using a Service Client
60+
## Usage
9261

93-
In order to use the SDK from within your app, you need to retrieve it from the [Laravel IoC
62+
In order to use the AWS SDK for PHP within your app, you need to retrieve it from the [Laravel IoC
9463
Container](http://four.laravel.com/docs/ioc). The following example uses the Amazon S3 client to upload a file.
9564

9665
```php
9766
$s3 = App::make('aws')->get('s3');
9867
$s3->putObject(array(
99-
'Bucket' => '<your-bucket>',
100-
'Key' => '<the-name-of-your-object>',
101-
'SourceFile' => '/path/to/the/file/you/are/uploading.ext',
68+
'Bucket' => 'YOUR_BUCKET',
69+
'Key' => 'YOUR_OBJECT_KEY',
70+
'SourceFile' => '/the/path/to/the/file/you/are/uploading.ext',
10271
));
10372
```
10473

105-
If the AWS Facade is registered within the `aliases` section of the application configuration, you can use
106-
the following more expressive method.
74+
If the AWS facade is registered within the `aliases` section of the application configuration, you can also use the
75+
following technique.
10776

10877
```php
10978
$s3 = AWS::get('s3');
11079
$s3->putObject(array(
111-
'Bucket' => '<your-bucket>',
112-
'Key' => '<the-name-of-your-object>',
113-
'SourceFile' => '/path/to/the/file/you/are/uploading.ext',
80+
'Bucket' => 'YOUR_BUCKET',
81+
'Key' => 'YOUR_OBJECT_KEY',
82+
'SourceFile' => '/the/path/to/the/file/you/are/uploading.ext',
11483
));
11584
```
11685

phpunit.xml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit backupGlobals="false"
3+
backupStaticAttributes="false"
4+
bootstrap="./tests/bootstrap.php"
5+
colors="false"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
processIsolation="false"
10+
stopOnFailure="false"
11+
syntaxCheck="false">
12+
13+
<testsuites>
14+
<testsuite name="AwsServiceProvider Test Suite">
15+
<directory>./tests/</directory>
16+
</testsuite>
17+
</testsuites>
18+
19+
<filter>
20+
<whitelist addUncoveredFilesFromWhitelist="false">
21+
<directory suffix=".php">src</directory>
22+
<exclude>
23+
<directory suffix=".php">vendor</directory>
24+
</exclude>
25+
</whitelist>
26+
</filter>
27+
</phpunit>

phpunit.xml.dist

-9
This file was deleted.

src/Aws/Laravel/AwsFacade.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
use Illuminate\Support\Facades\Facade;
2020

2121
/**
22-
* AWS SDK for PHP service provider for Laravel applications
22+
* Facade for the AWS service
2323
*/
2424
class AwsFacade extends Facade
2525
{

src/Aws/Laravel/AwsServiceProvider.php

+28-9
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,32 @@
2828
*/
2929
class AwsServiceProvider extends ServiceProvider
3030
{
31+
const VERSION = '1.1.0';
32+
3133
/**
32-
* @inheritdoc
34+
* Register the service provider.
35+
*
36+
* @return void
3337
*/
3438
public function register()
3539
{
3640
$this->app['aws'] = $this->app->share(function ($app) {
41+
// Retrieve the config
42+
$config = $app['config']['aws'] ?: $app['config']['aws::config'];
43+
if (isset($config['config_file'])) {
44+
$config = $config['config_file'];
45+
}
46+
3747
// Instantiate the AWS service builder
38-
$config = !empty($app['config']['aws']) ? $app['config']['aws'] : array();
3948
$aws = Aws::factory($config);
4049

41-
// Attach an event listener that will append the Laravel version number in the user agent string
50+
// Attach an event listener that will append the Laravel and module version numbers to the user agent string
4251
$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
4752
$clientConfig = $event['client']->getConfig();
4853
$commandParams = $clientConfig->get(Client::COMMAND_PARAMS) ?: array();
54+
$userAgentSuffix = 'Laravel/' . Application::VERSION . ' L4MOD/' . AwsServiceProvider::VERSION;
4955
$clientConfig->set(Client::COMMAND_PARAMS, array_merge_recursive($commandParams, array(
50-
UserAgentListener::OPTION => "Laravel/{$version}",
56+
UserAgentListener::OPTION => $userAgentSuffix,
5157
)));
5258
});
5359

@@ -56,9 +62,22 @@ public function register()
5662
}
5763

5864
/**
59-
* @inheritdoc
65+
* Bootstrap the application events.
66+
*
67+
* @return void
6068
*/
6169
public function boot()
6270
{
71+
$this->package('aws/aws-sdk-php-laravel', 'aws');
72+
}
73+
74+
/**
75+
* Get the services provided by the provider.
76+
*
77+
* @return array
78+
*/
79+
public function provides()
80+
{
81+
return array('aws');
6382
}
6483
}

src/config/config.php

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
/**
3+
* Copyright 2013 Amazon.com, Inc. or its affiliates. All Rights Reserved.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License").
6+
* You may not use this file except in compliance with the License.
7+
* A copy of the License is located at
8+
*
9+
* http://aws.amazon.com/apache2.0
10+
*
11+
* or in the "license" file accompanying this file. This file is distributed
12+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
13+
* express or implied. See the License for the specific language governing
14+
* permissions and limitations under the License.
15+
*/
16+
17+
return array(
18+
19+
/*
20+
|--------------------------------------------------------------------------
21+
| Your AWS Credentials
22+
|--------------------------------------------------------------------------
23+
|
24+
| In order to communicate with an AWS service, you must provide your AWS
25+
| credentials including your AWS Access Key ID and your AWS Secret Key. You
26+
| can obtain these keys by logging into your AWS account and visiting
27+
| https://console.aws.amazon.com/iam/home?#security_credential.
28+
|
29+
| To use credentials from your environment or to use IAM Instance Profile
30+
| credentials, please remove these config settings from your config. For
31+
| more information see http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/configuration.html
32+
|
33+
*/
34+
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
35+
'secret' => 'YOUR_AWS_SECRET_KEY',
36+
37+
/*
38+
|--------------------------------------------------------------------------
39+
| AWS Region
40+
|--------------------------------------------------------------------------
41+
|
42+
| Many AWS services are available in multiple regions. You should specify
43+
| the AWS region you would like to use, but please remember that not every
44+
| service is available in every region.
45+
|
46+
| These are the regions: us-east-1, us-west-1, us-west-2, us-gov-west-1
47+
| eu-west-1, sa-east-1, ap-northeast-1, ap-southeast-1, ap-southeast-2
48+
|
49+
*/
50+
'region' => 'us-east-1',
51+
52+
/*
53+
|--------------------------------------------------------------------------
54+
| AWS Config File Location
55+
|--------------------------------------------------------------------------
56+
|
57+
| Instead of specifying your credentials and region here, you can specify
58+
| the location of an AWS SDK for PHP config file to use. These files provide
59+
| more granular control over what credentials and regions you are using for
60+
| each service. If you specify a filepath for this configuration setting,
61+
| the others in this file will be ignored. See the SDK user guide for more
62+
| information: http://docs.aws.amazon.com/aws-sdk-php-2/guide/latest/configuration.html#using-a-custom-configuration-file
63+
|
64+
*/
65+
'config_file' => null,
66+
67+
);

tests/Aws/Laravel/Tests/AwsFacadeTest.php renamed to tests/Aws/Laravel/Test/AwsFacadeTest.php

+6-11
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,24 @@
1414
* permissions and limitations under the License.
1515
*/
1616

17-
namespace Aws\Laravel\Tests;
17+
namespace Aws\Laravel\Test;
1818

1919
use Aws\Laravel\AwsFacade as AWS;
20-
use Aws\Laravel\AwsServiceProvider;
21-
use Illuminate\Foundation\Application;
2220

2321
/**
2422
* AwsFacade test cases
2523
*/
26-
class AwsFacadeTest extends \PHPUnit_Framework_TestCase
24+
class AwsFacadeTest extends AwsServiceProviderTestCase
2725
{
2826
public function testFacadeCanBeResolvedToServiceInstance()
2927
{
30-
// Setup the Laravel app and AWS service provider
31-
$app = new Application();
32-
$app['config'] = array();
33-
$provider = new AwsServiceProvider($app);
34-
$app->register($provider);
35-
$provider->boot();
28+
$app = $this->setupApplication();
29+
$this->setupServiceProvider($app);
3630

31+
// Mount facades
3732
AWS::setFacadeApplication($app);
3833

39-
// Get an instance of a client (S3) to use for testing
34+
// Get an instance of a client (S3) via its facade
4035
$s3 = AWS::get('s3');
4136
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
4237
}

0 commit comments

Comments
 (0)