Skip to content

Commit

Permalink
support tags
Browse files Browse the repository at this point in the history
  • Loading branch information
m8rge committed Apr 9, 2015
1 parent ab8c780 commit 15e4d18
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 107 deletions.
115 changes: 115 additions & 0 deletions include.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{% macro headBlock(api) %}
# {{ api.info.title }} {{ api.info.version }}
{% if api.consumes %}
> ### Consumes
{% for contentType in api.consumes %}
> `{{ contentType }}`
{% endfor %}

{% endif %}{#api.consumes#}
> ### Produces
{% if api.produces %}
{% for contentType in api.produces %}
> `{{ contentType }}`
{% endfor %}

{% endif %}{#api.produces#}
{% if api.securityDefinitions %}
{% for title, securityScheme in api.securityDefinitions %}
### Authorization: {{ title }}
Type |{% if securityScheme.type == 'apiKey' %} Name | In |{% endif %} Description
--- |{% if securityScheme.type == 'apiKey' %} --- | --- |{% endif %} ---
{{ securityScheme.type }} |{% if securityScheme.type == 'apiKey' %} {{ securityScheme.name }} | {{ securityScheme.in }} |{% endif %} {{ securityScheme.description }}
{% endfor %}

{% endif %}{#api.securityDefinitions#}
{% endmacro %}

{# #################################################################################### #}

{% macro renderModels(api) %}
# Models
{% for name, data in api.definitions %}
{% set model = data|newSchema %}
## {{ name }}
```json
{{ model.getObject()|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}
```
{% if model.description %}

{{ model.description }}

{% endif %}
### Fields
Name | Type | Description
--- | --- | ---
{% for name, schema in model.properties %}
{{ name }}{% if name in model.required %}<b title="required">&nbsp;*&nbsp;</b>{% endif %} | {{ schema }} | {{ schema.description }}
{% endfor %}

{% endfor %}{# definition #}
{% endmacro %}

{# #################################################################################### #}

{% macro renderActions(api, endPoint, actions) %}
{% for httpAction, action in actions %}
## {{ action.summary|capitalize }}

```http
{{ httpAction|upper }} {{ api.basePath }}{{ endPoint }} HTTP/1.1
{% set parameters = action.parameters|newParameters %}
{% set body = parameters.getBodyObject() %}
{% if body %}
Content-Type: {{ api.consumes[0]|default('application/json') }}

{{ body|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}
{% endif %}
```
{% for code, response in action.responses %}
```http
HTTP/1.1 {{ code }} {{ code|textStatus }}
{% for contentType, response in response.examples|slice(0,1) %}
Content-Type: {{ contentType }}

{{ response|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_UNESCAPED_UNICODE'))|raw }}
{% else %}
{% if response.schema %}
Content-Type: {{ api.produces[0]|default('application/json') }}

{% set model = response.schema|newSchema %}
{{ model.getObject()|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}
{% endif %}{#response.schema#}
{% endfor %}{#contentType, response in response.examples|slice(0,1) %}#}
```
{% endfor %}{#code, response in action.responses#}
{% if action.description %}

{{ action.description }}

{% endif %}{#action.description#}

{% if action.parameters %}
{% set parameters = action.parameters|newParameters %}
### Parameters
Name | In | Type | Description
--- | --- | --- | ---
{% for parameter in parameters %}
{{ parameter.name }}{% if parameter.required %}<b title="required">&nbsp;*&nbsp;</b>{% endif %} | {{ parameter.in }} | {{ parameter }} | {{ parameter.description }}
{% endfor %}

{% endif %}{#action.parameters#}
### Responses
Http code | Type | Description
--- | --- | ---
{% for status, data in action.responses %}
{% if data.schema %}
{% set model = data.schema|newSchema %}
{% else %}
{% set model = 'no content' %}
{% endif %}{#data.schema#}
{{ status }} | {{ model }} | {{ data.description }}
{% endfor %}{#status, data in action.responses#}

{% endfor %}{# httpAction, action in actions #}
{% endmacro %}
7 changes: 4 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
$autoLoader = require_once 'vendor/autoload.php';
$autoLoader->addPsr4('e96\\', __DIR__);

$swagger = json_decode(file_get_contents('php://stdin'), true);
if ($swagger['swagger'] != '2.0') {
$config = json_decode(file_get_contents('php://stdin'), true);
if ($config['swagger'] != '2.0') {
die('swagger version must be 2.0');
}
Swagger::$root = $swagger;
Swagger::$root = $config;
$swagger = new Swagger($config);

$loader = new Twig_Loader_Filesystem(__DIR__);
$twig = new Twig_Environment($loader);
Expand Down
115 changes: 12 additions & 103 deletions slate.twig
Original file line number Diff line number Diff line change
@@ -1,112 +1,21 @@
{% import 'include.twig' as func %}
---
title: {{ api.info.title }} {{ api.info.version }}

search: true
---

# {{ api.info.title }} {{ api.info.version }}
{% if api.consumes %}
> ### Consumes
{% for contentType in api.consumes %}
> `{{ contentType }}`
{% endfor %}
{{ func.headBlock(api) }}

{% endif %}
> ### Produces
{% if api.produces %}
{% for contentType in api.produces %}
> `{{ contentType }}`
{% endfor %}

{% endif %}
{% if api.securityDefinitions %}
{% for title, securityScheme in api.securityDefinitions %}
### Authorization: {{ title }}
Type |{% if securityScheme.type == 'apiKey' %} Name | In |{% endif %} Description
--- |{% if securityScheme.type == 'apiKey' %} --- | --- |{% endif %} ---
{{ securityScheme.type }} |{% if securityScheme.type == 'apiKey' %} {{ securityScheme.name }} | {{ securityScheme.in }} |{% endif %} {{ securityScheme.description }}
{% endfor %}

{% endif %}
{% for endPoint, actions in api.paths %}
{% for httpAction, action in actions %}
## {{ action.summary|capitalize }}

```http
{{ httpAction|upper }} {{ api.basePath }}{{ endPoint }} HTTP/1.1
{% set parameters = action.parameters|newParameters %}
{% set body = parameters.getBodyObject() %}
{% if body %}
Content-Type: {{ api.consumes[0]|default('application/json') }}

{{ body|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}
{% endif %}
```
{% for code, response in action.responses %}
```http
HTTP/1.1 {{ code }} {{ code|textStatus }}
{% for contentType, response in response.examples|slice(0,1) %}
Content-Type: {{ contentType }}

{{ response|json_encode(constant('JSON_PRETTY_PRINT') b-or constant('JSON_UNESCAPED_UNICODE'))|raw }}
{% for tag in api.tags %}
# {{ tag.name|capitalize }}
{% for endPoint, actions in api.getPathsByTag(tag.name) %}
{{ func.renderActions(api, endPoint, actions) }}
{% endfor %}{# endPoint #}
{% else %}
{% if response.schema %}
Content-Type: {{ api.produces[0]|default('application/json') }}

{% set model = response.schema|newSchema %}
{{ model.getObject()|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}
{% endif %}
{% endfor %}
```
{% endfor %}
{% if action.description %}

{{ action.description }}

{% endif %}

{% if action.parameters %}
{% set parameters = action.parameters|newParameters %}
### Parameters
Name | In | Type | Description
--- | --- | --- | ---
{% for parameter in parameters %}
{{ parameter.name }}{% if parameter.required %}<b title="required">&nbsp;*&nbsp;</b>{% endif %} | {{ parameter.in }} | {{ parameter }} | {{ parameter.description }}
{% endfor %}

{% endif %}
### Responses
Http code | Type | Description
--- | --- | ---
{% for status, data in action.responses %}
{% if data.schema %}
{% set model = data.schema|newSchema %}
{% else %}
{% set model = 'no content' %}
{% endif %}
{{ status }} | {{ model }} | {{ data.description }}
{% endfor %}

{% endfor %}{# httpAction #}
{% endfor %}{# endpoint #}

# Models
{% for name, data in api.definitions %}
{% set model = data|newSchema %}
## {{ name }}
```json
{{ model.getObject()|json_encode(constant('JSON_PRETTY_PRINT'))|raw }}
```
{% if model.description %}

{{ model.description }}

{% endif %}
### Fields
Name | Type | Description
--- | --- | ---
{% for name, schema in model.properties %}
{{ name }}{% if name in model.required %}<b title="required">&nbsp;*&nbsp;</b>{% endif %} | {{ schema }} | {{ schema.description }}
{% endfor %}
{% for endPoint, actions in api.paths %}
{{ func.renderActions(api, endPoint, actions) }}
{% endfor %}{# endPoint #}
{% endfor %}{# tag #}

{% endfor %}{# definition #}
{{ func.renderModels(api) }}
6 changes: 6 additions & 0 deletions swagger/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ class Object // implements ArrayAccess
public function __construct($config = [])
{
$this->setConfig($config);
$this->init();
}

public function init()
{

}

public function setConfig($config = [])
Expand Down
86 changes: 85 additions & 1 deletion swagger/Swagger.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,91 @@
namespace e96\swagger;


class Swagger
class Swagger extends Object
{
public static $root;

/**
* @var string
*/
public $swagger;

/**
* @var mixed
*/
public $info;

/**
* @var string
*/
public $basePath;

/**
* @var string[]
*/
public $consumes;

/**
* @var string[]
*/
public $produces;

/**
* @var mixed
*/
public $paths;

/**
* @var mixed
*/
public $definitions;

/**
* @var Response[]
*/
public $responses;

/**
* @var SecurityScheme[]
*/
public $securityDefinitions;

/**
* @var mixed
*/
public $tags;

public function setSecurityDefinitions($config)
{
$this->securityDefinitions = [];
foreach ($config as $name => $innerConfig) {
$this->securityDefinitions[$name] = new SecurityScheme($innerConfig);
}
}

public function setResponses($config)
{
$this->responses = [];
foreach ($config as $name => $innerConfig) {
$this->responses[$name] = new Response($innerConfig);
}
}

public function getPathsByTag($tag)
{
$methods = ['get', 'post', 'put', 'delete', 'options', 'head', 'path'];
$res = [];
foreach ($this->paths as $endPoint => $pathItem) {
foreach ($methods as $method) {
if (array_key_exists($method, $pathItem) &&
array_key_exists('tags', $pathItem[$method]) &&
in_array($tag, $pathItem[$method]['tags'])
) {
$res[$endPoint][$method] = $pathItem[$method];
}
}
}

return $res;
}
}

0 comments on commit 15e4d18

Please sign in to comment.