定制 everlution/simple-rest-api 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

everlution/simple-rest-api

Composer 安装命令:

composer require everlution/simple-rest-api

包简介

A pattern for REST APIs

关键字:

README 文档

README

This library provides all the tools for defining REST APIs and validate requests and responses.

Installation

composer require everlution/simple-rest-api

# if you are using the JsonSchemaValidator
composer require justinrainbow/json-schema

API Definition

In order to define a new API you need to implement the Everlution\SimpleRestApi\Api\ApiInterface.

If you are implementing an API that is going to be validated with JSON Schema you can implement the JsonSchemaApiInterface.

<?php

namespace App\Api\Sms;

use App\ApiBusinessLogic\Sms\SendApiBusinessLogic;
use App\JsonSchema\Api\Sms\Send\RequestJsonSchema;
use App\JsonSchema\Api\Sms\Send\ResponseJsonSchema;
use Everlution\SimpleRestApi\Api\AbstractApi;
use Everlution\SimpleRestApi\Api\JsonSchemaApiInterface;
use Everlution\SimpleRestApi\ApiRequestHandler\DefaultApiRequestHandler;
use Everlution\SimpleRestApi\ApiValidator\JsonSchemaApiValidator;
use Symfony\Component\HttpFoundation\Request;

class SendApi extends AbstractApi implements JsonSchemaApiInterface
{
    private $requestJsonSchema;

    private $responseJsonSchema;

    public function __construct(
        JsonSchemaApiValidator $apiValidator,
        DefaultApiRequestHandler $apiRequestHandler,
        SendApiBusinessLogic $apiBusinessLogic,
        RequestJsonSchema $requestJsonSchema,
        ResponseJsonSchema $responseJsonSchema
    ) {
        parent::__construct($apiValidator, $apiRequestHandler, $apiBusinessLogic);

        $this->requestJsonSchema = $requestJsonSchema;
        $this->responseJsonSchema = $responseJsonSchema;
    }

    public function getTitle(): string
    {
        return 'SMS Send';
    }

    public function isDeprecated(): bool
    {
        return false;
    }

    public static function getMethods(): array
    {
        return [Request::METHOD_POST];
    }

    public static function getRoutesPaths(): array
    {
        return [
            '/sms/send',
        ];
    }

    public function isEnabled(): bool
    {
        return true;
    }

    public function getStatusCodes(): array
    {
        return parent::getStatusCodes() + [401 => 'Unauthorized'];
    }

    public function getRequestJsonSchema(): string
    {
        return $this->requestJsonSchema->generate();
    }

    public function getResponseJsonSchema(): string
    {
        return $this->responseJsonSchema->generate();
    }
}

API Request Handler

This is the service that is in charge to do validation of the request and response and execute the business logic. The library provides a default handler Everlution\SimpleRestApi\ApiRequestHandler\DefaultApiRequestHandler.

API Validation

<?php

namespace App\JsonSchema\Api\Sms\Send;

class RequestJsonSchema
{
    public function toArray(): array
    {
        return [
            'type' => 'object',
            'properties' => [
                'number' => [
                    'type' => 'string',
                    'description' => 'The phone number',
                ],
                'text' => [
                    'type' => 'string',
                    'description' => 'The content of the message',
                    'maxLength' => 255
                ]
            ]
        ];
    }
    
    public function generate(): string
    {
        return json_encode($this->toArray());
    }
}

API Business Logic

This is the service that define what the API is suppose to do.

<?php

namespace App\ApiBusinessLogic\Sms;

use Everlution\SimpleRestApi\Api\ApiInterface;
use Everlution\SimpleRestApi\ApiBusinessLogic\ApiBusinessLogicInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class SendApiBusinessLogic implements ApiBusinessLogicInterface
{
    public function execute(ApiInterface $api, Request $request): Response
    {
        // Logic here
    }
}

Same thing for the Response JSON Schema

How to integrate with frameworks

Symfony 4

Add this file to config/routes.php

<?php

use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
use App\Api\Sms;

$apis = [
    Sms\SendApi::class,
    // ... other APIs
];

return function (RoutingConfigurator $routes) use ($apis) {
    foreach ($apis as $api) {
        $i = 0;
        foreach ($api::getRoutesPaths() as $routePath) {
            $pathName = sprintf('%s_%s', str_replace('\\', '_', $api), $i);
            $routes
                ->add($pathName, $routePath)
                ->controller([$api, 'sendResponse'])
                ->methods($api::getMethods());

            $i++;
        }
    }
};

And then you need to add this to config/services.yaml in order to make the framework recognize the API as a controller.

_instanceof:
    Everlution\SimpleRestApi\Api\ApiInterface:
    tags: ['controller.service_arguments']

everlution/simple-rest-api 适用场景与选型建议

everlution/simple-rest-api 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 64 次下载、GitHub Stars 达 0, 最近一次更新时间为 2019 年 05 月 26 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「rest」 「api」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 everlution/simple-rest-api 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 everlution/simple-rest-api 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 64
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 3
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: Unknown
  • 更新时间: 2019-05-26