Skip to content

Commit 23f939d

Browse files
committed
[WIP] Refactoring config to support package-level configuration.
1 parent ccb255d commit 23f939d

File tree

7 files changed

+148
-98
lines changed

7 files changed

+148
-98
lines changed

src/Aws/Laravel/AwsServiceProvider.php

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,30 @@
2828
*/
2929
class AwsServiceProvider extends ServiceProvider
3030
{
31+
const VERSION = '1.1.0';
32+
3133
/**
3234
* @inheritdoc
3335
*/
3436
public function register()
3537
{
3638
$this->app['aws'] = $this->app->share(function ($app) {
39+
// Retrieve the config
40+
$config = $app['config']['aws'] ?: $app['config']['aws::config'] ?: array();
41+
if (isset($config['config_file'])) {
42+
$config = $config['config_file'];
43+
}
44+
3745
// Instantiate the AWS service builder
38-
$config = !empty($app['config']['aws']) ? $app['config']['aws'] : array();
3946
$aws = Aws::factory($config);
4047

41-
// Attach an event listener that will append the Laravel version number in the user agent string
48+
// Attach an event listener that will append the Laravel and module version numbers to the user agent string
4249
$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
4750
$clientConfig = $event['client']->getConfig();
4851
$commandParams = $clientConfig->get(Client::COMMAND_PARAMS) ?: array();
52+
$userAgentSuffix = 'Laravel/' . Application::VERSION . ' L4MOD/' . AwsServiceProvider::VERSION;
4953
$clientConfig->set(Client::COMMAND_PARAMS, array_merge_recursive($commandParams, array(
50-
UserAgentListener::OPTION => "Laravel/{$version}",
54+
UserAgentListener::OPTION => $userAgentSuffix,
5155
)));
5256
});
5357

@@ -60,5 +64,6 @@ public function register()
6064
*/
6165
public function boot()
6266
{
67+
$this->package('aws/aws-sdk-php-laravel', 'aws');
6368
}
6469
}

src/config/config.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
return array(
4+
'key' => 'YOUR_AWS_ACCESS_KEY_ID',
5+
'secret' => 'YOUR_AWS_SECRET_KEY',
6+
//'region' => 'YOUR_REGION',
7+
);

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

Lines changed: 6 additions & 11 deletions
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
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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+
namespace Aws\Laravel\Test;
18+
19+
/**
20+
* AwsServiceProvider test cases
21+
*/
22+
class AwsServiceProviderTest extends AwsServiceProviderTestCase
23+
{
24+
public function testRegisterAwsServiceProviderWithGlobalConfig()
25+
{
26+
$app = $this->setupApplication();
27+
$this->setupServiceProvider($app);
28+
29+
// Simulate global config
30+
$app['config']->set('aws', array(
31+
'key' => 'FOO',
32+
'secret' => 'BAR',
33+
));
34+
35+
// Get an instance of a client (S3)
36+
/** @var $s3 \Aws\S3\S3Client */
37+
$s3 = $app['aws']->get('s3');
38+
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
39+
40+
// Verify that the client received the credentials from the global config
41+
$this->assertEquals('FOO', $s3->getCredentials()->getAccessKeyId());
42+
$this->assertEquals('BAR', $s3->getCredentials()->getSecretKey());
43+
44+
// Make sure the user agent contains Laravel information
45+
$command = $s3->getCommand('ListBuckets');
46+
$request = $command->prepare();
47+
$s3->dispatch('command.before_send', array('command' => $command));
48+
$this->assertRegExp('/.+Laravel\/.+L4MOD\/.+/', (string) $request->getHeader('User-Agent'));
49+
}
50+
51+
public function testRegisterAwsServiceProviderWithPackageConfig()
52+
{
53+
$app = $this->setupApplication();
54+
$this->setupServiceProvider($app);
55+
56+
// Get an instance of a client (S3)
57+
/** @var $s3 \Aws\S3\S3Client */
58+
$s3 = $app['aws']->get('s3');
59+
$this->assertInstanceOf('Aws\S3\S3Client', $s3);
60+
61+
// Verify that the client received the credentials from the package config
62+
$this->assertEquals('YOUR_AWS_ACCESS_KEY_ID', $s3->getCredentials()->getAccessKeyId());
63+
$this->assertEquals('YOUR_AWS_SECRET_KEY', $s3->getCredentials()->getSecretKey());
64+
}
65+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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+
namespace Aws\Laravel\Test;
18+
19+
use Aws\Laravel\AwsServiceProvider;
20+
use Illuminate\Config\Repository;
21+
use Illuminate\Foundation\Application;
22+
use Illuminate\Filesystem\Filesystem;
23+
24+
/**
25+
* AwsServiceProvider Base Test Case
26+
*/
27+
abstract class AwsServiceProviderTestCase extends \PHPUnit_Framework_TestCase
28+
{
29+
/**
30+
* @return Application
31+
*/
32+
protected function setupApplication()
33+
{
34+
// Create the application such that the config is loaded
35+
$app = new Application();
36+
$app->instance('path', 'foobar');
37+
$app->instance('files', new Filesystem);
38+
$app->instance('config', new Repository($app->getConfigLoader(), 'foobar'));
39+
40+
return $app;
41+
}
42+
43+
/**
44+
* @param Application $app
45+
*
46+
* @return AwsServiceProvider
47+
*/
48+
protected function setupServiceProvider(Application $app)
49+
{
50+
// Create and register the provider
51+
$provider = new AwsServiceProvider($app);
52+
$app->register($provider);
53+
$provider->boot();
54+
55+
return $provider;
56+
}
57+
}

tests/Aws/Laravel/Tests/AwsServiceProviderTest.php

Lines changed: 0 additions & 80 deletions
This file was deleted.

tests/bootstrap.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
<?php
22

33
require_once __DIR__ . '/../vendor/autoload.php';
4+
require_once __DIR__ . '/Aws/Laravel/Test/AwsServiceProviderTestCase.php';

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy