Skip to content

Commit 92c0d35

Browse files
committed
Updated the factory to read the version from the manifest file
1 parent 28c265c commit 92c0d35

File tree

3 files changed

+79
-66
lines changed

3 files changed

+79
-66
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,8 @@
2020
"require": {
2121
"aws/aws-sdk-php": "^3.33",
2222
"magium/configuration-manager": "^1"
23+
},
24+
"require-dev": {
25+
"phpunit/phpunit": "^6.4"
2326
}
2427
}

lib/AwsFactory.php

Lines changed: 31 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,7 @@
22

33
namespace Magium\AwsFactory;
44

5-
use Aws\ApiGateway\ApiGatewayClient;
6-
use Aws\AutoScaling\AutoScalingClient;
75
use Aws\AwsClient;
8-
use Aws\CloudDirectory\CloudDirectoryClient;
9-
use Aws\CloudFormation\CloudFormationClient;
10-
use Aws\CloudFront\CloudFrontClient;
11-
use Aws\CloudSearch\CloudSearchClient;
12-
use Aws\CloudWatch\CloudWatchClient;
13-
use Aws\CodeDeploy\CodeDeployClient;
14-
use Aws\CognitoIdentity\CognitoIdentityClient;
15-
use Aws\CognitoIdentityProvider\CognitoIdentityProviderClient;
16-
use Aws\DirectoryService\DirectoryServiceClient;
17-
use Aws\DynamoDb\DynamoDbClient;
18-
use Aws\Ec2\Ec2Client;
19-
use Aws\Ecr\EcrClient;
20-
use Aws\Ecs\EcsClient;
21-
use Aws\Efs\EfsClient;
22-
use Aws\ElastiCache\ElastiCacheClient;
23-
use Aws\ElasticLoadBalancing\ElasticLoadBalancingClient;
24-
use Aws\ElasticLoadBalancingV2\ElasticLoadBalancingV2Client;
25-
use Aws\ElasticsearchService\ElasticsearchServiceClient;
26-
use Aws\Iam\IamClient;
27-
use Aws\Rds\RdsClient;
28-
use Aws\Route53\Route53Client;
29-
use Aws\S3\S3Client;
30-
use Aws\Ses\SesClient;
31-
use Aws\Sms\SmsClient;
32-
use Aws\Sns\SnsClient;
33-
use Aws\Sqs\SqsClient;
34-
use Aws\Sts\StsClient;
356
use Magium\Configuration\Config\Repository\ConfigInterface;
367

378
class AwsFactory
@@ -47,37 +18,7 @@ class AwsFactory
4718
* @var array
4819
*/
4920

50-
private static $versions = [
51-
Ec2Client::class => '2016-11-15',
52-
ApiGatewayClient::class => '2015-07-09',
53-
AutoScalingClient::class => '2011-01-01',
54-
CloudDirectoryClient::class => '2016-05-10',
55-
CloudFormationClient::class => '2010-05-15',
56-
CloudFrontClient::class => '2017-03-25',
57-
CloudSearchClient::class => '2013-01-01',
58-
CloudWatchClient::class => '2010-08-01',
59-
CodeDeployClient::class => '2014-10-06',
60-
CognitoIdentityClient::class => '2014-06-30',
61-
CognitoIdentityProviderClient::class => '2016-04-18',
62-
DirectoryServiceClient::class => '2015-04-16',
63-
DynamoDbClient::class => '2012-08-10',
64-
EcsClient::class => '2014-11-13',
65-
EcrClient::class => '2015-09-21',
66-
EfsClient::class => '2015-02-01',
67-
ElastiCacheClient::class => '2015-02-02',
68-
ElasticLoadBalancingClient::class => '2012-06-01',
69-
ElasticLoadBalancingV2Client::class => '2015-12-01',
70-
ElasticsearchServiceClient::class => '2015-01-01',
71-
IamClient::class => '2010-05-08',
72-
RdsClient::class => '2014-10-31',
73-
Route53Client::class => '2013-04-01',
74-
S3Client::class => '2006-03-01',
75-
SesClient::class => '2010-12-01',
76-
SmsClient::class => '2016-10-24',
77-
SnsClient::class => '2010-03-31',
78-
SqsClient::class => '2012-11-05',
79-
StsClient::class => '2011-06-15'
80-
];
21+
private static $versions = [];
8122

