├── .php_cs ├── SwaggerAsset.php ├── composer.json ├── LICENSE ├── SwaggerUIRenderer.php ├── OpenAPIRenderer.php ├── views └── index.php └── README.md /.php_cs: -------------------------------------------------------------------------------- 1 | exclude('vendor') 5 | ->in([__DIR__]); 6 | 7 | $config = PhpCsFixer\Config::create() 8 | ->setUsingCache(false) 9 | ->setRules([ 10 | '@Symfony' => true, 11 | 'phpdoc_align' => false, 12 | 'phpdoc_summary' => false, 13 | 'phpdoc_inline_tag' => false, 14 | 'pre_increment' => false, 15 | 'heredoc_to_nowdoc' => false, 16 | 'cast_spaces' => false, 17 | 'include' => false, 18 | 'phpdoc_no_package' => false, 19 | 'concat_space' => ['spacing' => 'one'], 20 | 'ordered_imports' => true, 21 | 'array_syntax' => ['syntax' => 'short'], 22 | 'yoda_style' => false, 23 | ]) 24 | ->setFinder($finder); 25 | 26 | return $config; 27 | -------------------------------------------------------------------------------- /SwaggerAsset.php: -------------------------------------------------------------------------------- 1 | =7.1.0", 19 | "yiisoft/yii2": "~2.0.12", 20 | "zircote/swagger-php": "^2.0", 21 | "bower-asset/swagger-ui": "^3.1" 22 | }, 23 | "require-dev": { 24 | "friendsofphp/php-cs-fixer": "~2.0", 25 | "phpunit/phpunit": "~6.0" 26 | }, 27 | "autoload": { 28 | "psr-4": { 29 | "yii2mod\\swagger\\": "" 30 | } 31 | }, 32 | "repositories": [ 33 | { 34 | "type": "composer", 35 | "url": "https://asset-packagist.org" 36 | } 37 | ] 38 | } 39 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Yii2 modules & extensions 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 | -------------------------------------------------------------------------------- /SwaggerUIRenderer.php: -------------------------------------------------------------------------------- 1 | controller->layout = $this->layout; 38 | 39 | return $this->controller->render($this->view, [ 40 | 'restUrl' => $this->restUrl, 41 | ]); 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /OpenAPIRenderer.php: -------------------------------------------------------------------------------- 1 | cache !== null) { 59 | $this->cache = Instance::ensure($this->cache, Cache::class); 60 | } 61 | } 62 | 63 | /** 64 | * @inheritdoc 65 | */ 66 | public function run(): Response 67 | { 68 | $this->enableCORS(); 69 | 70 | return $this->controller->asJson($this->getSwaggerDocumentation()); 71 | } 72 | 73 | /** 74 | * Scan the filesystem for swagger annotations and build swagger-documentation. 75 | * 76 | * @return Swagger 77 | */ 78 | protected function getSwaggerDocumentation(): Swagger 79 | { 80 | if (!$this->cache instanceof Cache) { 81 | return \Swagger\scan($this->scanDir, $this->scanOptions); 82 | } 83 | 84 | return $this->cache->getOrSet($this->cacheKey, function () { 85 | return \Swagger\scan($this->scanDir, $this->scanOptions); 86 | }, $this->cacheDuration); 87 | } 88 | 89 | /** 90 | * Enable CORS 91 | */ 92 | protected function enableCORS(): void 93 | { 94 | $headers = Yii::$app->getResponse()->getHeaders(); 95 | 96 | $headers->set('Access-Control-Allow-Headers', 'Content-Type, api_key, Authorization'); 97 | $headers->set('Access-Control-Allow-Methods', 'GET, POST, DELETE, PUT'); 98 | $headers->set('Access-Control-Allow-Origin', '*'); 99 | } 100 | } 101 | -------------------------------------------------------------------------------- /views/index.php: -------------------------------------------------------------------------------- 1 | 10 | beginPage(); ?> 11 | 12 | 13 |
14 | 15 |
2 |
3 |
4 |
5 |