forked from aws/aws-sdk-php
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request aws#837 from jeskew/feature/generate-shared-examples
Feature/generate shared examples
- Loading branch information
Showing
9 changed files
with
312 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<?php | ||
namespace Aws\Build\Docs; | ||
|
||
use Aws\Api\ListShape; | ||
use Aws\Api\MapShape; | ||
use Aws\Api\Service; | ||
use Aws\Api\Shape; | ||
use Aws\Api\StructureShape; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class CodeSnippetGenerator | ||
{ | ||
/** @var Service */ | ||
private $service; | ||
|
||
public function __construct(Service $service) | ||
{ | ||
$this->service = $service; | ||
} | ||
|
||
public function __invoke($operation, $params, $comments, $isInput = true) | ||
{ | ||
$messageShape = $isInput | ||
? $this->service->getOperation($operation)->getInput() | ||
: $this->service->getOperation($operation)->getOutput(); | ||
$code = $this->visit($messageShape, $params, '', [], $comments); | ||
|
||
return $isInput | ||
? "\$result = \$client->" . lcfirst($operation) . "($code);" | ||
: $code; | ||
} | ||
|
||
private function visit(Shape $shape, $value, $indent, $path, $comments) | ||
{ | ||
switch ($shape['type']) { | ||
case 'structure': | ||
return $this->structure($shape, $value, $indent, $path, $comments); | ||
case 'list': | ||
return $this->arr($shape, $value, $indent, $path, $comments); | ||
case 'map': | ||
return $this->map($shape, $value, $indent, $path, $comments); | ||
case 'string': | ||
return "'{$value}'"; | ||
case 'timestamp': | ||
return "<DateTimeInterface>"; | ||
case 'blob': | ||
return "<binary string>"; | ||
default: | ||
return $value; | ||
} | ||
} | ||
|
||
private function structure(StructureShape $shape, $value, $indent, $path, $comments) | ||
{ | ||
$lines = ['[']; | ||
foreach ($value as $key => $val) { | ||
$path[] = ".{$key}"; | ||
$comment = $this->getCommentFor($path, $comments); | ||
$shapeVal = $this->visit($shape->getMember($key), $val, "{$indent} ", $path, $comments); | ||
$lines[] = rtrim("{$indent} '{$key}' => {$shapeVal}, {$comment}"); | ||
array_pop($path); | ||
} | ||
$lines[] = "{$indent}]"; | ||
|
||
return implode("\n", $lines); | ||
} | ||
|
||
private function arr(ListShape $shape, $value, $indent, $path, $comments) | ||
{ | ||
$lines = ['[']; | ||
foreach ($value as $ind => $val) { | ||
$path[] = "[{ind}]"; | ||
$comment = $this->getCommentFor($path, $comments); | ||
$shapeVal = $this->visit($shape->getMember(), $val, "{$indent} ", $path, $comments); | ||
$lines[] = rtrim("{$indent} {$shapeVal}, {$comment}"); | ||
array_pop($path); | ||
} | ||
$lines[] = "{$indent}]"; | ||
|
||
return implode("\n", $lines); | ||
} | ||
|
||
private function map(MapShape $shape, $value, $indent, $path, $comments) | ||
{ | ||
$lines = ['[']; | ||
foreach ($value as $key => $val) { | ||
$path[] = ".{$key}"; | ||
$comment = $this->getCommentFor($path, $comments); | ||
$shapeVal = $this->visit($shape->getValue(), $val, "{$indent} ", $path, $comments); | ||
$lines[] = rtrim("{$indent} '{$key}' => {$shapeVal}, {$comment}"); | ||
array_pop($path); | ||
} | ||
$lines[] = "{$indent}]"; | ||
|
||
return implode("\n", $lines); | ||
} | ||
|
||
private function getCommentFor($path, $comments) | ||
{ | ||
$key = preg_replace('/^\./', '', implode('', $path)); | ||
if (isset($comments[$key])) { | ||
return '// ' . $comments[$key]; | ||
} else { | ||
return ''; | ||
} | ||
} | ||
|
||
private function isAssociative(array $arr) | ||
{ | ||
return $arr = array_values($arr); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
<?php | ||
namespace Aws\Test\Build\Docs; | ||
|
||
|
||
use Aws\Api\ApiProvider; | ||
use Aws\Api\Service; | ||
use Aws\Build\Docs\CodeSnippetGenerator; | ||
|
||
class CodeSnippetGeneratorTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider exampleProvider | ||
* | ||
* @param Service $service | ||
* @param string $operation | ||
* @param array $input | ||
* @param string $expected | ||
* @param bool $isInput | ||
*/ | ||
public function testCanBuildCodeExamples( | ||
Service $service, | ||
$operation, | ||
array $input, | ||
$expected, | ||
$isInput = true | ||
) { | ||
$builder = new CodeSnippetGenerator($service); | ||
$this->assertSame($expected, $builder($operation, $input, [], $isInput)); | ||
} | ||
|
||
public function exampleProvider() | ||
{ | ||
$provider = ApiProvider::defaultProvider(); | ||
return [ | ||
// strings in input | ||
[ | ||
new Service($provider->resolve($provider, 'api', 'sqs', 'latest'), $provider), | ||
'GetQueueUrl', | ||
[ | ||
'QueueName' => 'MyQueue', | ||
'QueueOwnerAWSAccountId' => '12345678910', | ||
], | ||
<<<'EOCS' | ||
$result = $client->getQueueUrl([ | ||
'QueueName' => 'MyQueue', | ||
'QueueOwnerAWSAccountId' => '12345678910', | ||
]); | ||
EOCS | ||
], | ||
// strings in output | ||
[ | ||
new Service($provider->resolve($provider, 'api', 'sqs', 'latest'), $provider), | ||
'GetQueueUrl', | ||
["QueueUrl" => "https://queue.amazonaws.com/123456789101112/MyQueue"], | ||
<<<'EOCS' | ||
[ | ||
'QueueUrl' => 'https://queue.amazonaws.com/123456789101112/MyQueue', | ||
] | ||
EOCS | ||
, false, | ||
], | ||
// Dates and numbers in input | ||
[ | ||
new Service($provider->resolve($provider, 'api', 'autoscaling', 'latest'), $provider), | ||
'DescribeScheduledActions', | ||
[ | ||
'EndTime' => 1449799223, | ||
'MaxRecords' => 10, | ||
'StartTime' => 1449798223, | ||
], | ||
<<<'EOCS' | ||
$result = $client->describeScheduledActions([ | ||
'EndTime' => <DateTimeInterface>, | ||
'MaxRecords' => 10, | ||
'StartTime' => <DateTimeInterface>, | ||
]); | ||
EOCS | ||
], | ||
// Dates, numbers, and lists in output | ||
[ | ||
new Service($provider->resolve($provider, 'api', 'autoscaling', 'latest'), $provider), | ||
'DescribeScheduledActions', | ||
[ | ||
'NextToken' => 'Next', | ||
'ScheduledUpdateGroupActions' => [ | ||
[ | ||
'AutoScalingGroupName' => 'my-autoscaling-group', | ||
'DesiredCapacity' => 4, | ||
'EndTime' => 1449799223, | ||
'MaxSize' => 5, | ||
'MinSize' => 3, | ||
'Recurrence' => 'daily', | ||
'ScheduledActionARN' => 'arn:aws:iam::123456789012:user/David', | ||
'ScheduledActionName' => 'David', | ||
'StartTime' => 1449798223, | ||
'Time' => 1449798223, | ||
], | ||
], | ||
], | ||
<<<'EOCS' | ||
[ | ||
'NextToken' => 'Next', | ||
'ScheduledUpdateGroupActions' => [ | ||
[ | ||
'AutoScalingGroupName' => 'my-autoscaling-group', | ||
'DesiredCapacity' => 4, | ||
'EndTime' => <DateTimeInterface>, | ||
'MaxSize' => 5, | ||
'MinSize' => 3, | ||
'Recurrence' => 'daily', | ||
'ScheduledActionARN' => 'arn:aws:iam::123456789012:user/David', | ||
'ScheduledActionName' => 'David', | ||
'StartTime' => <DateTimeInterface>, | ||
'Time' => <DateTimeInterface>, | ||
], | ||
], | ||
] | ||
EOCS | ||
, false, | ||
], | ||
// Blobs and maps in input | ||
[ | ||
new Service($provider->resolve($provider, 'api', 'dynamodb', 'latest'), $provider), | ||
'PutItem', | ||
[ | ||
'Item' => [ | ||
'Key' => [ | ||
'B' => "3q2+7w==", | ||
], | ||
], | ||
], | ||
<<<'EOCS' | ||
$result = $client->putItem([ | ||
'Item' => [ | ||
'Key' => [ | ||
'B' => <binary string>, | ||
], | ||
], | ||
]); | ||
EOCS | ||
] | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
namespace Aws\Tests; | ||
namespace Aws\Test; | ||
|
||
use Aws\Command; | ||
use Aws\HandlerList; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
namespace Aws\Tests\S3; | ||
namespace Aws\Test\S3; | ||
|
||
use Aws\Middleware; | ||
use Aws\Result; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
<?php | ||
namespace Aws\Tests\S3; | ||
namespace Aws\Test\S3; | ||
|
||
use Aws\CommandInterface; | ||
use Aws\Result; | ||
|
Oops, something went wrong.