auto1-oss/service-api-handler-bundle

PHP微服务创建的Auto1服务API处理包

v1.1.3 2024-08-07 09:58 UTC

This package is auto-updated.

Last update: 2024-09-07 10:09:18 UTC


README

注册所需的包

    Auto1\ServiceAPIComponentsBundle\Auto1ServiceAPIComponentsBundle::class => ['all' => true],
    Auto1\ServiceAPIHandlerBundle\Auto1ServiceAPIHandlerBundle::class => ['all' => true],

config/routing.yml

endpoints:
    resource: "@Auto1ServiceAPIHandlerBundle/Resources/config/routing.yml"

描述

使用端点规范来处理symfony请求流程。

$_GLOBALS准备RequestDTO并从ServiceResponse(ResponseDTO, HTTP_CODE)序列化响应

控制器

  • 控制器必须带有controller.service_arguments标签并以Controller结尾
  • 动作方法必须以Action结尾

服务响应

  • 完全模拟但不实现HttpFoundation\Response(JsonResponse)的行为
  • 对响应格式不敏感,并在从控制器返回后执行序列化

端点定义示例(yaml)

# CarLead
getCarLeadByVin:
    method:        'GET'
    baseUrl:       '%auto1.api.url%'
    path:          '/v1/carlead/vin/{vin}'
    requestClass:  'Auto1\ServiceDTOCollection\CarLead\CarLeadRead\Request\GetCarLeadByVinRequest'
    responseClass: 'Auto1\ServiceDTOCollection\CarLead\CarLeadRead\Response\CarLead'

服务请求实现示例

class GetCarLeadByVinRequest implements ServiceRequestInterface
{
    private $vin;

    public function setVin(string $vin): self
    {
        $this->vin = $vin;

        return $this;
    }

    public function getVin()
    {
        return $this->vin;
    }
}

端点实现示例

use Auto1\ServiceAPIHandlerBundle\Response\ServiceResponse;
use Auto1\ServiceDTOCollection\CarLead\CarLeadRead\Request\GetCarLeadByVinRequest;
use Auto1\ServiceDTOCollection\CarLead\CarLeadRead\Response\CarLead;

class MyController {
   
    public function carLeadByVinAction(GetCarLeadByVinRequest $carLeadRequestDTO): ServiceResponse
    {
        /** @var CarLead $carLead */
        $carLead = $this->...->find($carLeadRequestDTO->getVin());
    
        return new ServiceResponse(
            $carLead,
            200
        );
    }
}

Swagger生成

对于symfony>=6.0nelmio/api-doc-bundle>=4.0,swagger json文件以OpenApi v3格式"openapi": "3.0.0"生成。对于symfonynelmio/api-doc-bundle的先前版本,swagger json文件以Swagger V2格式"swagger": "2.0"生成。

调试

bin/console c:c && bin/console debug:router --show-controllers

更多信息 - 请查看service-api-components-bundle使用