juststeveking/php-sdk
Composer 安装命令:
composer require juststeveking/php-sdk
包简介
A framework for building SDKs in PHP.
关键字:
README 文档
README
A framework for building SDKs in PHP.
Installation
composer require juststeveking/php-sdk
Purpose
The purpose of this package is to provide a consistent and interoperable way to build PHP SDKs to work with 3rd party APis.
Usage
To get started with this library, you need to start by extending the Client class. Let's walk through building an SDK.
Create your SDK class
use JustSteveKing\Sdk\Client; final class YourSDK extends Client { // }
Once this is in place, you can start adding your resources to the class. Let's add a projects method for a projects resource.
use JustSteveKing\Sdk\Client; use JustSteveKing\Sdk\Tests\Stubs\Resources\ProjectResource; final class YourSDK extends Client { public function projects() { return new ProjectResource( client: $this, ); } }
We return a new instance of our resource classes, passing through your SDK as a client. This is so that each resource is able to talk through the client to send requests.
Now, let's look at how to structure a resource.
final class ProjectResource { // }
To save time, there are a collection of traits that you can use on your resources.
CanAccessClient- which will add the default constructor required for a resource.CanCreateDataObjects- which will allow you to create DataObjects from API responses.CanCreateRequests- which will allow you to create HTTP requests and payloads using PSR-17 Factories.
Let's look at an example of a full resource class.
use Exception; use JustSteveKing\Sdk\Concerns\Resources; use JustSteveKing\Tools\Http\Enums\Method; use Ramsey\Collection\Collection; use Throwable; final class ProjectResource { use Resources\CanAccessClient; use Resources\CanCreateDataObjects; use Resources\CanCreateRequests; public function all(): Collection { $request = $this->request( method: Method::GET, uri: '/projects', ); try { $response = $this->client->send( request: $request, ); } catch (Throwable $exception) { throw new Exception( message: 'Failed to list test.', code: $exception->getCode(), previous: $exception, ); } return (new Collection( collectionType: Project::class, data: array_map( callback: static fn(array $data): Project => Project::make( data: $data, ), array: (array) json_decode( json: $response->getBody()->getContents(), associative: true, flags: JSON_THROW_ON_ERROR, ), ), )); } }
We start by creating a request, and then try to get a response by sending it through the client.
Once we have a response, we create a Collection thanks to a package by Ben Ramsey. We pass through the type of each item we expect it to be,
then the data as an array. To create the data we map over the response content and statically create a new Data Object.
This allows us to keep our code clean, concise, and testable.
Testing
To run the test:
composer run test
Static analysis
To run the static analysis checks:
composer run stan
Code Style
To run the code style fixer:
composer run pint
Refactoring
To run the rector code refactoring:
composer run refactor
Special Thanks
Without the following packages and people, this framework would have been a lot harder to build.
Credits
LICENSE
The MIT License (MIT). Please see License File for more information.
juststeveking/php-sdk 适用场景与选型建议
juststeveking/php-sdk 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 73.89k 次下载、GitHub Stars 达 217, 最近一次更新时间为 2020 年 10 月 29 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「sdk」 「psr-18」 「sdk-framework」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 juststeveking/php-sdk 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 juststeveking/php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 juststeveking/php-sdk 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Safely namespaced Guzzle for WP2Static
Symfony Opentracing bundle extension for Zipkin tracers
REST client for PHP 7.4+
Open Policy Agent client and PSR-7, PSR-15 Authorization Middleware
Common interface for HTTP clients
Manage http server and client message
统计信息
- 总下载量: 73.89k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 218
- 点击次数: 14
- 依赖项目数: 3
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2020-10-29