auto1-oss / service-api-handler-bundle
PHP微服务创建的Auto1服务API处理包
v1.1.3
2024-08-07 09:58 UTC
Requires
- php: >=7.1
- auto1-oss/service-api-components-bundle: ^1.0
- auto1-oss/service-api-request: ^1.0
- monolog/monolog: ~1.22
- symfony/config: ~3.0|~4.0|~5.0|~6.0
- symfony/dependency-injection: ~3.0|~4.0|~5.0|~6.0
- symfony/http-foundation: ~3.0|~4.0|~5.0|~6.0
- symfony/http-kernel: ~3.0|~4.0|~5.0|~6.0
- symfony/monolog-bridge: ~3.0|~4.0|~5.0|~6.0
- symfony/property-info: ^3.4.2|~4.0|~5.0|~6.0
- symfony/routing: ~4.1|~5.0|~6.0
- symfony/serializer: ~3.0|~4.0|~5.0|~6.0
- symfony/yaml: ~3.0|~4.0|~5.0|~6.0
Requires (Dev)
- phpspec/prophecy: ^1.7.2
- phpunit/phpunit: ^7.5|^8.0
- symfony/console: ~3.0|~4.0|~5.0|~6.0
Suggests
- nelmio/api-doc-bundle: For Generating API documentations
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.0
和nelmio/api-doc-bundle>=4.0
,swagger json文件以OpenApi v3格式"openapi": "3.0.0"
生成。对于symfony
和nelmio/api-doc-bundle
的先前版本,swagger json文件以Swagger V2格式"swagger": "2.0"
生成。
调试
bin/console c:c && bin/console debug:router --show-controllers
更多信息 - 请查看service-api-components-bundle使用