Skip to content

Commit

Permalink
Adjust variables and use Environment class
Browse files Browse the repository at this point in the history
  • Loading branch information
Damian Mooyman committed Nov 20, 2017
1 parent 73622f8 commit 76d8d23
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 25 deletions.
8 changes: 4 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ sudo: false
env:
global:
- COMPOSER_ROOT_VERSION=1.0.x-dev
- CORE_RELEASE=master

matrix:
fast_finish: true
Expand All @@ -19,12 +18,13 @@ matrix:

before_script:
- composer self-update || true
- "if [ \"$PHPCS_TEST\" = \"1\" ]; then pyrus install pear/PHP_CodeSniffer; fi"
- export PATH=~/.composer/vendor/bin:$PATH
- phpenv rehash
- phpenv config-rm xdebug.ini
- composer validate
- composer install --prefer-dist
- if [[ $PHPCS_TEST ]]; then composer global require squizlabs/php_codesniffer:^3 --prefer-dist --no-interaction --no-progress --no-suggest -o; fi

script:
- "if [ \"$PHPUNIT_TEST\" = \"1\" ]; then vendor/bin/phpunit; fi"
- "if [ \"$PHPCS_TEST\" = \"1\" ]; then composer run-script lint; fi"
- if [[ $PHPUNIT_TEST ]]; then vendor/bin/phpunit; fi
- if [[ $PHPCS_TEST ]]; then composer run-script lint; fi
9 changes: 6 additions & 3 deletions _config.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php
$dynamoSession = \SilverStripe\DynamoDb\Model\DynamoDbSession::get();
if($dynamoSession) {
$dynamoSession->register();

use SilverStripe\DynamoDb\Model\DynamoDbSession;

$dynamoSession = DynamoDbSession::get();
if ($dynamoSession) {
$dynamoSession->register();
}
33 changes: 17 additions & 16 deletions code/Model/DynamoDbSession.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\Common\Cache\ApcuCache;
use SilverStripe\Core\Config\Config;
use SilverStripe\Control\Session;
use SilverStripe\Core\Environment;

class DynamoDbSession
{
Expand Down Expand Up @@ -44,32 +45,32 @@ public function getHandler()
public static function get()
{
// Use DynamoDB for distributed session storage if it's configured
$AWS_DYNAMODB_SESSION_TABLE = getenv('AWS_DYNAMODB_SESSION_TABLE');
if (!empty($AWS_DYNAMODB_SESSION_TABLE)) {
$AWS_REGION_NAME = getenv('AWS_REGION_NAME');
$AWS_DYNAMODB_ENDPOINT = getenv('AWS_DYNAMODB_ENDPOINT');
$AWS_ACCESS_KEY = getenv('AWS_ACCESS_KEY');
$AWS_SECRET_KEY = getenv('AWS_SECRET_KEY');
$awsDynamoDBSessionTable = Environment::getEnv('AWS_DYNAMODB_SESSION_TABLE');
if (!empty($awsDynamoDBSessionTable)) {
$awsRegionName = Environment::getEnv('AWS_REGION_NAME');
$awsDynamoDBEndpoint = Environment::getEnv('AWS_DYNAMODB_ENDPOINT');
$awsAccessKey = Environment::getEnv('AWS_ACCESS_KEY');
$awsSecretKey = Environment::getEnv('AWS_SECRET_KEY');

$dynamoOptions = array('region' => $AWS_REGION_NAME);
$dynamoOptions = array('region' => $awsRegionName);

// This endpoint can be set for locally testing DynamoDB.
// see http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocal.html
if (!empty($AWS_DYNAMODB_ENDPOINT)) {
$dynamoOptions['endpoint'] = $AWS_DYNAMODB_ENDPOINT;
if (!empty($awsDynamoDBEndpoint)) {
$dynamoOptions['endpoint'] = $awsDynamoDBEndpoint;
}

if (!empty($AWS_ACCESS_KEY) && !empty($AWS_SECRET_KEY)) {
$dynamoOptions['credentials']['key'] = $AWS_ACCESS_KEY;
$dynamoOptions['credentials']['secret'] = $AWS_SECRET_KEY;
if (!empty($awsAccessKey) && !empty($awsSecretKey)) {
$dynamoOptions['credentials']['key'] = $awsAccessKey;
$dynamoOptions['credentials']['secret'] = $awsSecretKey;
} else {
// cache credentials when IAM fetches the credentials from EC2 metadata service
// this will use doctrine/cache (included via composer) to do the actual caching into APCu
// http://docs.aws.amazon.com/aws-sdk-php/guide/latest/performance.html#cache-instance-profile-credentials
$dynamoOptions['credentials'] = new DoctrineCacheAdapter(new ApcuCache());
}

return new static($dynamoOptions, $AWS_DYNAMODB_SESSION_TABLE);
return new static($dynamoOptions, $awsDynamoDBSessionTable);
}

return null;
Expand Down Expand Up @@ -98,9 +99,9 @@ public function __construct($options, $table)
*/
protected function getSessionLifetime()
{
$AWS_DYNAMODB_SESSION_LIFETIME = getenv('AWS_DYNAMODB_SESSION_LIFETIME');
if (!empty($AWS_DYNAMODB_SESSION_LIFETIME)) {
return $AWS_DYNAMODB_SESSION_LIFETIME;
$awsDynamoDBSessionLifetime = Environment::getEnv('AWS_DYNAMODB_SESSION_LIFETIME');
if (!empty($awsDynamoDBSessionLifetime)) {
return $awsDynamoDBSessionLifetime;
}
if (($timeout = (int)Config::inst()->get(Session::class, 'timeout')) > 0) {
return $timeout;
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "silverstripe/dynamodb",
"description": "SilverStripe DynamoDB integration.",
"type": "silverstripe-module",
"type": "silverstripe-vendormodule",
"keywords": ["silverstripe", "aws", "dynamodb"],
"license": "BSD-3-Clause",
"authors": [
Expand Down
2 changes: 1 addition & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Standard module phpunit configuration.
Requires PHPUnit ^5.7
-->
<phpunit bootstrap="framework/tests/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
<testsuite name="Default">
<directory>tests/</directory>
</testsuite>
Expand Down

0 comments on commit 76d8d23

Please sign in to comment.