rtek/aws-gen
Composer 安装命令:
composer require rtek/aws-gen
包简介
Generate PHP wrapper classes for AWS services, operations and types
关键字:
README 文档
README
AwsGen generates strictly typed PHP classes that allow you to use Amazon Web Services with objects instead of arrays.
Installation
\Rtek\AwsGen has no runtime component, so require it as a development dependency:
$ composer require --dev rtek/aws-gen
Why Bother?
AWS has approximately 170 services with ~18,000 types. The SDK provides access
to these services using \ArrayAccess and rich runtime metadata,
but does not offer code-completion by realizing the API in PHP classes*.
\Rtek\AwsGen will generate PHP classes for the services and operations that
you choose, while allowing you to use the underlying SDK at all times.
You can treat these classes as read-only, or embed them in your project as the basis for more sophisticated wrappers.
*If you used AwsGen for all services, there would be ~10x the number of files as the SDK
An Example: S3 Service
Console generation
$ 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 generation
<?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
Usage
<?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())); }
Terms
Service- An AWS service that has a
Clientand metadata - Contains multiple
OperationandShape - e.g.
S3,DynamoDb,Ec2
- An AWS service that has a
Operation- An AWS API call that does something
- Has an
Inputand anOutput - e.g.
DynamoDb\DynamoDbClient::putItem(),S3\S3Client::getObject()
Shape- An AWS type that can be serialized as an associative array
- Accessors are read and write
- e.g.
S3\ObjectList,Ec2\Instance
Input- A
Shapethat contains the input parameters for anOperation - Defines the expected
Outputfor anOperation - Accessors are write-only
- e.g.
DynamoDb\PutItemInput,S3\ListObjectRequest
- A
Output- A
Shapethat contains the result of anOperation - Extends
\Aws\Result - Accessors are read-only
- e.g.
DynamoDb\PutItemOutput,S3\ListObjectOutput
- A
Client- Extends the corresponding SDK client
- Marshals an
Inputto the SDKOperationand returns theOutput - e.g.
S3\S3Client,DynamoDb\DynamoDbClient
Issues / Quriks
- Paginators are not implemented
- CommandPools are not implemented
\Aws\Result::$datais passed by value toOutputclasses_will be appended the PHP class name when:- A service contains two types with identical case-insensitive names
- A type is a PHP keyword
- Some service names are oddly named vs the namespace: e.g.
streams.dynamodb => DynamoDbStreams - Some
Inputclasses use the termRequestinstead ofInputper the SDK metadata
Acknowledgements
- Inspired by goetas-webeservices/xsd2php
rtek/aws-gen 适用场景与选型建议
rtek/aws-gen 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 03 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「object」 「aws」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rtek/aws-gen 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rtek/aws-gen 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rtek/aws-gen 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
A set of useful PHP classes.
Runn Me! Value Objects Library
This package includes a script and fail2ban configuration that allows you to use fail2ban when utilizing AWS elastic load balancer (ELB) and an apache webserver.
Elastic Driver for Laravel Scout
Laravel SDK mapping data in object
统计信息
- 总下载量: 4
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 10
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-03-09