juststeveking/php-sdk 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

juststeveking/php-sdk

Composer 安装命令:

composer require juststeveking/php-sdk

包简介

A framework for building SDKs in PHP.

README 文档

README

Latest Version PHP Version tests Total Downloads

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 juststeveking/php-sdk 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 73.89k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 218
  • 点击次数: 14
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 217
  • Watchers: 7
  • Forks: 14
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-29