amsify42/php-swagger-postman 问题修复 & 功能扩展

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

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

amsify42/php-swagger-postman

Composer 安装命令:

composer require amsify42/php-swagger-postman

包简介

PHP package for generating swagger json through its annotation and generate postman collection from it

README 文档

README

composer require amsify42/php-swagger-postman

For generating enpoint specific Attribute, you can call this method

$attribute = new \Amsify42\PhpSwaggerPostman\Swagger\Attribute();
echo $attribute->generate();

You can also pass parameter rules and route params also with key value pair to get it added to the attribute

echo $attribute->generate(
  ['name' => 'required', 'address_id' => 'reqiured|integer'],
  ['userId' => 42]
);

For generating response data in attibute

$attribute->setSuccessData(['id' => 42, 'name' => 'SomeUser']);
echo $attribute->generate();

You can also pass callback for checking rules and decide the param needs to have TinyInt or Enum Values or Custom Property Attribute

$attribute->checkRules(
                function ($name, $rule) {
                    if ($rule == 'some_custom_rule') {
                        return [
                            'property' => ''// Pass some custom property attribute syntax or empty string for skipping
                        ];
                    } else if ($rule == 'enum') {
                        return  [
                            'enum' => ['paid', 'unpaid']
                        ];
                    } else if ($rule == 'boolean') {
                        return  [
                            'tinyint' => true
                        ];
                    }
                    return NULL;
                }
            );

By default NULL needs to be return if array of tinyint/enum/property is not return and if you want to generate property for a param which might be having simple or nested array

$attribute->checkRules(
                function ($name, $rule) use($attribute) {
                    if ($rule == 'array') {
                        return [
                            'property' => $attribute->createObjectOrArrayProperty(
                                $_POST[$name], // or $_GET[$name] or value from body data
                                $name
                              )
                        ];
                    }
                    return NULL;
                }
            );

For adding security

$attribute->setSecurity('XApiKey'); // The XApiKey will be from the security attribute already added

Example security attribute already added

#[OA\SecurityScheme(
    securityScheme: "XApiKey",
    type: "apiKey",
    in: "header",
    name: "X-Api-Key"
)]

For adding header with static value

$attribute->setHeader(['Auth-Key' => 'some_key']);

For adding response which already defined

$attribute->setResponse([
                [
                    'code' => 422,
                    'ref' => '#/components/responses/Validation' // Validation here is the name of response which is already defined somewhere
                ]
            ]);

Example Validation already defined

#[OA\Response(
    response: "Validation",
    description: "Validation Errors",
    content: new OA\JsonContent(
        properties: [
            new OA\Property(
                property: "message",
                example: "Please check the inputs provided"
            ),
            new OA\Property(
                property: "errors",
                type: "object"
            )
        ]
    )
)]

For generating Annotation instead of Attribute

$annotation = new \Amsify42\PhpSwaggerPostman\Swagger\Annotation();
echo $annotation->generate();

Notes:

  1. You can use all the method with Annotation which is mentioned above for Attribute but Attribute requires minimum php 8.2 version.
  2. For using Annotation with latest swagger-php installed, you may have to separately install doctrine/annotations
composer require doctrine/annotations

Refer to this link for more details https://github.com/zircote/swagger-php/blob/master/docs/guide/migrating-to-v4.md

Scanning the directory and generating swagger json for API documentation and postman collection and postman environment json.

$swagger = new \Amsify42\PhpSwaggerPostman\Swagger;
$swagger->getGeneratedJson(
    "path/to/scan-directory",
    "path/to/export-swagger-and-postman-json/"
);

Notes:

  1. Make sure to have OA/Info and at least one API endpoint already added before running $swagger->getGeneratedJson() method.
  2. For detecting source classes properly while scanning the directory, Either set autoload in composer.json or use spl_autoload_register() method for customize autoloading of the classes.
use OpenApi\Attributes as OA;

#[OA\Info(title: "My API", version: "1.0.0")]

#[OA\Get(path:"/api/users")]
#[OA\Response(response:"200", description:"An example endpoint")]

Annotation Example

/**
 * @OA\Info(
 *   version="1.0.0",
 *   title="My API"
 * )
 */

/**
 * @OA\Get(
 *     path="/api/users",
 *     @OA\Response(response="200", description="An example endpoint")
 * )
 */

If you want postman environment to have baseURL variable value, you can either set like this

$swagger = new \Amsify42\PhpSwaggerPostman\Swagger('http://www.site.com');
$swagger->getGeneratedJson(
    "path/to/scan-directory",
    "path/to/export-swagger-and-postman-json/"
);

or define at least one server in Attribute

#[OA\Server(
    url: 'http://www.site.com',
    description: 'some description about site'
)]

or in Annotation

/**
 * @OA\Server(
 *     url="http://www.site.com",
 *     description="some description about site"
 * )
 */

for adding environment suffix in postman environment file name, you can pass it in second parameter of Swagger Constructor

$swagger = new \Amsify42\PhpSwaggerPostman\Swagger('http://www.site.com', 'Local');

amsify42/php-swagger-postman 适用场景与选型建议

amsify42/php-swagger-postman 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 18 次下载、GitHub Stars 达 1, 最近一次更新时间为 2024 年 07 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 amsify42/php-swagger-postman 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-07-23