genxoft/swagger-php-module

为mezzio和laminas适配的Swagger-PHP模块。

v0.1.3-stable 2022-07-18 15:50 UTC

This package is auto-updated.

Last update: 2024-09-18 20:30:10 UTC


README

描述

为Mezzio/Laminas适配的Swagger PHP模块。

需求

  • PHP >7.4
  • Mezzio\Template\TemplateRendererInterface

安装

安装此包装器的首选方式是通过 composer

php composer.phar require genxoft/php-swagger-module

composer require genxoft/php-swagger-module

或在composer.json的require部分添加

"genxoft/php-swagger-module" : "*"

配置

安装包后,您需要完成以下步骤才能使用PHP Swagger模块:

  1. \Genxoft\SwaggerPhpModule\ConfigProvider::class添加到您的配置聚合器中
  2. 添加并自定义(如有必要)配置(将文件php-swagger.global.php添加到config/autoload中)
<?php
return [
    'swagger_php' => [
        'scanDirs' => [
            __DIR__ . '/module'
        ],
        'jsonUrl' => '/api-oas-docs/json',
    ],
    'routes' => [
        'swagger_php.route.json' => [
            'path' => '/api-oas-docs/json',
            'middleware' => [
                \Genxoft\SwaggerPhpModule\Handler\JsonAction::class,
            ],
            'allowed_methods' => ['GET'],
        ],
        'swagger_php.route.ui' => [
            'path' => '/api-oas-docs/ui',
            'middleware' => [
                \Genxoft\SwaggerPhpModule\Handler\UiAction::class,
            ],
            'allowed_methods' => ['GET'],
        ],
    ],
];

Open Api Swagger 3示例注解

API服务器描述

/**
 * @OA\Info(
 *   version="1.0",
 *   title="Application API",
 *   description="Server - Mobile app API",
 *   @OA\Contact(
 *     name="John Smith",
 *     email="john@example.com",
 *   ),
 * ),
 * @OA\Server(
 *   url="https://example.com/api",
 *   description="main server",
 * )
 * @OA\Server(
 *   url="https://dev.example.com/api",
 *   description="dev server",
 * )
 */
...

处理程序注解

/**
 * @OA\Get(path="/",
 *   summary="Handshake",
 *   tags={"handshake"},
 *   @OA\Parameter(
 *     name="access-token",
 *     in="header",
 *     required=false,
 *     @OA\Schema(
 *       type="string"
 *     )
 *   ),
 *   @OA\Response(
 *     response=200,
 *     description="Returns Hello object",
 *     @OA\MediaType(
 *         mediaType="application/json",
 *         @OA\Schema(ref="#/components/schemas/Hello"),
 *     ),
 *   ),
 * )
 */
class HelloHandler implements RequestHandlerInterface
{
...

模型注解

/**
 *@OA\Schema(
 *  schema="Hello",
 *  @OA\Property(
 *     property="message",
 *     type="string",
 *     description="Text message"
 *  ),
 *  @OA\Property(
 *     property="time",
 *     type="integer",
 *     description="Server current Unix time"
 *  ),
 *  @OA\Property(
 *     property="date",
 *     type="string",
 *     format="date-time",
 *     description="Server current date time"
 *  )
 *)
 */
class HelloModel
{
...

截图

Swagger UI Screenshot

捐赠

许可证

此curl包装器在MIT许可证下发布。