Skip to content

Commit

Permalink
初次提交
Browse files Browse the repository at this point in the history
  • Loading branch information
huijiewei committed Jul 8, 2018
0 parents commit a4be6c8
Show file tree
Hide file tree
Showing 8 changed files with 510 additions and 0 deletions.
102 changes: 102 additions & 0 deletions .gitignore
@@ -0,0 +1,102 @@
# Created by .ignore support plugin (hsz.mobi)
### JetBrains template
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839


# CMake
cmake-build-debug/

# Mongo Explorer plugin:
.idea/**/mongoSettings.xml

## File-based project format:
*.iws

## Plugin-specific files:

# IntelliJ
out/

# mpeltonen/sbt-idea plugin
.idea_modules/

# JIRA plugin
atlassian-ide-plugin.xml

# Cursive Clojure plugin
.idea/replstate.xml

# Crashlytics plugin (for Android Studio and IntelliJ)
com_crashlytics_export_strings.xml
crashlytics.properties
crashlytics-build.properties
fabric.properties
### Linux template
*~

# temporary files which can be created if a process still has a handle open of a deleted file
.fuse_hidden*

# KDE directory preferences
.directory

# Linux trash folder which might appear on any partition or disk
.Trash-*

# .nfs files are created when an open file is removed but is still being accessed
.nfs*
### macOS template
# General
.DS_Store
.AppleDouble
.LSOverride

# Icon must end with two \r
Icon

# Thumbnails
._*

# Files that might appear in the root of a volume
.DocumentRevisions-V100
.fseventsd
.Spotlight-V100
.TemporaryItems
.Trashes
.VolumeIcon.icns
.com.apple.timemachine.donotpresent

# Directories potentially created on remote AFP share
.AppleDB
.AppleDesktop
Network Trash Folder
Temporary Items
.apdisk
### Windows template
# Windows thumbnail cache files
Thumbs.db
ehthumbs.db
ehthumbs_vista.db

# Dump file
*.stackdump

# Folder config file
Desktop.ini

# Recycle Bin used on file shares
$RECYCLE.BIN/

# Windows Installer files
*.cab
*.msi
*.msm
*.msp

# Windows shortcuts
*.lnk

/vendor/
.idea
composer.lock
57 changes: 57 additions & 0 deletions README.md
@@ -0,0 +1,57 @@
# yii2-swagger
Yii2 Swagger 扩展

## 安装
```
composer require huijiewei/yii2-swagger
```

## 用法

##### Controller 里面集成 Action:
```php
public function actions()
{
return [
'ui' => [
'class' => 'huijiewei\swagger\actions\SwaggerUiAction',
'apiUrl' => \yii\helpers\Url::to(['/site/api'], true),
],
'api' => [
'class' => 'huijiewei\swagger\actions\SwaggerApiAction',
'scanDir' => '扫描的目录,可以是数组,支持 yii 的 alias',
],
];
}
```

##### Module 里面集成 Controller:
```php
public function init()
{
parent::init();

$this->controllerMap = [
'swagger' => [
'class' => 'huijiewei\swagger\SwaggerController',
'apiOptions' => [
'scanDir' => '扫描的目录,可以是数组,支持 yii 的 alias',
'defines' => [ // 可以定义一些常量,具体查阅 swagger-php 文档
'API_HOST' => 'API_HOST',
'API_BASE_PATH' => 'BASE_PATH'
]
],
'uiOptions' => [
'apiUrlRoute' => 'swagger/api'
]
],
];
}
```

