Lzh on GitHub

生成 OpenAPI 文档

./vendor/bin/openapi

swagger-php 包含一个命令行工具 ./vendor/bin/openapi。该工具可用于生成 OpenAPI 文档。

./vendor/bin/openapi app -o openapi.yaml
输出格式
默认的输出格式是 YAML。如果提供了文件名(通过 --output-o),该工具将根据文件扩展名来确定格式。
可以使用 --format 选项强制指定一种格式。
引导文件 (Bootstrap)
引导文件选项 -b 在你尝试在没有适当自动加载的情况下使用 swagger-php 时非常有用。
例如,你可能想用一个只有几个注解的单一文件来评估这个库。在这种情况下,告诉 swagger-php 在处理该文件之前对其进行引导(预加载),可以确保 PHP 的反射代码能够检查你的代码。
./vendor/bin/openapi -b my_file.php my_file.php

要查看所有可用选项的列表,请使用 -h 选项。

./vendor/bin/openapi -h

Usage: openapi [--option value] [/path/to/project ...]

Options:
  --config (-c)     Generator config
                    ex: -c operationId.hash=false
  --output (-o)     Path to store the generated documentation.
                    ex: --output openapi.yaml
  --exclude (-e)    Exclude path(s).
                    ex: --exclude vendor,library/Zend
  --pattern (-n)    Pattern of files to scan.
                    ex: --pattern "*.php" or --pattern "/\.(phps|php)$/"
  --bootstrap (-b)  Bootstrap a php file for defining constants, etc.
                    ex: --bootstrap config/constants.php
  --processor (-p)  Register an additional processor.
  --format (-f)     Force yaml or json.
  --debug (-d)      Show additional error information.
  --version         The OpenAPI version; defaults to 3.0.0.
  --help (-h)       Display this help message.

使用 PHP

根据你的用例,也可以使用 PHP 代码以更动态的方式生成 OpenAPI 文档。

最简单的形式可能如下所示:

<?php
require("vendor/autoload.php");

$openapi = \OpenApi\Generator::scan(['/path/to/project']);

header('Content-Type: application/x-yaml');
echo $openapi->toYaml();
编程 API
有关 swagger-php API 的详细信息,请参见 参考