zircote/swagger-php
Composer 安装命令:
composer require zircote/swagger-php
包简介
Generate interactive documentation for your RESTful API using PHP attributes (preferred) or PHPDoc annotations
关键字:
README 文档
README
swagger-php
Generate interactive OpenAPI documentation for your RESTful API
using PHP attributes (preferred) or
doctrine annotations (requires additional
doctrine/annotations library).
See the documentation website for supported attributes and annotations.
Annotations are deprecated and may be removed in a future release of swagger-php.
Features
- Compatible with the OpenAPI 3.0, 3.1 and 3.2 specification.
- Extracts information from code and existing phpdoc comments.
- Can be used programmatically or via command-line tool.
- Documentation site with a getting started guide.
- Error reporting (with hints, context).
- All metadata is configured via PHP attributes.
OpenAPI version support
swagger-php allows to generate specs either for OpenAPI 3.0.0, OpenAPI 3.1.0 and OpenAPI 3.2.0.
By default, the spec will be in version 3.0.0. The command line option --version may be used to change to
any other supported version.
Programmatically, the method Generator::setVersion() can be used to change the version.
Requirements
swagger-php requires at least PHP 8.2.
Installation (with Composer)
composer require zircote/swagger-php
For cli usage from anywhere, install swagger-php globally and make sure to place the ~/.composer/vendor/bin directory
in your PATH so the openapi executable can be located by your system.
composer global require zircote/swagger-php
doctrine/annotations
As of version 4.8 the doctrine annotations library is
optional and no longer installed by default.
If your code uses doctrine annotations you will need to install that library manually:
composer require doctrine/annotations
Usage
Use OpenAPI attributes to add metadata to your classes, methods and other structural PHP elements.
use OpenApi\Attributes as OAT; #[OAT\Info(title: 'My First API', version: '0.1')] class MyApi { #[OAT\Get(path: '/api/resource.json')] #[OAT\Response(response: '200', description: 'An example resource')] public function getResource() { // ... } }
Visit the Documentation website for the Getting started guide or look at the examples directory for more examples.
Usage from PHP
Generate always-up-to-date documentation.
<?php require("vendor/autoload.php"); $openapi = (new \OpenApi\Generator())->generate(['/path/to/project']); header('Content-Type: application/x-yaml'); echo $openapi->toYaml();
Documentation of how to use the Generator class can be found in
the Generator reference.
Usage from the Command Line Interface
The openapi command line interface can be used to generate the documentation to a static yaml/json file.
./vendor/bin/openapi --help
Automatic type resolution
As of version 6, resolving of types is done using the TypeInfoTypeResolver class. It uses the symfony/type-info
library under the hood which allows handling of complext types.
With this change, swagger-php supports all available native type-hints and also complext generic type-hints via phpdoc
blocks.
This simplifies the definition of schemas.
For example, the following two examples are now equivalent:
class MyClass { #[OAT\Property(items: new OAT\Items(oneOf: [ new OAT\Schema(type: SchemaOne::class), new OAT\Schema(type: SchemaTwo::class), ]))] public array $values; }
class MyClass { /** * @var list<SchemaOne|SchemaTwo> */ public array $values; }
If this is not desired, the LegacyTypeResolver can be used to preserve the old behaviour of version 5.
The LegacyTypeResolver is deprecated and will be removed in a future release.
Contributing
More on OpenApi & Swagger
zircote/swagger-php 适用场景与选型建议
zircote/swagger-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 145.8M 次下载、GitHub Stars 达 5.3k, 最近一次更新时间为 2012 年 04 月 19 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「json」 「rest」 「api」 「service discovery」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 zircote/swagger-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 zircote/swagger-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 zircote/swagger-php 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Kinikit - PHP Application development framework MVC component
ext-json wrapper with sane defaults
A package to cast json fields, each sub-keys is castable
A PSR-7 compatible library for making CRUD API endpoints
Api bundle
swagger-php - Generate interactive documentation for your RESTful API using phpdoc annotations
统计信息
- 总下载量: 145.8M
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 5381
- 点击次数: 30
- 依赖项目数: 529
- 推荐数: 13
其他信息
- 授权协议: Apache-2.0
- 更新时间: 2012-04-19