Swagger php - 在不拆分代码的情况下过滤 public/private API
Swagger php - filtering public/private APIs without splitting up code
我正在考虑使用 Zircote Swagger PHP 来记录 API。我还没有完全熟悉它,因为我在承诺使用它之前一直试图回答这个问题。
我想要两套不同的文档。一个文档页面将包含我们所有的 API,而另一个将仅包含那些应该 public 记录的 API。
我发现 或多或少给了我想要的结果。但是,他们建议的解决方案要求我显着重构现有代码,因为我们的 public 和私有 API 的实现尚未按照答案建议的方式拆分。
有没有办法在注释中标记单个 API,并在我生成文档时过滤这些标记?
尚未实施,但我相信欢迎 PR。
目前您可以post处理生成的文档,例如通过单层管道输送:
swagger /path/to/project | php -r '$s = json_decode(file_get_contents("php://stdin"),true);array_walk($s["paths"],function(&$p){$p=array_filter($p,function($m){return count(array_intersect(["tag1","tag2"],$m["tags"]))==0;});});file_put_contents("php://stdout",json_encode($s,192));' > public.json
为了便于阅读而格式化:
$s = json_decode(file_get_contents("php://stdin"), true);
array_walk(
$s["paths"],
function (&$path) {
$path = array_filter(
$path,
function ($method) {
return count(array_intersect(["tag1", "tag2"], $method["tags"])) == 0;
}
);
}
);
file_put_contents("php://stdout", json_encode($s, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
其中 tag1
和 tag2
是要排除的标签。
我正在考虑使用 Zircote Swagger PHP 来记录 API。我还没有完全熟悉它,因为我在承诺使用它之前一直试图回答这个问题。
我想要两套不同的文档。一个文档页面将包含我们所有的 API,而另一个将仅包含那些应该 public 记录的 API。
我发现
有没有办法在注释中标记单个 API,并在我生成文档时过滤这些标记?
尚未实施,但我相信欢迎 PR。
目前您可以post处理生成的文档,例如通过单层管道输送:
swagger /path/to/project | php -r '$s = json_decode(file_get_contents("php://stdin"),true);array_walk($s["paths"],function(&$p){$p=array_filter($p,function($m){return count(array_intersect(["tag1","tag2"],$m["tags"]))==0;});});file_put_contents("php://stdout",json_encode($s,192));' > public.json
为了便于阅读而格式化:
$s = json_decode(file_get_contents("php://stdin"), true);
array_walk(
$s["paths"],
function (&$path) {
$path = array_filter(
$path,
function ($method) {
return count(array_intersect(["tag1", "tag2"], $method["tags"])) == 0;
}
);
}
);
file_put_contents("php://stdout", json_encode($s, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
其中 tag1
和 tag2
是要排除的标签。