Skip to content

Commit

Permalink
Updated the factory to read the version from the manifest file
Browse files Browse the repository at this point in the history
  • Loading branch information
kschroeder committed Nov 20, 2017
1 parent 28c265c commit 92c0d35
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 66 deletions.
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,8 @@
"require": {
"aws/aws-sdk-php": "^3.33",
"magium/configuration-manager": "^1"
},
"require-dev": {
"phpunit/phpunit": "^6.4"
}
}
97 changes: 31 additions & 66 deletions lib/AwsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,7 @@

namespace Magium\AwsFactory;

use Aws\ApiGateway\ApiGatewayClient;
use Aws\AutoScaling\AutoScalingClient;
use Aws\AwsClient;
use Aws\CloudDirectory\CloudDirectoryClient;
use Aws\CloudFormation\CloudFormationClient;
use Aws\CloudFront\CloudFrontClient;
use Aws\CloudSearch\CloudSearchClient;
use Aws\CloudWatch\CloudWatchClient;
use Aws\CodeDeploy\CodeDeployClient;
use Aws\CognitoIdentity\CognitoIdentityClient;
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
use Aws\DirectoryService\DirectoryServiceClient;
use Aws\DynamoDb\DynamoDbClient;
use Aws\Ec2\Ec2Client;
use Aws\Ecr\EcrClient;
use Aws\Ecs\EcsClient;
use Aws\Efs\EfsClient;
use Aws\ElastiCache\ElastiCacheClient;
use Aws\ElasticLoadBalancing\ElasticLoadBalancingClient;
use Aws\ElasticLoadBalancingV2\ElasticLoadBalancingV2Client;
use Aws\ElasticsearchService\ElasticsearchServiceClient;
use Aws\Iam\IamClient;
use Aws\Rds\RdsClient;
use Aws\Route53\Route53Client;
use Aws\S3\S3Client;
use Aws\Ses\SesClient;
use Aws\Sms\SmsClient;
use Aws\Sns\SnsClient;
use Aws\Sqs\SqsClient;
use Aws\Sts\StsClient;
use Magium\Configuration\Config\Repository\ConfigInterface;

class AwsFactory
Expand All @@ -47,37 +18,7 @@ class AwsFactory
* @var array
*/

private static $versions = [
Ec2Client::class => '2016-11-15',
ApiGatewayClient::class => '2015-07-09',
AutoScalingClient::class => '2011-01-01',
CloudDirectoryClient::class => '2016-05-10',
CloudFormationClient::class => '2010-05-15',
CloudFrontClient::class => '2017-03-25',
CloudSearchClient::class => '2013-01-01',
CloudWatchClient::class => '2010-08-01',
CodeDeployClient::class => '2014-10-06',
CognitoIdentityClient::class => '2014-06-30',
CognitoIdentityProviderClient::class => '2016-04-18',
DirectoryServiceClient::class => '2015-04-16',
DynamoDbClient::class => '2012-08-10',
EcsClient::class => '2014-11-13',
EcrClient::class => '2015-09-21',
EfsClient::class => '2015-02-01',
ElastiCacheClient::class => '2015-02-02',
ElasticLoadBalancingClient::class => '2012-06-01',
ElasticLoadBalancingV2Client::class => '2015-12-01',
ElasticsearchServiceClient::class => '2015-01-01',
IamClient::class => '2010-05-08',
RdsClient::class => '2014-10-31',
Route53Client::class => '2013-04-01',
S3Client::class => '2006-03-01',
SesClient::class => '2010-12-01',
SmsClient::class => '2016-10-24',
SnsClient::class => '2010-03-31',
SqsClient::class => '2012-11-05',
StsClient::class => '2011-06-15'
];
private static $versions = [];

private $config;
private static $self;
Expand All @@ -88,22 +29,46 @@ public function __construct(ConfigInterface $config)
self::$self = $this;
}

public static function setVersion($class, $version)
private static function getDataDir($class)
{
self::$versions[$class] = $version;
// We use the class to navigate our way to the data directory
$classParts = explode('\\', $class);
array_pop($classParts);
$reflection = new \ReflectionClass($class);
$dataDirName = dirname($reflection->getFileName());
$parts = explode(DIRECTORY_SEPARATOR, $dataDirName);
do {
$cwd = array_pop($parts);
} while (count($parts) > 0 && $cwd != 'aws-sdk-php');

return implode(DIRECTORY_SEPARATOR, $parts)
. DIRECTORY_SEPARATOR
. implode(DIRECTORY_SEPARATOR , ['aws-sdk-php', 'src', 'data']);
}

public static function getVersionFor($class)
{
if (!isset(self::$versions[$class])) {
throw new UnknownVersionException('Could not find version for ' . $class);
$namespace = explode('\\', $class);
array_pop($namespace); // Get rid of the class name
$namespace = array_pop($namespace); // Get the highest level namespace

if (!isset(self::$versions[$namespace])) {
$manifestFile = self::getDataDir($class) . DIRECTORY_SEPARATOR . 'manifest.json.php';
$manifest = include $manifestFile;
foreach ($manifest as $entry) {
self::$versions[$entry['namespace']] = $entry['versions']['latest'];
}
if (!isset(self::$versions[$namespace])) {
throw new UnknownVersionException('Could not find version for ' . $class);
}
}
return self::$versions[$class];
return self::$versions[$namespace];
}


/**
* @param AwsClient $class
* @param string $class
* @return AwsClient
*/

public function factory($class = AwsClient::class)
Expand Down
45 changes: 45 additions & 0 deletions test/ValidateVersionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Magium\AwsFactory\Tests;

use Aws\Ec2\Ec2Client;
use Magium\AwsFactory\AwsFactory;
use PHPUnit\Framework\TestCase;

class ValidateVersionTest extends TestCase
{

public function testClientVersionExtractor()
{
$classesToIgnore = [
'Aws\MultiRegionClient'
];
$srcDir = __DIR__ . '/../vendor/aws/aws-sdk-php/src';
$Directory = new \RecursiveDirectoryIterator($srcDir);
$Iterator = new \RecursiveIteratorIterator($Directory);
$Regex = new \RegexIterator($Iterator, '/^.+Client\.php$/i', \RecursiveRegexIterator::GET_MATCH);
foreach ($Regex as $classFilename) {
$classFilename = array_shift($classFilename);
if (strpos($classFilename, 'AwsClient') !== false) {
continue; // We don't need the version for the base client class
}
$fileContents = file_get_contents($classFilename);
$matches = null;
preg_match('/namespace ([^;]+).*/s', $fileContents, $matches);
if (!$matches) continue;
$namespace = $matches[1];
$className = substr(basename($classFilename), 0, -4);
$class = $namespace . '\\' . $className;
if (in_array($class, $classesToIgnore)) continue;
$version = AwsFactory::getVersionFor($class);
self::assertNotNull($version, 'Failed to find version for ' . $class);
}
}

public function testEc2Client()
{
$version = AwsFactory::getVersionFor(Ec2Client::class);
self::assertNotNull($version);
}

}

0 comments on commit 92c0d35

Please sign in to comment.