kaz29 / cakephp-open-api-theme-plugin
CakePHP 的 OpenApiTheme 插件
1.1.6
2024-05-15 20:51 UTC
Requires
- php: >=8.1
- cakephp/bake: ^3.1
- cakephp/cakephp: ^5.0.1
- zircote/swagger-php: ^4.9
Requires (Dev)
- phpunit/phpunit: ^10.1.0
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 文件