Skip to content

Commit

Permalink
implementation with swagger-ui3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
lichunqiang committed Mar 30, 2018
1 parent faadb59 commit 54fe1c9
Show file tree
Hide file tree
Showing 5 changed files with 337 additions and 321 deletions.
116 changes: 92 additions & 24 deletions src/SwaggerAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
use Yii;
use yii\base\Action;
use yii\base\InvalidArgumentException;
use yii\helpers\Json;
use yii\web\AssetBundle;
use yii\web\JsExpression;
use yii\web\Response;

/**
Expand Down Expand Up @@ -61,52 +63,118 @@ class SwaggerAction extends Action
/**
* @var string|array The rest url configuration.
* Check documentation for more information.
* @doc https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
* @see https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
*/
public $restUrl;
/**
* @var array The OAuth configration
* @var array The OAuth configuration.
*/
public $oauthConfiguration = [];

/**
* @var string The customer asset bundle.
* @since 2.0.0
*/
public $additionalAsset;

/**
* @var string
* @since 2.0.0
*/
public $title = 'Swagger-ui';
/**
* @var array The swagger-ui component configurations.
* @see https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
* @since 2.0.0
*/
public $configurations = [];
/**
* @var array Default swagger-ui configurations.
* @since 2.0.0
*/
protected $defaultConfigurations = [
'dom_id' => '#swagger-ui',
'deepLinking' => true,
'presets' => [
'SwaggerUIBundle.presets.apis',
'SwaggerUIStandalonePreset',
],
'plugins' => [
'SwaggerUIBundle.plugins.DownloadUrl',
'SwaggerUIBundle.plugins.Topbar',
],
'layout' => 'StandaloneLayout',
'validatorUrl' => null,
];

/**
* @inheritdoc
*/
public function run()
{
Yii::$app->getResponse()->format = Response::FORMAT_HTML;

$this->controller->layout = false;

$view = $this->controller->getView();

if (empty($this->oauthConfiguration)) {
$this->oauthConfiguration = [
'clientId' => 'your-client-id',
'clientSecret' => 'your-client-secret-if-required',
'realm' => 'your-realms',
'appName' => 'your-app-name',
'scopeSeparator' => ' ',
'additionalQueryStringParams' => [],
];
}


return $view->renderFile(__DIR__ . '/index.php', [
'rest_url' => $this->restUrl,
'oauthConfig' => $this->oauthConfiguration,
'configurations' => $this->prepareConfiguration(),
'oauthConfiguration' => $this->oauthConfiguration,
'title' => $this->title,
], $this->controller);
}


/**
* @return string
*/
protected function prepareConfiguration()
{
$configurations = array_merge($this->defaultConfigurations, $this->configurations);

if ($this->restUrl) {
$configurations[is_array($this->restUrl) ? 'urls' : 'url'] = $this->restUrl;
}

if (isset($configurations['plugins'])) {
$configurations['plugins'] = array_map(
[$this, 'convertJsExpression'],
(array)$configurations['plugins']
);
}

if (isset($configurations['presets'])) {
$configurations['presets'] = array_map(
[$this, 'convertJsExpression'],
(array)$configurations['presets']
);
}

return Json::encode($configurations);
}

/**
* @param string $str
*
* @return JsExpression
*/
protected function convertJsExpression($str)
{
return new JsExpression($str);
}

/**
* @inheritdoc
*/
protected function beforeRun()
{
if ($this->additionalAsset != null) {
$additionalAsset = $this->additionalAsset;
if (class_exists($additionalAsset)) {
$additionalAsset::register($this->controller->view);
$additionalAsset::register($this->controller->getView());
} else {
throw new InvalidArgumentException("Not valid class");
throw new InvalidArgumentException('Not valid class');
}
}

return parent::beforeRun();
}
}
Loading

0 comments on commit 54fe1c9

Please sign in to comment.