### 更多文档
查阅 [Swagger PHP 文档](http://zircote.com/swagger-php/).

查阅 [Swagger UI 文档](https://github.com/swagger-api/swagger-ui).

感谢 `swagger-php` `swagger-ui`
30 changes: 30 additions & 0 deletions composer.json
@@ -0,0 +1,30 @@
{
"name": "huijiewei/yii2-swagger",
"description": "Swagger 扩展",
"type": "yii2-extension",
"license": "MIT",
"authors": [
{
"name": "Huijie Wei",
"email": "huijiewei@outlook.com"
}
],
"minimum-stability": "dev",
"require": {
"php": "^7.1.3",
"yiisoft/yii2": "^2.0",
"zircote/swagger-php": "^2.0",
"bower-asset/swagger-ui": "^3.0"
},
"autoload": {
"psr-4": {
"huijiewei\\swagger\\": "src"
}
},
"repositories": {
"asset": {
"type": "composer",
"url": "https://asset-packagist.org"
}
}
}
39 changes: 39 additions & 0 deletions src/SwaggerController.php
@@ -0,0 +1,39 @@
<?php
/**
* Created by PhpStorm.
* User: huijiewei
* Date: 2018/7/8
* Time: 17:07
*/

namespace huijiewei\swagger;

use huijiewei\swagger\actions\SwaggerApiAction;
use huijiewei\swagger\actions\SwaggerUiAction;
use yii\helpers\Url;
use yii\web\Controller;

class SwaggerController extends Controller
{
public $apiOptions = [];
public $uiOptions = [
'apiUrlRoute'
];

public function actions()
{
$apiUrlRoute = $this->uiOptions['apiUrlRoute'];

unset($this->uiOptions['apiUrlRoute']);

return [
'api' => array_merge([
'class' => SwaggerApiAction::class,
], $this->apiOptions),
'ui' => array_merge([
'class' => SwaggerUiAction::class,
'apiUrl' => Url::toRoute([$apiUrlRoute], true)
], $this->uiOptions),
];
}
}
30 changes: 30 additions & 0 deletions src/SwaggerUiAsset.php
@@ -0,0 +1,30 @@
<?php
/**
* Created by PhpStorm.
* User: Huijiewei
* Date: 2017/7/6
* Time: 下午5:04
*/

namespace huijiewei\swagger;

use yii\web\AssetBundle;
use yii\web\View;

class SwaggerUiAsset extends AssetBundle
{
public $sourcePath = '@bower/swagger-ui/dist';

public $js = [
'swagger-ui-bundle.js',
'swagger-ui-standalone-preset.js',
];

public $jsOptions = [
'position' => View::POS_END,
];

public $css = [
'swagger-ui.css',
];
}
77 changes: 77 additions & 0 deletions src/actions/SwaggerApiAction.php
@@ -0,0 +1,77 @@
<?php
/**
* Created by PhpStorm.
* User: huijiewei
* Date: 2018/7/8
* Time: 16:31
*/

namespace huijiewei\swagger\actions;

use yii\base\Action;
use yii\caching\CacheInterface;
use yii\di\Instance;
use yii\helpers\StringHelper;
use yii\web\Response;

class SwaggerApiAction extends Action
{
public $scanDir;
public $scanOptions = [];

public $cache = 'cache';

public $enableCache = false;

public $cacheKey = 'bp-api-swagger-cache';

public $defines = [];

public function init()
{
$this->cache = Instance::ensure($this->cache, CacheInterface::class);
}

public function run($clearCache = false)
{
if ($clearCache !== false) {
$this->cache->delete($this->cacheKey);

return 'Swagger API 文档缓存清理成功';
}


foreach ($this->defines as $define => $value) {
defined($define) || define($define, $value);
}

\Yii::$app->getResponse()->format = Response::FORMAT_JSON;

if ($this->enableCache) {
$swagger = $this->cache->get($this->cacheKey);

if ($swagger !== false) {
return $swagger;
}
}

$swagger = $this->getSwagger();

if ($this->enableCache) {
$this->cache->set($this->cacheKey, $swagger);
}

return $swagger;
}

protected function getSwagger()
{
$scanDir = $this->scanDir;

if (StringHelper::startsWith($scanDir, '@')) {
$scanDir = \Yii::getAlias($scanDir);
}

return \Swagger\scan($scanDir, $this->scanOptions);
}
}

0 comments on commit a4be6c8

Please sign in to comment.