uuf6429/oas-php

OpenAPI 规范的 PHP 实现

v0.1-alpha 2020-03-25 19:21 UTC

This package is auto-updated.

Last update: 2024-09-06 09:24:24 UTC


README

Minimum PHP Version License Packagist

OAS-PHP:OpenAPI 规范的 PHP 实现。

这个库是 OpenAPI v3 规范的实现。OpenAPI v3 规范

使用

这个库只是一系列值对象。主入口点是 Document 类,就像规范一样。

$document = new \uuf6429\OpenAPI\Spec\Document();
$path = new \uuf6429\OpenAPI\Path();
$document->paths->set('/resource', $path);

渲染

理想情况下,应该使用 YAML 序列化器(如 symfony)进行渲染。由于 YAML 是 JSON 的超集,也可以这样做:

$document = new Document();
$json = json_encode($document, JSON_THROW_ON_ERROR | JSON_PRETTY_PRINT);

// save to file
file_put_contents('openapi.yaml', $json);

// or serve it out
header('Content-Type: application/x-yaml');
echo $json;

为什么?

这个库并不比现有的众多生成器等更好。理念是,现有的和新开发的基于 PHP 的 OpenAPI 处理工具应该使用这个实现,而不是自己编写 OpenAPI 规范的版本。