|
14 | 14 | use Yii;
|
15 | 15 | use yii\base\Action;
|
16 | 16 | use yii\base\InvalidArgumentException;
|
| 17 | +use yii\helpers\Json; |
17 | 18 | use yii\web\AssetBundle;
|
| 19 | +use yii\web\JsExpression; |
18 | 20 | use yii\web\Response;
|
19 | 21 |
|
20 | 22 | /**
|
@@ -61,52 +63,118 @@ class SwaggerAction extends Action
|
61 | 63 | /**
|
62 | 64 | * @var string|array The rest url configuration.
|
63 | 65 | * 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 |
65 | 67 | */
|
66 | 68 | public $restUrl;
|
67 | 69 | /**
|
68 |
| - * @var array The OAuth configration |
| 70 | + * @var array The OAuth configuration. |
69 | 71 | */
|
70 | 72 | public $oauthConfiguration = [];
|
71 |
| - |
| 73 | + /** |
| 74 | + * @var string The customer asset bundle. |
| 75 | + * @since 2.0.0 |
| 76 | + */ |
72 | 77 | 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 | + */ |
74 | 111 | public function run()
|
75 | 112 | {
|
76 | 113 | Yii::$app->getResponse()->format = Response::FORMAT_HTML;
|
77 |
| - |
| 114 | + |
78 | 115 | $this->controller->layout = false;
|
79 |
| - |
| 116 | + |
80 | 117 | $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 | + |
93 | 119 | 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, |
96 | 123 | ], $this->controller);
|
97 | 124 | }
|
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 | + */ |
99 | 167 | protected function beforeRun()
|
100 | 168 | {
|
101 | 169 | if ($this->additionalAsset != null) {
|
102 | 170 | $additionalAsset = $this->additionalAsset;
|
103 | 171 | if (class_exists($additionalAsset)) {
|
104 |
| - $additionalAsset::register($this->controller->view); |
| 172 | + $additionalAsset::register($this->controller->getView()); |
105 | 173 | } else {
|
106 |
| - throw new InvalidArgumentException("Not valid class"); |
| 174 | + throw new InvalidArgumentException('Not valid class'); |
107 | 175 | }
|
108 | 176 | }
|
109 |
| - |
| 177 | + |
110 | 178 | return parent::beforeRun();
|
111 | 179 | }
|
112 | 180 | }
|
0 commit comments