8223
private $config;
8324
private static $self;
@@ -88,22 +29,46 @@ public function __construct(ConfigInterface $config)
8829
self::$self = $this;
8930
}
9031

91-
public static function setVersion($class, $version)
32+
private static function getDataDir($class)
9233
{
93-
self::$versions[$class] = $version;
34+
// We use the class to navigate our way to the data directory
35+
$classParts = explode('\\', $class);
36+
array_pop($classParts);
37+
$reflection = new \ReflectionClass($class);
38+
$dataDirName = dirname($reflection->getFileName());
39+
$parts = explode(DIRECTORY_SEPARATOR, $dataDirName);
40+
do {
41+
$cwd = array_pop($parts);
42+
} while (count($parts) > 0 && $cwd != 'aws-sdk-php');
43+
44+
return implode(DIRECTORY_SEPARATOR, $parts)
45+
. DIRECTORY_SEPARATOR
46+
. implode(DIRECTORY_SEPARATOR , ['aws-sdk-php', 'src', 'data']);
9447
}
9548

9649
public static function getVersionFor($class)
9750
{
98-
if (!isset(self::$versions[$class])) {
99-
throw new UnknownVersionException('Could not find version for ' . $class);
51+
$namespace = explode('\\', $class);
52+
array_pop($namespace); // Get rid of the class name
53+
$namespace = array_pop($namespace); // Get the highest level namespace
54+
55+
if (!isset(self::$versions[$namespace])) {
56+
$manifestFile = self::getDataDir($class) . DIRECTORY_SEPARATOR . 'manifest.json.php';
57+
$manifest = include $manifestFile;
58+
foreach ($manifest as $entry) {
59+
self::$versions[$entry['namespace']] = $entry['versions']['latest'];
60+
}
61+
if (!isset(self::$versions[$namespace])) {
62+
throw new UnknownVersionException('Could not find version for ' . $class);
63+
}
10064
}
101-
return self::$versions[$class];
65+
return self::$versions[$namespace];
10266
}
10367

10468

10569
/**
106-
* @param AwsClient $class
70+
* @param string $class
71+
* @return AwsClient
10772
*/
10873

10974
public function factory($class = AwsClient::class)

test/ValidateVersionTest.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace Magium\AwsFactory\Tests;
4+
5+
use Aws\Ec2\Ec2Client;
6+
use Magium\AwsFactory\AwsFactory;
7+
use PHPUnit\Framework\TestCase;
8+
9+
class ValidateVersionTest extends TestCase
10+
{
11+
12+
public function testClientVersionExtractor()
13+
{
14+
$classesToIgnore = [
15+
'Aws\MultiRegionClient'
16+
];
17+
$srcDir = __DIR__ . '/../vendor/aws/aws-sdk-php/src';
18+
$Directory = new \RecursiveDirectoryIterator($srcDir);
19+
$Iterator = new \RecursiveIteratorIterator($Directory);
20+
$Regex = new \RegexIterator($Iterator, '/^.+Client\.php$/i', \RecursiveRegexIterator::GET_MATCH);
21+
foreach ($Regex as $classFilename) {
22+
$classFilename = array_shift($classFilename);
23+
if (strpos($classFilename, 'AwsClient') !== false) {
24+
continue; // We don't need the version for the base client class
25+
}
26+
$fileContents = file_get_contents($classFilename);
27+
$matches = null;
28+
preg_match('/namespace ([^;]+).*/s', $fileContents, $matches);
29+
if (!$matches) continue;
30+
$namespace = $matches[1];
31+
$className = substr(basename($classFilename), 0, -4);
32+
$class = $namespace . '\\' . $className;
33+
if (in_array($class, $classesToIgnore)) continue;
34+
$version = AwsFactory::getVersionFor($class);
35+
self::assertNotNull($version, 'Failed to find version for ' . $class);
36+
}
37+
}
38+
39+
public function testEc2Client()
40+
{
41+
$version = AwsFactory::getVersionFor(Ec2Client::class);
42+
self::assertNotNull($version);
43+
}
44+
45+
}

0 commit comments

Comments
 (0)