定制 hmaus/drafter-php 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

hmaus/drafter-php

Composer 安装命令:

composer require hmaus/drafter-php

包简介

PHP wrapper around drafter binary

README 文档

README

PHP wrapper for Drafter API Blueprint Parser harness.

Minimum PHP Version codecov.io

What is Drafter-php?

Drafter-php allows you to use use the drafter API Blueprint Parser harness with your PHP application.

In a nutshell: you can convert API Blueprint files to parse result.

API Blueprint is a webservice documentation language built on top of Markdown.

Requirements

What Is What

Drafter is a C++ tool to parse API Blueprint.
Drafter-php is a PHP wrapper around the Drafter command line tool.

Installation

The recommended way to install Drafter-php is by using composer:

$ composer require hmaus/drafter-php

This will install the PHP package with your application.
Please keep in mind that Drafter is not included.

Install Drafter Command Line Tool using Composer

Head over to hmaus/drafter-installer.

Usage of Drafter-php

  1. Get an instance of the \DrafterPhp\DrafterInterface implementation, \DrafterPhp\Drafter 1.1 You will need to pass the path to your drafter binary to the constructor 1.2 It is recommended to solve this using a dependency injection container
  2. Set the input file and options on your \DrafterPhp\Drafter instance 2.1 Drafter-php currently does not support passing blueprint code directly to Drafter; it has to be stored in a file at this time
  3. Run your command

Input / Output Examples

Note: drafter-php does not assert the structure of the output. If you see differences in the examples to your actual output, please refer to the official drafter docs.

Given this api blueprint source:

# GET /message
+ Response 200 (text/plain)

        Hello World!

The result will look similar (json refract):

{
  "element": "parseResult",
  "content": [
    {
      "element": "category",
      "meta": {
        "classes": [
          "api"
        ],
        "title": ""
      },
      "content": [
        {
          "element": "category",
          "meta": {
            "classes": [
              "resourceGroup"
            ],
            "title": ""
          },
          "content": [
            {
              "element": "resource",
              "meta": {
                "title": ""
              },
              "attributes": {
                "href": "/message"
              },
              "content": [
                {
                  "element": "transition",
                  "meta": {
                    "title": ""
                  },
                  "content": [
                    {
                      "element": "httpTransaction",
                      "content": [
                        {
                          "element": "httpRequest",
                          "attributes": {
                            "method": "GET"
                          },
                          "content": []
                        },
                        {
                          "element": "httpResponse",
                          "attributes": {
                            "statusCode": "200",
                            "headers": {
                              "element": "httpHeaders",
                              "content": [
                                {
                                  "element": "member",
                                  "content": {
                                    "key": {
                                      "element": "string",
                                      "content": "Content-Type"
                                    },
                                    "value": {
                                      "element": "string",
                                      "content": "text/plain"
                                    }
                                  }
                                }
                              ]
                            }
                          },
                          "content": [
                            {
                              "element": "asset",
                              "meta": {
                                "classes": [
                                  "messageBody"
                                ]
                              },
                              "attributes": {
                                "contentType": "text/plain"
                              },
                              "content": "Hello World!\n"
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Code Examples

Found something wrong? Feel free to contribute

Make sure it works

To make sure it works, we'll ask Drafter for the current version.

$version = $drafter
    ->version()
    ->run();

// Reset options on the command
$drafter->resetOptions();

$version should now contain a string like v1.0.0. If something is wrong, an exception will have been thrown most likely.

Keep in mind that Drafter-php is designed to keep its state, run \DrafterPhp\DrafterInterface::resetOptions to get rid of the version option you just set for the next call on the instance.

Parse your-service.apib into your-service.refract.json

Make sure your input path is correct and readable, and your output path is writable.

$drafter
    ->input('your-service.apib')
    ->format('json')
    ->type('refract')
    ->output('your-service.refract.json')
    ->run();

Parse your-service.apib into your-service.ast.json

Make sure your input path is correct and readable, and your output path is writable.

$drafter
    ->input('your-service.apib')
    ->format('json')
    ->output('your-service.ast.json')
    ->run();

Parse your-service.apib into a PHP data structure

$refract = $drafter
    ->input('your-service.apib')
    ->format('json')
    ->run();
    
$phpObj = json_decode($refract);
$phpArr = json_decode($refract, true);

Parse your-service.apib into YAML format

$drafter
    ->input('your-service.apib')
    ->format('yaml') // optional as yaml is the default
    ->output('your-service.ast.yml')
    ->run();

Get Process before it is run

$process = $drafter
    ->input('your-service.apib')
    ->format('json')
    ->output('your-service.refract.json')
    ->build();

// do stuff with the process

$drafter
    ->run($process);

Feature Roadmap

Do not hesitate to contribute.

  • support passing raw api blueprint code into \DrafterPhp\DrafterInterface::input, rather than always a file path

License

Drafter-php is licensed under the MIT License - see the LICENSE file for details

hmaus/drafter-php 适用场景与选型建议

hmaus/drafter-php 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 128.28k 次下载、GitHub Stars 达 32, 最近一次更新时间为 2015 年 09 月 03 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 hmaus/drafter-php 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 hmaus/drafter-php 我们能提供哪些服务?
定制开发 / 二次开发

基于 hmaus/drafter-php 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 128.28k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 32
  • 点击次数: 21
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 32
  • Watchers: 2
  • Forks: 9
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-09-03