osushi/apidoc
Composer 安装命令:
composer require osushi/apidoc
包简介
API markdown document generator
README 文档
README
Generate markdown documentation from your code.
Requirements
php>=7.2.5 || ~8.0.0
Installation
$ composer require --dev osushi/apidoc
Usage examples
Script
1. Create script file
$ mkdir docs $ touch apidoc.php
2. Edit code
<?php require_once '../vendor/autoload.php'; use Osushi\Apidoc\Apidoc; use Osushi\Apidoc\Permission; use Osushi\Apidoc\Parameter; use Osushi\Apidoc\Request; use Osushi\Apidoc\Response; Apidoc::init(); $apiDoc = Apidoc::getInstance(); $permission = new Permission(); $permission->add('users:*'); $permission->add('users:get'); $parameter = new Parameter(); $parameter->add('name', ['isa' => 'string', 'required' => true, 'comment' => 'user name', 'except' => ['bob', 'john']]); $parameter->add('status', ['isa' => 'numric', 'default' => '10', 'comment' => 'user status', 'only' => ['10', '20', '30']]); $parameter->note('Here is note1'); $parameter->note('Here is note2'); $apiDoc->record( 'users:/users:GET', # This key format is {filename}:{path}:{method} $permission, $parameter, 'Get All Users' # It's able to add comment ); $request = new Request([ 'method' => 'GET', 'path' => '/users', 'parameters' => ['status' => 10, 'name' => 'tarou'], 'headers' => ['Content-Type' => 'application/json'], ]); $response = new Response([ 'code' => 200, 'headers' => ['Content-Type' => 'application/json; charset=utf-8'], 'body' => '{"id": 1,"name": "tarou","status": 10,"created_at": "2015-04-21T14:55:09.351Z","updated_at": "2015-04-21T14:55:09.351Z"}', ]); $documents->example( 'users:/users:GET', # This key format is {filename}:{path}:{method} $request, $response, '200 Success' # It's able to add comment ); $apiDoc->render();
3. Run apidoc
$ php apidoc.php APIDOC $ tree docs docs ├── toc.md └── users.md
Here are examples
⚠️ If you want to render documents, please run script with APIDOC params.
Integration to phpunit
1. Initialize apidoc on bootstrap.php
<?php require_once '../vendor/autoload.php'; use Osushi\Apidoc\Apidoc; Apidoc::init(); register_shutdown_function(function(){ $apiDoc = Apidoc::getInstance(); $apiDoc->render(); })
2. Write your feature test.
<?php use Tests\TestCase; use Osushi\Apidoc\Apidoc; use Osushi\Apidoc\Permission; use Osushi\Apidoc\Parameter; use Osushi\Apidoc\Request; use Osushi\Apidoc\Response; class UserIndexTest extends TestCase { public static $apiDoc; public static function setUpBeforeClass() { # Set Permission Details $permission = new Permission(); $permission->add('users:*'); $permission->add('users:get'); # Set Parameter Details $parameter = new Parameter(); $parameter->add('name', ['isa' => 'string', 'required' => true, 'comment' => 'user name', 'except' => ['bob', 'john']]); $parameter->add('status', ['isa' => 'numric', 'default' => '10', 'comment' => 'user status', 'only' => ['10', '20', '30']]); $parameter->note('here is note'); self::$apiDoc = Apidoc::getInstance(); self::$apiDoc->record( 'users:/users:GET', # This key format is {filename}:{path}:{method} $permission, $parameter, 'Get All Users' # Be able to add comment ); } public function testIndex() { $params = [ 'status' => 10, 'name' => 'tarou', ]; $response = $this->call('GET', '/users', $params); $response->assertStatus(200); $request = new Request([ 'method' => 'GET', 'path' => '/users', 'parameters' => $params, 'headers' => ['Content-Type' => 'application/json'], ]); $response = new Response([ 'code' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody(), ]); self::$apiDoc->example( 'users:/users:GET', # This key format is {filename}:{path}:{method} $request, $response, '200 Success' # It's able to add comment ); } }
3. Run apidoc
$ APIDOC=1 phpunit $ tree docs docs ├── toc.md └── users.md
Documents
Initialization
Apidoc::init($config);
Configuration
use Osushi\Apidoc\Config; Apidoc::init([Parameter.php Config::OUTPUT_PATH => 'yourdir', Config::TOC => false, ]);
- Config::DOCUMENT_TEMPLATE_PATH - [String] twig template for each document (default: document.md)
- Config::DOCUMENT_TOC_TEMPLATE_PATH - [String] twig template for ToC of docuement (default: document.toc.md)
- Config::DOCUMENT_TOC_TITLE - [String] ToC of document title (default: # Table of Contents)
- Config::TOC_TEMPLATE_PATH - [String] twig template for ToC (default: toc.md)
- Config::TOC_TITLE - [String] ToC title (default: # Table of Contents);
- Config::OUTPUT_PATH - [String] location to output files (default: ./docs);
- Config::TOC - [Boolean] whether to generate toc.md (default: true);
Parameter
use Osushi\Apidoc\Parameter; $parameter = new Parameter(); $parameter->add('name', ['isa' => 'string', 'required' => true, 'comment' => 'user name', 'except' => ['bob', 'john']]); $parameter->add('status', ['isa' => 'numric', 'default' => '10', 'comment' => 'user status', 'only' => ['10', '20', '30']]); $parameter->note('here is note');
- add(string $value, array $options) - Add parameters for documents
- options.isa - string (e.g. string, integer)
- options.required - boolean (e.g. true/false)
- options.comment- string (e.g. comment)
- options.format- string (e.g. Ymd)
- options.except - array (e.g. ['bob', 'john'])
- options.only - array (e.g. [10, 20])
- note(string $node) - Add note for documents
Request
use Osushi\Apidoc\Request; $request = new Request([ 'method' => {string method}, 'path' => {string path}, 'parameters' => {array params}, 'headers' => {array headers}, ]); # or $request = new Request(); $request = setMethod({string method}); $request = setPath({string method}); $request = setParameters({array params}); $request = setHeaders({array headers});
Response
use Osushi\Apidoc\Response; $response = new Response([ 'code' => {int status_code}, 'headers' => {array headers}, 'body' => {string json_body}, ]); # or $response = new Response(); $response = setCode({int status_code}); $response = setHeaders({array headers}); $response = setBody({string json_body});
License
MIT
osushi/apidoc 适用场景与选型建议
osushi/apidoc 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.85k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2018 年 11 月 28 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「generator」 「api」 「markdown」 「doc」 「document」 「docs」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 osushi/apidoc 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 osushi/apidoc 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 osushi/apidoc 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Memio's PrettyPrinter, used to generate PHP code from given Model
Texy converts plain text in easy to read Texy syntax into structurally valid (X)HTML. It supports adding of images, links, nested lists, tables and has full support for CSS. Texy supports hyphenation of long words (which reflects language rules), clickable emails and URL (emails are obfuscated again
Adds more BBCode
A PSR-7 compatible library for making CRUD API endpoints
Creates a markdown changelog for your GitHub repository.
Lightweight Application To Convert WordPress Readme Into Github Markdown
统计信息
- 总下载量: 2.85k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-11-28