├── LICENSE ├── Module.php ├── ModuleAsset.php ├── README.md ├── assets ├── doc.css ├── doc.js └── jsonview │ ├── jquery.jsonview.min.css │ └── jquery.jsonview.min.js ├── composer.json ├── controllers └── DefaultController.php └── views └── default └── index.php /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Volodymyr Dovbenko 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Module.php: -------------------------------------------------------------------------------- 1 | getUrlManager()->addRules([ 13 | $this->id => $this->id . '/default/index', 14 | $this->id . '//' => $this->id . '//', 15 | ], false); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /ModuleAsset.php: -------------------------------------------------------------------------------- 1 | [ 11 | ... 12 | 'documentation' => 'nostop8\yii2\rest_api_doc\Module', 13 | ... 14 | ], 15 | ``` 16 | 17 | - In your application config file inside `bootstrap` section add: 18 | ``` 19 | 'bootstrap' => [ 20 | ... 21 | 'documentation' 22 | ... 23 | ], 24 | ``` 25 | Please, note. You may change `documentation` into any other word, which would be better to call documentation for your REST API. 26 | 27 | - Now run your application at `http:///documentation` and if you did everything correct, you shoul see something like this: ![alt tag](http://i.imgur.com/uw91eR6.png) 28 | 29 | 30 | ## Usage 31 | - First of all you should know that this documentation generator will work only in case you define your REST API endpoints using following principles: http://www.yiiframework.com/doc-2.0/guide-rest-routing.html 32 | - Currently you can define for you endpoints following annotations types, which will be later displayed/provided by API documentation generator: 33 | 34 | 1. Rest Description: Your endpoint description. 35 | 2. Rest Fields: ['field1', 'field2'] or ['field3', 'field4']. (Please, note: `or` and array after it is extra and might be skipped if your service accepts only one type of body) 36 | 3. Rest Filters: ['filter1', 'filter2']. 37 | 4. Rest Expand: ['expandRelation1', 'expandRelation2']. 38 | 39 | - In case you are using CRUD services, which does not require endpoints to be defined (because they are already predefined inside `yii\rest\UrlRule` - http://www.yiiframework.com/doc-2.0/yii-rest-urlrule.html and implemented inside `\yii\rest\ActiveController`) and you still want to add some description, define in your controller empty methods with the same names (e.g. actionCreate, actionUpdate etc.) and add annotations to them as you would do for other actions implemented by you. 40 | 41 | ## Example of annotations 42 | 43 | ``` 44 | online API mocking tool - QuickMocker. It is also useful while integrating your application with webhooks to debug requests that arrive from some 3-rd party while you do not host your application remotely. 69 | -------------------------------------------------------------------------------- /assets/doc.css: -------------------------------------------------------------------------------- 1 | .docs-index .label { 2 | color: #333; 3 | } 4 | .docs-index .pointer { 5 | cursor: pointer 6 | } 7 | .docs-index .ellipsis { 8 | text-overflow: ellipsis; 9 | overflow: hidden; 10 | } -------------------------------------------------------------------------------- /assets/doc.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | 'use strict'; 3 | 4 | function prettify(value) { 5 | try { 6 | var object; 7 | eval('object = ' + value); 8 | return JSON.stringify(object, undefined, 2); 9 | } catch (e) { 10 | console.log(e); 11 | return value; 12 | } 13 | } 14 | 15 | function escapeHtml(text) { 16 | return text 17 | .replace(//g, ">") 19 | .replace(/(?:\r\n|\r|\n)/g, '
') 20 | .replace(/\s/g, " "); 21 | } 22 | 23 | $(document).ready(function () { 24 | $('.endpoint-toggle').click(function () { 25 | $(this).toggleClass('active'); 26 | }); 27 | 28 | $('.prettify').click(function (e) { 29 | e.preventDefault(); 30 | var $bodyField = $(this).closest('form').find('[name="body"]'); 31 | $bodyField.val(prettify($bodyField.val())); 32 | }); 33 | 34 | $('.sample').each(function () { 35 | $(this).html(escapeHtml(prettify($(this).text()))); 36 | }).click(function (e) { 37 | e.preventDefault(); 38 | var $bodyField = $(this).closest('form').find('[name="body"]'); 39 | $bodyField.val(prettify($(this).text())); 40 | }); 41 | 42 | $('#token, #base_url').each(function () { 43 | $(this).val(localStorage.getItem('rest_api_doc_' + $(this).attr('id'))); 44 | }); 45 | 46 | $('#token, #base_url').change(function () { 47 | localStorage.setItem('rest_api_doc_' + $(this).attr('id'), $(this).val()); 48 | }); 49 | 50 | $('.docs-index form').submit(function (e) { 51 | e.preventDefault(); 52 | var form = this; 53 | var url = $('[name="url"]', form).val(); 54 | var method = $('[name="method"]', form).val(); 55 | var body; 56 | var $bodyField = $('[name="body"]', form); 57 | if ($bodyField.length) { 58 | $bodyField.val(prettify($bodyField.val())); 59 | body = $bodyField.val(); 60 | } 61 | $('.params input', form).each(function () { 62 | url = url.replace($(this).attr('data-key'), $(this).val()); 63 | }); 64 | 65 | var urlParams = []; 66 | $('.filters input', form).each(function () { 67 | urlParams.push($(this).attr('data-key') + '=' + $(this).val()); 68 | }); 69 | 70 | var expand = []; 71 | $('.expand input:checked', form).each(function () { 72 | expand.push($(this).val()); 73 | }); 74 | if (expand.length) { 75 | urlParams.push('expand=' + expand.join(',')); 76 | } 77 | if (urlParams.length) { 78 | url += '?' + urlParams.join('&'); 79 | } 80 | var response = $('.response', form).get(0); 81 | $('.loader', response).removeClass('hidden'); 82 | $('.data', response).addClass('hidden'); 83 | $('.data .element', response).text(''); 84 | 85 | var formData = {}; 86 | if ($('.files', form).length) { 87 | formData = { 88 | data: new FormData(form), 89 | processData: false, 90 | contentType: false, 91 | cache: false 92 | }; 93 | } 94 | 95 | var ajaxParams = $.extend({ 96 | url: $('#base_url').val() + url, 97 | method: method, 98 | data: body, 99 | contentType: 'application/json', 100 | dataType: 'json', 101 | headers: { 102 | Authorization: 'Bearer ' + $('#token').val() 103 | } 104 | }, formData); 105 | 106 | var ajax = $.ajax(ajaxParams).done(function (data, textStatus, jqXHR) { 107 | outputResponse(jqXHR, response); 108 | }).fail(function (jqXHR, textStatus, errorThrown) { 109 | outputResponse(jqXHR, response); 110 | }).always(function () { 111 | $('.loader', response).addClass('hidden'); 112 | }); 113 | 114 | function outputResponse(jqXHR, response) { 115 | $('.data', response).removeClass('hidden'); 116 | $('.code', response).text(jqXHR.status); 117 | $('.final-url', response).text(url); 118 | $('.text', response).text(jqXHR.statusText); 119 | if (jqXHR.responseText) { 120 | $('.body', response).JSONView(prettify(jqXHR.responseText), {collapsed: jqXHR.status.toString().indexOf('20') === 0 ? true : false}); 121 | } else { 122 | $('.body', response).html('
Empty
'); 123 | } 124 | $('.headers', response).html(escapeHtml(ajax.getAllResponseHeaders())); 125 | } 126 | }); 127 | }); 128 | 129 | })(); -------------------------------------------------------------------------------- /assets/jsonview/jquery.jsonview.min.css: -------------------------------------------------------------------------------- 1 | @charset "UTF-8";.jsonview{font-family:monospace;font-size:1.1em;white-space:pre-wrap}.jsonview .prop{font-weight:700;text-decoration:none;color:#000}.jsonview .null,.jsonview .undefined{color:red}.jsonview .bool,.jsonview .num{color:#00f}.jsonview .string{color:green;white-space:pre-wrap}.jsonview .string.multiline{display:inline-block;vertical-align:text-top}.jsonview .collapser{position:absolute;left:-1em;cursor:pointer}.jsonview .collapsible{transition:height 1.2s;transition:width 1.2s}.jsonview .collapsible.collapsed{height:.8em;width:1em;display:inline-block;overflow:hidden;margin:0}.jsonview .collapsible.collapsed:before{content:"…";width:1em;margin-left:.2em}.jsonview .collapser.collapsed{transform:rotate(0)}.jsonview .q{display:inline-block;width:0;color:transparent}.jsonview li{position:relative}.jsonview ul{list-style:none;margin:0 0 0 2em;padding:0}.jsonview h1{font-size:1.2em} -------------------------------------------------------------------------------- /assets/jsonview/jquery.jsonview.min.js: -------------------------------------------------------------------------------- 1 | !function(e){var t,n,r,l,o;return o=["object","array","number","string","boolean","null"],r=function(){function t(e){null==e&&(e={}),this.options=e}return t.prototype.htmlEncode=function(e){return null!==e?e.toString().replace(/&/g,"&").replace(/"/g,""").replace(//g,">"):""},t.prototype.jsString=function(e){return e=JSON.stringify(e).slice(1,-1),this.htmlEncode(e)},t.prototype.decorateWithSpan=function(e,t){return''+this.htmlEncode(e)+""},t.prototype.valueToHTML=function(t,n){var r;if(null==n&&(n=0),r=Object.prototype.toString.call(t).match(/\s(.+)]/)[1].toLowerCase(),this.options.strict&&!e.inArray(r,o))throw new Error(""+r+" is not a valid JSON value type");return this[""+r+"ToHTML"].call(this,t,n)},t.prototype.nullToHTML=function(e){return this.decorateWithSpan("null","null")},t.prototype.undefinedToHTML=function(){return this.decorateWithSpan("undefined","undefined")},t.prototype.numberToHTML=function(e){return this.decorateWithSpan(e,"num")},t.prototype.stringToHTML=function(e){var t,n;return/^(http|https|file):\/\/[^\s]+$/i.test(e)?'"'+this.jsString(e)+'"':(t="",e=this.jsString(e),this.options.nl2br&&(n=/([^>\\r\\n]?)(\\r\\n|\\n\\r|\\r|\\n)/g,n.test(e)&&(t=" multiline",e=(e+"").replace(n,"$1
"))),'"'+e+'"')},t.prototype.booleanToHTML=function(e){return this.decorateWithSpan(e,"bool")},t.prototype.arrayToHTML=function(e,t){var n,r,l,o,i,s,a,p;for(null==t&&(t=0),r=!1,i="",o=e.length,l=a=0,p=e.length;p>a;l=++a)s=e[l],r=!0,i+="
  • "+this.valueToHTML(s,t+1),o>1&&(i+=","),i+="
  • ",o--;return r?(n=0===t?"":" collapsible",'[
      '+i+"
    ]"):"[ ]"},t.prototype.objectToHTML=function(e,t){var n,r,l,o,i,s,a;null==t&&(t=0),r=!1,i="",o=0;for(s in e)o++;for(s in e)a=e[s],r=!0,l=this.options.escape?this.jsString(s):s,i+='
  • "'+l+'": '+this.valueToHTML(a,t+1),o>1&&(i+=","),i+="
  • ",o--;return r?(n=0===t?"":" collapsible",'{
      '+i+"
    }"):"{ }"},t.prototype.jsonToHTML=function(e){return'
    '+this.valueToHTML(e)+"
    "},t}(),"undefined"!=typeof module&&null!==module&&(module.exports=r),n=function(){function e(){}return e.bindEvent=function(e,t){var n;return e.firstChild.addEventListener("click",function(e){return function(n){return e.toggle(n.target.parentNode.firstChild,t)}}(this)),n=document.createElement("div"),n.className="collapser",n.innerHTML=t.collapsed?"+":"-",n.addEventListener("click",function(e){return function(n){return e.toggle(n.target,t)}}(this)),e.insertBefore(n,e.firstChild),t.collapsed?this.collapse(n):void 0},e.expand=function(e){var t,n;return n=this.collapseTarget(e),""!==n.style.display?(t=n.parentNode.getElementsByClassName("ellipsis")[0],n.parentNode.removeChild(t),n.style.display="",e.innerHTML="-"):void 0},e.collapse=function(e){var t,n;return n=this.collapseTarget(e),"none"!==n.style.display?(n.style.display="none",t=document.createElement("span"),t.className="ellipsis",t.innerHTML=" … ",n.parentNode.insertBefore(t,n),e.innerHTML="+"):void 0},e.toggle=function(e,t){var n,r,l,o,i,s;if(null==t&&(t={}),l=this.collapseTarget(e),n="none"===l.style.display?"expand":"collapse",t.recursive_collapser){for(r=e.parentNode.getElementsByClassName("collapser"),s=[],o=0,i=r.length;i>o;o++)e=r[o],s.push(this[n](e));return s}return this[n](e)},e.collapseTarget=function(e){var t,n;return n=e.parentNode.getElementsByClassName("collapsible"),n.length?t=n[0]:void 0},e}(),t=e,l={collapse:function(e){return"-"===e.innerHTML?n.collapse(e):void 0},expand:function(e){return"+"===e.innerHTML?n.expand(e):void 0},toggle:function(e){return n.toggle(e)}},t.fn.JSONView=function(){var e,o,i,s,a,p,c;return e=arguments,null!=l[e[0]]?(a=e[0],this.each(function(){var n,r;return n=t(this),null!=e[1]?(r=e[1],n.find(".jsonview .collapsible.level"+r).siblings(".collapser").each(function(){return l[a](this)})):n.find(".jsonview > ul > li .collapsible").siblings(".collapser").each(function(){return l[a](this)})})):(s=e[0],p=e[1]||{},o={collapsed:!1,nl2br:!1,recursive_collapser:!1,escape:!0,strict:!1},p=t.extend(o,p),i=new r(p),"[object String]"===Object.prototype.toString.call(s)&&(s=JSON.parse(s)),c=i.jsonToHTML(s),this.each(function(){var e,r,l,o,i,s;for(e=t(this),e.html(c),l=e[0].getElementsByClassName("collapsible"),s=[],o=0,i=l.length;i>o;o++)r=l[o],"LI"===r.parentNode.nodeName?s.push(n.bindEvent(r.parentNode,p)):s.push(void 0);return s}))}}(jQuery); -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "nostop8/rest-api-doc", 3 | "type": "yii2-extension", 4 | "description": "Simple documentation generator for Yii2 REST applications based on defined API endpoints and actions annotations.", 5 | "keywords": ["yii2", "rest", "api", "generator", "documentation"], 6 | "license": "MIT", 7 | "authors": [ 8 | { 9 | "name": "Volodymyr Dovbenko", 10 | "email": "nostop8@gmail.com" 11 | } 12 | ], 13 | "require": { 14 | "yiisoft/yii2-bootstrap": "~2.0.0", 15 | "bower-asset/jquery": ">=1.11.1" 16 | }, 17 | "autoload": { 18 | "psr-4": { 19 | "nostop8\\yii2\\rest_api_doc\\": "" 20 | } 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /controllers/DefaultController.php: -------------------------------------------------------------------------------- 1 | getView(); 15 | \nostop8\yii2\rest_api_doc\ModuleAsset::register($view); 16 | parent::init(); 17 | } 18 | 19 | /** 20 | * {@inheritdoc} 21 | */ 22 | public function beforeAction($action) 23 | { 24 | \Yii::$app->response->format = Response::FORMAT_HTML; 25 | return parent::beforeAction($action); 26 | } 27 | 28 | public function actionIndex() 29 | { 30 | $rules = []; 31 | foreach (\Yii::$app->urlManager->rules as $urlRule) { 32 | if ($urlRule instanceof \yii\rest\UrlRule) { 33 | foreach ($urlRule->controller as $urlName => $controllerName) { 34 | $entity = []; 35 | $controllerName = strrchr($controllerName, '/') === false ? $controllerName : substr(strrchr($controllerName, '/'), 1); 36 | if (strpos($urlName, '/') !== FALSE) { 37 | $entity['title'] = Inflector::titleize(str_replace('/', ' - ', $urlName), TRUE); 38 | } else { 39 | $entity['title'] = str_replace(['/'], '_', ucfirst($controllerName)); 40 | } 41 | $urlRuleReflection = new \ReflectionClass($urlRule); 42 | $rulesObject = $urlRuleReflection->getProperty('rules'); 43 | $rulesObject->setAccessible(true); 44 | $generatedRules = $rulesObject->getValue($urlRule); 45 | 46 | $entity['rules'] = $this->_processRules($generatedRules[$urlName]); 47 | 48 | $rules[] = $entity; 49 | } 50 | } 51 | } 52 | return $this->render('index', [ 53 | 'rules' => $rules, 54 | ]); 55 | } 56 | 57 | function _processRules($generatedRules) 58 | { 59 | $rules = []; 60 | 61 | foreach ($generatedRules as $generatedRule) { 62 | $reflectionObject = new \ReflectionClass($generatedRule); 63 | $templateObject = $reflectionObject->getProperty('_template'); 64 | $templateObject->setAccessible(true); 65 | if (empty($generatedRule->verb)) { 66 | continue; 67 | } 68 | $rule = []; 69 | $rule['url'] = str_replace(['<', '>'], ['{', '}'], rtrim($templateObject->getValue($generatedRule), '/')); 70 | $rule['method'] = current($generatedRule->verb); 71 | preg_match_all('/\{[^}]*\}/', $rule['url'], $matched); 72 | 73 | $params = []; 74 | if (!empty($matched[0])) { 75 | foreach ($matched[0] as $key) { 76 | $name = str_replace(['{', '}'], '', $key); 77 | $params[] = [ 78 | 'key' => $key, 79 | 'name' => $name, 80 | 'title' => $name == 'id' ? 'ID' : ucfirst(str_replace('_', ' ', $name)), 81 | ]; 82 | } 83 | } 84 | 85 | $rule['params'] = $params; 86 | 87 | list($controller, $actionID) = \Yii::$app->createController($generatedRule->route); 88 | 89 | try { 90 | $methodName = 'action' . BaseInflector::id2camel($actionID); 91 | $controllerReflection = new \ReflectionClass($controller); 92 | $methodInfo = $controllerReflection->getMethod($methodName); 93 | $fieldsString = $this->_findString($methodInfo->getDocComment(), 'Rest Fields'); 94 | if ($fieldsString) { 95 | $fieldsOptions = explode('||', $fieldsString); 96 | foreach ($fieldsOptions as $fieldsOption) { 97 | eval('$rule[\'fields\'][] = ' . $fieldsOption . ';'); 98 | } 99 | } 100 | $rule['filters'] = $this->_findElements($methodInfo->getDocComment(), 'Rest Filters'); 101 | $rule['expand'] = $this->_findElements($methodInfo->getDocComment(), 'Rest Expand'); 102 | $rule['description'] = $this->_findString($methodInfo->getDocComment(), 'Rest Description'); 103 | } catch (\Exception $ex) { 104 | // Silence, because we do not require description of REST 105 | // ActiveController method. TODO: add some warning. 106 | } 107 | 108 | if (!empty($rule['fields'])) { 109 | $fileFields = []; 110 | $rule['fields'] = $this->_fieldsFlip($rule['fields'], $fileFields); 111 | $rule['fileFields'] = $fileFields; 112 | } 113 | 114 | $rules[] = $rule; 115 | } 116 | 117 | usort($rules, function ($a, $b) { 118 | return strcmp($a['url'], $b['url']); 119 | }); 120 | 121 | return $rules; 122 | } 123 | 124 | function _fieldsFlip($fields, &$fileFields = []) 125 | { 126 | $flipped = []; 127 | foreach ($fields as $key => $field) { 128 | if (is_array($field)) { 129 | $flipped[$key] = $this->_fieldsFlip($field, $fileFields); 130 | } else { 131 | if (substr($field, 0, 1) == '_') { 132 | $field = substr($field, 1); 133 | } elseif (strpos($field, ':')) { 134 | list($fieldName, $fieldType) = explode(':', $field); 135 | if ($fieldType == 'file') { 136 | $fileFields[] = $fieldName; 137 | continue; 138 | } 139 | } 140 | $flipped[$field] = ''; 141 | } 142 | } 143 | return $flipped; 144 | } 145 | 146 | function _findString($string, $title, $pattern = '[^.]*\.') 147 | { 148 | preg_match("/$title:$pattern/", str_replace('*', '', $string), $matched); 149 | if (!empty($matched[0])) { 150 | return trim(str_replace($title . ':', '', $matched[0]), ' .'); 151 | } 152 | } 153 | 154 | function _findElements($string, $title, $pattern = '[^.]*\.') 155 | { 156 | $elementsString = $this->_findString($string, $title, $pattern); 157 | $elements = []; 158 | if ($elementsString) { 159 | eval('$elements = ' . $elementsString . ';'); 160 | } 161 | $finalElements = []; 162 | if (!empty($elements)) { 163 | foreach ($elements as $element) { 164 | $finalElements[] = [ 165 | 'title' => ucfirst(str_replace('_', ' ', $element)), 166 | 'key' => $element, 167 | ]; 168 | } 169 | } 170 | return $finalElements; 171 | } 172 | } 173 | -------------------------------------------------------------------------------- /views/default/index.php: -------------------------------------------------------------------------------- 1 | title = 'Documentation'; 7 | 8 | $methodColorMap = [ 9 | 'GET' => 'info', 10 | 'HEAD' => 'info', 11 | 'OPTIONS' => 'info', 12 | 'DELETE' => 'danger', 13 | 'POST' => 'success', 14 | 'PUT' => 'warning', 15 | 'PATCH' => 'warning', 16 | ]; 17 | 18 | ?> 19 |
    20 |
    21 |
    22 |
    23 | 24 |
    25 |
    26 |
    27 |
    28 | 29 |
    30 |
    31 |
    32 | 33 |
    34 | $entity) : ?> 35 |
    36 |
    37 |

    38 | 39 | 40 | 41 |

    42 |
    43 |
    44 |
    45 | 46 |
    47 | $rule) : ?> 48 | 64 | 65 |
    66 |
    67 | 68 |

    69 | 70 |
    71 | 72 | 73 | 74 |
    75 | Query Parameters 76 | 77 |
    78 | 79 | 80 |
    81 | 82 |
    83 | 84 | 85 |
    86 | Query Filters 87 | 88 |
    89 | 90 | 91 |
    92 | 93 |
    94 | 95 | 96 |
    97 | Expand Parameters 98 | 99 |
    100 | 104 |
    105 | 106 |
    107 | 108 | 109 |
    110 |
    111 |
    112 | 113 | 114 |
    115 | 116 |
    117 | $fields) : ?> 118 | 119 |
    120 | 121 |
    122 | 123 |
    124 | 125 |
    126 |
    127 | 128 | 129 |
    130 | 131 |
    132 | 133 | 134 |
    135 | 136 |
    137 | 138 |
    139 | 140 | 141 | 142 | 143 |
    144 |
    145 | 146 | 154 |
    155 |
    156 |
    157 |
    158 | 159 | 160 |
    161 | 162 |
    163 |
    164 |
    165 | 166 |
    167 |
    168 | --------------------------------------------------------------------------------