Skip to content

Commit 0fb9adc

Browse files
committed
Initial commit of current plugin code
1 parent c86bf43 commit 0fb9adc

10 files changed

Lines changed: 305 additions & 0 deletions

File tree

src/Controller/SwaggerUiTrait.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
namespace SwaggerUi\Controller;
4+
5+
use Cake\Core\Configure;
6+
use Cake\Http\Response;
7+
use Symfony\Component\Yaml\Yaml;
8+
9+
/**
10+
* SwaggerUi Controller Trait
11+
*
12+
* @property \Cake\Http\Response $response The CakePHP HTTP Response it uses.
13+
* @todo Test by utilizing a small test app with route, config & controller.
14+
*/
15+
trait SwaggerUiTrait
16+
{
17+
/**
18+
* The Swagger UI
19+
*
20+
* @param string|null $apiName The configuration name of the API to show, defaults to "default".
21+
* @return \Cake\Http\Response|null|void
22+
*/
23+
public function swaggerUi($apiName = 'default')
24+
{
25+
$openApiSpecification = $this->_getSpecificationFile($apiName);
26+
$openApiSpecification = Yaml::parseFile($openApiSpecification);
27+
$openApiSpecification = json_encode($openApiSpecification);
28+
$this->set('openApiSpecification', $openApiSpecification);
29+
$this->set('title', $apiName);
30+
$this->render('SwaggerUi.SwaggerUi/index');
31+
}
32+
33+
/**
34+
* The Swagger UI
35+
*
36+
* @param string|null $apiName The configuration name of the API to show, defaults to "default".
37+
* @return \Cake\Http\Response
38+
*/
39+
public function swaggerFile($apiName = 'default'): Response
40+
{
41+
$openApiSpecification = $this->_getSpecificationFile($apiName);
42+
$this->response = $this->response->withFile($openApiSpecification);
43+
44+
return $this->response;
45+
}
46+
47+
/**
48+
* Get the OpenAPI specification file for the given API name
49+
*
50+
* @param string $apiName The API
51+
* @return string The OpenAPI specification file path.
52+
*/
53+
protected function _getSpecificationFile($apiName): string
54+
{
55+
$configKey = 'SwaggerUi.apis.' . $apiName . '.file';
56+
$openApiSpecification = Configure::consumeOrFail($configKey);
57+
58+
return $openApiSpecification;
59+
}
60+
}

src/Plugin.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace SwaggerUi;
4+
5+
use Cake\Core\BasePlugin;
6+
7+
/**
8+
* Plugin for SwaggerUi
9+
*/
10+
class Plugin extends BasePlugin
11+
{
12+
}

src/Template/SwaggerUi/index.ctp

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?php
2+
/**
3+
* Swagger UI Template
4+
*
5+
* @var \App\View\AppView $this
6+
* @var string $title The title of the Swagger UI page.
7+
* @var string $swaggerSPecUrl The URL to the Open API specification file.
8+
*/
9+
$this->setLayout(false);
10+
$swaggerSPecUrl = $swaggerSPecUrl ?? 'https://petstore.swagger.io/v2/swagger.json';
11+
12+
if (!isset($title)) {
13+
$title = 'API Specification';
14+
}
15+
?>
16+
<!-- HTML for static distribution bundle build -->
17+
<!DOCTYPE html>
18+
<html lang="en">
19+
<head>
20+
<meta charset="UTF-8">
21+
<title><?= $title ?> - Swagger UI</title>
22+
<?= $this->Html->css('/swagger_ui/css/swagger-ui.css') ?>
23+
<?= $this->Html->meta('icon', '/swagger_ui/img/favicon-32x32.png', ['sizes' => '32x32']) ?>
24+
<?= $this->Html->meta('icon', '/swagger_ui/img/favicon-16x16.png', ['sizes' => '16x16']) ?>
25+
<style>
26+
html
27+
{
28+
box-sizing: border-box;
29+
overflow: -moz-scrollbars-vertical;
30+
overflow-y: scroll;
31+
}
32+
33+
*,
34+
*:before,
35+
*:after
36+
{
37+
box-sizing: inherit;
38+
}
39+
40+
body
41+
{
42+
margin:0;
43+
background: #fafafa;
44+
}
45+
</style>
46+
</head>
47+
48+
<body>
49+
<div id="swagger-ui"></div>
50+
51+
<?= $this->Html->script('/swagger_ui/js/swagger-ui-bundle.js') ?>
52+
<?= $this->Html->script('/swagger_ui/js/swagger-ui-standalone-preset.js') ?>
53+
<script>
54+
window.onload = function() {
55+
// Begin Swagger UI call region
56+
const ui = SwaggerUIBundle({
57+
spec: <?= $openApiSpecification ?>,
58+
//url: "<?//= $swaggerSPecUrl ?>//",
59+
dom_id: '#swagger-ui',
60+
deepLinking: true,
61+
presets: [
62+
SwaggerUIBundle.presets.apis,
63+
// SwaggerUIStandalonePreset
64+
],
65+
plugins: [
66+
SwaggerUIBundle.plugins.DownloadUrl
67+
],
68+
// layout: "StandaloneLayout"
69+
})
70+
// End Swagger UI call region
71+
72+
window.ui = ui
73+
}
74+
</script>
75+
</body>
76+
</html>

tests/bootstrap.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
/**
3+
* Test suite bootstrap for SwaggerUi.
4+
*
5+
* This function is used to find the location of CakePHP whether CakePHP
6+
* has been installed as a dependency of the plugin, or the plugin is itself
7+
* installed as a dependency of an application.
8+
*/
9+
$findRoot = function ($root) {
10+
do {
11+
$lastRoot = $root;
12+
$root = dirname($root);
13+
if (is_dir($root . '/vendor/cakephp/cakephp')) {
14+
return $root;
15+
}
16+
} while ($root !== $lastRoot);
17+
18+
throw new Exception("Cannot find the root of the application, unable to run tests");
19+
};
20+
$root = $findRoot(__FILE__);
21+
unset($findRoot);
22+
23+
chdir($root);
24+
25+
require_once $root . '/vendor/autoload.php';
26+
27+
/**
28+
* Define fallback values for required constants and configuration.
29+
* To customize constants and configuration remove this require
30+
* and define the data required by your plugin here.
31+
*/
32+
require_once $root . '/vendor/cakephp/cakephp/tests/bootstrap.php';
33+
34+
if (file_exists($root . '/config/bootstrap.php')) {
35+
require $root . '/config/bootstrap.php';
36+
37+
return;
38+
}

webroot/css/swagger-ui.css

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webroot/img/favicon-16x16.png

665 Bytes
Loading

webroot/img/favicon-32x32.png

628 Bytes
Loading

webroot/js/swagger-ui-bundle.js

Lines changed: 93 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webroot/js/swagger-ui-standalone-preset.js

Lines changed: 14 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

webroot/js/swagger-ui.js

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)