Skip to content

Commit 54fe1c9

Browse files
committed
implementation with swagger-ui3.0
1 parent faadb59 commit 54fe1c9

File tree

5 files changed

+337
-321
lines changed

5 files changed

+337
-321
lines changed

src/SwaggerAction.php

Lines changed: 92 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@
1414
use Yii;
1515
use yii\base\Action;
1616
use yii\base\InvalidArgumentException;
17+
use yii\helpers\Json;
1718
use yii\web\AssetBundle;
19+
use yii\web\JsExpression;
1820
use yii\web\Response;
1921

2022
/**
@@ -61,52 +63,118 @@ class SwaggerAction extends Action
6163
/**
6264
* @var string|array The rest url configuration.
6365
* Check documentation for more information.
64-
* @doc https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
66+
* @see https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
6567
*/
6668
public $restUrl;
6769
/**
68-
* @var array The OAuth configration
70+
* @var array The OAuth configuration.
6971
*/
7072
public $oauthConfiguration = [];
71-
73+
/**
74+
* @var string The customer asset bundle.
75+
* @since 2.0.0
76+
*/
7277
public $additionalAsset;
73-
78+
/**
79+
* @var string
80+
* @since 2.0.0
81+
*/
82+
public $title = 'Swagger-ui';
83+
/**
84+
* @var array The swagger-ui component configurations.
85+
* @see https://github.com/swagger-api/swagger-ui/blob/master/docs/usage/configuration.md
86+
* @since 2.0.0
87+
*/
88+
public $configurations = [];
89+
/**
90+
* @var array Default swagger-ui configurations.
91+
* @since 2.0.0
92+
*/
93+
protected $defaultConfigurations = [
94+
'dom_id' => '#swagger-ui',
95+
'deepLinking' => true,
96+
'presets' => [
97+
'SwaggerUIBundle.presets.apis',
98+
'SwaggerUIStandalonePreset',
99+
],
100+
'plugins' => [
101+
'SwaggerUIBundle.plugins.DownloadUrl',
102+
'SwaggerUIBundle.plugins.Topbar',
103+
],
104+
'layout' => 'StandaloneLayout',
105+
'validatorUrl' => null,
106+
];
107+
108+
/**
109+
* @inheritdoc
110+
*/
74111
public function run()
75112
{
76113
Yii::$app->getResponse()->format = Response::FORMAT_HTML;
77-
114+
78115
$this->controller->layout = false;
79-
116+
80117
$view = $this->controller->getView();
81-
82-
if (empty($this->oauthConfiguration)) {
83-
$this->oauthConfiguration = [
84-
'clientId' => 'your-client-id',
85-
'clientSecret' => 'your-client-secret-if-required',
86-
'realm' => 'your-realms',
87-
'appName' => 'your-app-name',
88-
'scopeSeparator' => ' ',
89-
'additionalQueryStringParams' => [],
90-
];
91-
}
92-
118+
93119
return $view->renderFile(__DIR__ . '/index.php', [
94-
'rest_url' => $this->restUrl,
95-
'oauthConfig' => $this->oauthConfiguration,
120+
'configurations' => $this->prepareConfiguration(),
121+
'oauthConfiguration' => $this->oauthConfiguration,
122+
'title' => $this->title,
96123
], $this->controller);
97124
}
98-
125+
126+
/**
127+
* @return string
128+
*/
129+
protected function prepareConfiguration()
130+
{
131+
$configurations = array_merge($this->defaultConfigurations, $this->configurations);
132+
133+
if ($this->restUrl) {
134+
$configurations[is_array($this->restUrl) ? 'urls' : 'url'] = $this->restUrl;
135+
}
136+
137+
if (isset($configurations['plugins'])) {
138+
$configurations['plugins'] = array_map(
139+
[$this, 'convertJsExpression'],
140+
(array)$configurations['plugins']
141+
);
142+
}
143+
144+
if (isset($configurations['presets'])) {
145+
$configurations['presets'] = array_map(
146+
[$this, 'convertJsExpression'],
147+
(array)$configurations['presets']
148+
);
149+
}
150+
151+
return Json::encode($configurations);
152+
}
153+
154+
/**
155+
* @param string $str
156+
*
157+
* @return JsExpression
158+
*/
159+
protected function convertJsExpression($str)
160+
{
161+
return new JsExpression($str);
162+
}
163+
164+
/**
165+
* @inheritdoc
166+
*/
99167
protected function beforeRun()
100168
{
101169
if ($this->additionalAsset != null) {
102170
$additionalAsset = $this->additionalAsset;
103171
if (class_exists($additionalAsset)) {
104-
$additionalAsset::register($this->controller->view);
172+
$additionalAsset::register($this->controller->getView());
105173
} else {
106-
throw new InvalidArgumentException("Not valid class");
174+
throw new InvalidArgumentException('Not valid class');
107175
}
108176
}
109-
177+
110178
return parent::beforeRun();
111179
}
112180
}

0 commit comments

Comments
 (0)