Skip to content

hendrikmaus/drafter-php

Repository files navigation

Drafter PHP Binding

PHP wrapper for Drafter API Blueprint Parser harness v1.x.

Minimum PHP Version Build Status codecov.io Dependency Status Code Climate

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 [refract] Abstract Syntax Trees in JSON or YAML format.

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

The Drafter repository itself has a section on installing drafter it.

Another way of installing Drafter, is using a composer script.
If you do not already have, add a scripts section to your root composer.json:

"scripts": {
    "post-install-cmd": [
        "if ! [[ -d ext/drafter ]]; then echo \"### Installing drafter to ./ext; drafter bin to ./vendor/bin/ ###\"; fi",
        "if ! [[ -d ext/drafter ]]; then git clone --branch v1.0.0 --recursive https://github.com/apiaryio/drafter.git ext/drafter; fi",
        "if ! [[ -d vendor/bin ]]; then mkdir -p vendor/bin; fi",
        "if ! [[ -f vendor/bin/drafter ]]; then cd ext/drafter && ./configure && make drafter; fi",
        "if ! [[ -f vendor/bin/drafter ]]; then cd vendor/bin && ln -s ../../ext/drafter/bin/drafter drafter; fi"
    ]
}

Now run composer install; it should start building drafter within an ext/ folder in your project root. If you want the script to put drafter somewhere else, modify every occurrence of ext/drafter to another one.

Note: there is an open Composer feature request for downloading binaries and compiling from source: composer/composer#4381

You would see this method used by default, when contributing to drafter-php. Installing Drafter using composer has only been tested on Mac OS X and Linux (Ubuntu 12).

If you have issues or questions regarding Drafter, please turn to the Drafter repository.

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

Given this api blueprint source:

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

        Hello World!

This refract element will be the result (json format):

{
  "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"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ]
        }
      ]
    }
  ]
}

Setting the type option to 'ast', results in this abstract syntax tree:

type 'refract' is the default since since drafter v1.0.0

{
  "_version": "4.0",
  "metadata": [],
  "name": "",
  "description": "",
  "element": "category",
  "resourceGroups": [
    {
      "name": "",
      "description": "",
      "resources": [
        {
          "element": "resource",
          "name": "",
          "description": "",
          "uriTemplate": "/message",
          "model": {},
          "parameters": [],
          "actions": [
            {
              "name": "",
              "description": "",
              "method": "GET",
              "parameters": [],
              "attributes": {
                "relation": "",
                "uriTemplate": ""
              },
              "content": [],
              "examples": [
                {
                  "name": "",
                  "description": "",
                  "requests": [],
                  "responses": [
                    {
                      "name": "200",
                      "description": "",
                      "headers": [
                        {
                          "name": "Content-Type",
                          "value": "text/plain"
                        }
                      ],
                      "body": "Hello World!\n",
                      "schema": "",
                      "content": [
                        {
                          "element": "asset",
                          "attributes": {
                            "role": "bodyExample"
                          },
                          "content": "Hello World!\n"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ],
          "content": []
        }
      ]
    }
  ],
  "content": [
    {
      "element": "category",
      "content": [
        {
          "element": "resource",
          "name": "",
          "description": "",
          "uriTemplate": "/message",
          "model": {},
          "parameters": [],
          "actions": [
            {
              "name": "",
              "description": "",
              "method": "GET",
              "parameters": [],
              "attributes": {
                "relation": "",
                "uriTemplate": ""
              },
              "content": [],
              "examples": [
                {
                  "name": "",
                  "description": "",
                  "requests": [],
                  "responses": [
                    {
                      "name": "200",
                      "description": "",
                      "headers": [
                        {
                          "name": "Content-Type",
                          "value": "text/plain"
                        }
                      ],
                      "body": "Hello World!\n",
                      "schema": "",
                      "content": [
                        {
                          "element": "asset",
                          "attributes": {
                            "role": "bodyExample"
                          },
                          "content": "Hello World!\n"
                        }
                      ]
                    }
                  ]
                }
              ]
            }
          ],
          "content": []
        }
      ]
    }
  ]
}

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