rtek/aws-gen

生成 AWS 服务、操作和类型的 PHP 封装类

0.1.0 2019-03-09 02:40 UTC

This package is auto-updated.

Last update: 2024-09-18 08:41:23 UTC


README

AwsGen 生成严格类型的 PHP 类,允许您使用对象而不是数组来使用 Amazon Web 服务。

安装

\Rtek\AwsGen 没有运行时组件,因此将其作为开发依赖项要求

$ composer require --dev rtek/aws-gen

为什么要这么做呢?

AWS 大约有 170 个服务,大约有 18,000 个类型。SDK 提供了对这些服务的访问,使用 \ArrayAccess 和丰富的运行时 元数据,但并没有通过在 PHP 类中实现 API 来提供代码补全*。

\Rtek\AwsGen 将为您选择的服务和操作生成 PHP 类,同时允许您始终使用底层 SDK。

您可以将其类视为只读的,或者将其嵌入到您的项目中作为更复杂封装的基础。

*如果您为所有服务使用 AwsGen,文件数量将是 SDK 的约 10 倍

示例:S3 服务

控制台生成

$ vendor/bin/aws-gen generate

 Search Service:
 > s3

 Choose Service [Search again]:
  [0] Stop searching
  [1] Search again
  [2] s3
  [3] s3:2006-03-01
  [4] s3control
  [5] s3control:2018-08-20
 > 2

 What namespace? [App\AwsGen\]:
 > App\AwsGen\

 What output directory? [src]:
 > src

 PSR-4 namespace prefix? [App\AwsGen\]:
  [0]
  [1] App\
  [2] App\AwsGen\
 > 1

Generating: s3
==============

 Added s3:latest
 Generating...
 ...Complete

 [OK] Wrote 294 files to src/AwsGen/

PHP 生成

<?php

namespace App;

use Rtek\AwsGen\Generator;
use Rtek\AwsGen\Writer\DirWriter;

$gen = new Generator('App\\AwsGen');  //generate classes to the 'App\AwsGen' namespace
$gen->addService('s3', '2006-03-01'); //add the s3 service, version optional
DirWriter::create('src')              //set the root directory to write the files
    ->setPsr4Prefix('App\\')          //optionally set a PSR4 prefix
    ->write($gen);                    //writes App\AwsGen\S3 to src/AwsGen/S3

用法

<?php 

namespace App;

use App\AwsGen\S3 as S3;

$config = [
    'credentials' => [
        'key' => '***',
        'secret' => '***',
    ],
    'region' => 'us-east-1', 
];
//generated client extends `\Aws\S3Client` with the same config as SDK except
//for `version` which is overridden by the specified generation version
$client = new S3\S3Client($config); 
                                    
//the operation input create(...) contains required params
$input = S3\CreateBucketRequest::create($bucket = 'test'); 

//they can be set directly just like optional params
$input->Bucket($bucket)->ACL('public-read');               
              
//operation names are the same as SDK   
$output = $client->createBucket($input);

//the operation output has getters that match the SDK 
echo "Bucket created at: {$output->Location()}\n"; 

//supports fluent interface
$output = $client->putObject(
    S3\PutObjectRequest::create($bucket, $key = 'foo.txt')
        ->Body('bar baz')->ContentType('text/plain')
);   

//`\ArrayAccess` works as usual since output classes extend `\Aws\Result`
echo "Created object {$key} with ETag {$output['ETag']}\n"; 

//supports setting values via array by constructor
$input = new S3\GetObjectRequest([
    'Bucket' => $bucket,
    'Key' => $key,
]);
$output = $client->getObject($input);
echo "The object has a body of: {$output->Body()}\n";

//you can bypass AwsGen classes by passing the array argument to the client
$result = $client->getObject([
    'Bucket' => $bucket,
    'Key' => $key,
]);
echo "The object still has a body of: {$result['Body']}\n";

//`\IteratorAggregate` is implemented for iterable properties
$output = $client->listObjectsV2(S3\ListObjectsV2Request::create($bucket));
foreach ($output->Contents() as $object) {
    $client->deleteObject(S3\DeleteObjectRequest::create($bucket, $object->getKey()));
}

术语

  • 服务
    • 一个具有 Client 和元数据的 AWS 服务
    • 包含多个 OperationShape
    • 例如 S3DynamoDbEc2
  • 操作
    • 一个执行某些操作的 AWS API 调用
    • 具有 InputOutput
    • 例如 DynamoDb\DynamoDbClient::putItem()S3\S3Client::getObject()
  • 形状
    • 一个可以作为关联数组序列化的 AWS 类型
    • 访问器是读写
    • 例如 S3\ObjectListEc2\Instance
  • 输入
    • 包含 Operation 输入参数的 Shape
    • 定义 Operation 的预期 Output
    • 访问器是只写的
    • 例如 DynamoDb\PutItemInputS3\ListObjectRequest
  • 输出
    • 包含 Operation 结果的 Shape
    • 扩展 \Aws\Result
    • 访问器是只读的
    • 例如 DynamoDb\PutItemOutputS3\ListObjectOutput
  • 客户端
    • 扩展相应的 SDK 客户端
    • Input 封装到 SDK Operation 并返回 Output
    • 例如 S3\S3ClientDynamoDb\DynamoDbClient

问题 / 疑问

  • 分页器未实现
  • 命令池未实现
  • \Aws\Result::$data 通过值传递给 Output
    • 服务包含两个具有相同的大小写不敏感名称的类型时
    • 类型是 PHP 关键字
  • 某些服务名称与命名空间不匹配:例如 streams.dynamodb => DynamoDbStreams
  • 某些 Input 类使用 SDK 元数据中的 Request 而不是 Input 术语

致谢