kaz29 / cakephp-open-api-theme-plugin

CakePHP 的 OpenApiTheme 插件

安装次数: 12,570

依赖项: 0

建议者: 0

安全: 0

星标: 4

关注者: 2

分支: 2

开放问题: 1

语言:Twig

类型:cakephp-plugin

1.1.6 2024-05-15 20:51 UTC

This package is auto-updated.

Last update: 2024-09-15 21:30:27 UTC


README

安装

您可以使用 composer 将此插件安装到您的 CakePHP 应用程序中。

安装 composer 包的推荐方式是

composer require kaz29/cakephp-open-api-theme-plugin --dev

依赖

设置

  • swagger-settings.php 复制到配置目录。

  • swagger-settings.php 中描述您应用程序的设置。

  • "build:swagger" : "build-swagger-json" 添加到您的应用程序 composer.json 脚本部分。

    "scripts": {
        "post-install-cmd": "App\\Console\\Installer::postInstall",
        "post-create-project-cmd": "App\\Console\\Installer::postInstall",
        "check": [
            "@test",
            "@cs-check"
        ],
        "cs-check": "phpcs --colors -p  src/ tests/",
        "cs-fix": "phpcbf --colors -p src/ tests/",
        "stan": "phpstan analyse",
        "test": "phpunit --colors=always",
                "build:openapi": "openapi -b ./vendor/autoload.php --output /srv/docs/openapi.yml ./src"  // <-- add this line
    },
    
  • 将以下配置添加到 config/bootstrap_cli.php 的末尾。

Configure::write('Bake.theme', 'OpenApiTheme');
  • 按照以下方式更改 Application.php。
#[OA\Info(title: '**Your Application name**', version: '**Your application version**')]
#[OA\Server(description: '**Environment name**', url: '**URL**')]
#[OA\Schema(
    schema: 'ValidationErrorResponse',
    title: 'Validation error response',
    required: ['message', 'errors'],
    properties: [
        new OA\Property(
            property: 'message',
            type: 'string',
            description: 'Error message',
        ),
        new OA\Property(
            property: 'errors',
            type: 'object',
            description: 'Error details',
        ),
    ]
)]
class Application extends BaseApplication
...
    protected function bootstrapCli(): void
    {
        try {
            $this->addPlugin('Bake');
        } catch (MissingPluginException $e) {
            // Do not halt if the plugin is missing
        }

        $this->addPlugin('Migrations');

        // Load more plugins here
        $this->addPlugin('OpenApiTheme');  // Add this line
    }

使用示例

bake 控制器

$ bin/cake bake open_api_controller Articles --prefix Api

bake 模型

$ bin/cake bake open_api_model Articles

创建 swagger.json

$ composer build:swagger

烘焙示例

可以在 CakePHP CMS 教程中找到 示例目录中的烘焙示例。

作者

Kazuhiro Watanabe - cyo [at] mac.com - https://twitter.com/kaz_29

许可协议

CakePHP 的 OpenApiTheme 插件采用 MIT 许可协议 - 详细信息请参阅 LICENSE 文件